【问题标题】:Junit Testing : org.springframework.http.InvalidMediaTypeException: Invalid mime typeJunit测试:org.springframework.http.InvalidMediaTypeException:无效的mime类型
【发布时间】:2018-04-30 10:52:23
【问题描述】:

我正在尝试为发布请求编写JUnit 测试用例。我正在尝试发送正文以进行发布请求。我尝试如下但我得到org.springframework.http.InvalidMediaTypeException 谁能告诉我我在下面的代码中缺少什么?

JSON 格式

{        
    "nAccountId":651754,
     "sLocation": "B",
     "nAccountCPCMappingid":8,
     "sAcctDesc":" FY07 GEN GIFTS" ,
     "nDeptId":21728,
     "sClientAcctId":"2100012",
     "nInvestigatorId":65760

}

我试过这样

String json = "{\n" + 
                "        \"nAccountId\":\"651754\", \"sLocation\": \"B\",\n" + 
                "         \"nAccountCPCMappingid\":\"8\",\"sAcctDesc\":\" FY07 GEN GIFTS\" ,\n" + 
                "         \"nDeptId\":\"21728\", \"sClientAcctId\":\"2100012\",\n" + 
                "         \"nInvestigatorId\":\"65760\" }";

堆栈跟踪

org.springframework.http.InvalidMediaTypeException: Invalid mime type "{
        "nAccountId":"651754", "sLocation": "B",
         "nAccountCPCMappingid":"8","sAcctDesc":" FY07 GEN GIFTS" ,
         "nDeptId":"21728", "sClientAcctId":"2100012",
         "nInvestigatorId":"65760" }": does not contain '/'
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:452)
    at org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.contentType(MockHttpServletRequestBuilder.java:272)
    at com.spacestudy.AccountControllerTest.btnSaveClickTest(AccountControllerTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: org.springframework.util.InvalidMimeTypeException: Invalid mime type "{
        "nAccountId":"651754", "sLocation": "B",
         "nAccountCPCMappingid":"8","sAcctDesc":" FY07 GEN GIFTS" ,
         "nDeptId":"21728", "sClientAcctId":"2100012",
         "nInvestigatorId":"65760" }": does not contain '/'
    at org.springframework.util.MimeTypeUtils.parseMimeType(MimeTypeUtils.java:256)
    at org.springframework.http.MediaType.parseMediaType(MediaType.java:449)
    ... 32 more

【问题讨论】:

    标签: spring unit-testing junit4


    【解决方案1】:

    在看到您的错误日志后,很明显 Content-Type 是错误的,应该是 application/json 但您将 JSON 对象本身作为 Content-Type 传递.仔细看下面一行

    org.springframework.http.InvalidMediaTypeException: Invalid mime type "{
            "nAccountId":"651754", "sLocation": "B",
             "nAccountCPCMappingid":"8","sAcctDesc":" FY07 GEN GIFTS" ,
             "nDeptId":"21728", "sClientAcctId":"2100012",
             "nInvestigatorId":"65760" }": does not contain '/'
    

    再次查看配置和代码,您一定会自己发现。

    【讨论】:

    • 我解决了我的错误。 String json没有问题。问题是我写的是contentType(json) 而不是content
    【解决方案2】:

    你的 stracktrace 有趣的部分就在那里:

    org.springframework.http.InvalidMediaTypeException:无效的 mime 类型“{ “nAccountId”:“651754”,“sLocation”:“B”, "nAccountCPCMappingid":"8","sAcctDesc":"FY07 GEN GIFTS" , “nDeptId”:“21728”,“sClientAcctId”:“2100012”, "nInvestigatorId":"65760" }": 不包含'/' 在 org.springframework.http.MediaType.parseMediaType(MediaType.java:452) 在 org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.contentType(MockHttpServletRequestBuilder.java:272) 在 com.spacestudy.AccountControllerTest.btnSaveClickTest(AccountControllerTest.java:51)

    我们可以读到MockHttpServletRequestBuilder.contentType(String contentType) 方法设置了要构建的请求的Content-Type 标头,但它作为无效参数接收:JSON 对象本身。
    它确切地说是does not contain '/',因为Content-Type 的值中应该有一个/ 字符,例如these one
    因此,您应该检查实际代码以创建请求,因为您似乎将 JSON 对象作为标头传递到 Content-Type 标头中,而您应该传递 String 值,例如 application/json

    【讨论】:

    • 感谢您的宝贵时间和建议。问题是我正在写 ContentType 而不是 content 并在其中传递字符串 JSON 变量
    【解决方案3】:

    您能否检查一下您的这部分代码

    String json = "{\n" + 
                    "        \"nAccountId\":\"651754\", \"sLocation\": \"B\",\n" + 
                    "         \"nAccountCPCMappingid\":\"8\",\"sAcctDesc\":\" FY07 GEN GIFTS\" ,\n" + 
                    "         \"nDeptId\":\"21728\", \"sClientAcctId\":\"2100012\",\n" + 
                    "         \"nInvestigatorId\":\"65760\" }";
    

    【讨论】:

    • 我解决了我的错误。 String json没有问题。问题是我写的是contentType(json) 而不是content
    猜你喜欢
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多