SpringUser
SpringUser

Reputation: 1439

Junit Testing : org.springframework.http.InvalidMediaTypeException: Invalid mime type

I am trying to write JUnit test case for post request. I am trying to send body for post request. I tried like below But I am getting org.springframework.http.InvalidMediaTypeException can any one please tell me what I am missing in below code?

JSON format

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

}

I tried like this

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

Stack Trace

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

Upvotes: 0

Views: 1888

Answers (3)

ajay tomar
ajay tomar

Reputation: 382

After seeing your error log it is pretty much clear that Content-Type is wrong which should be application/json but you passes the JSON object itself as Content-Type. See below line carefully

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 '/'

Check again the configurations and code you will definitely find out yourself.

Upvotes: 3

davidxxx
davidxxx

Reputation: 131346

The interesting part of your stracktrace is there :

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)

We can read that the MockHttpServletRequestBuilder.contentType(String contentType) method sets the Content-Type header of the request to build but it received as parameter for an invalid one : the JSON object itself.
It says exactly that does not contain '/' because a Content-Type should have a / character in its value such as these one.
So you should check your actual code to create the request as you seem to pass the JSON object as header in the Content-Type header while you should pass a String value such as application/json.

Upvotes: 2

user9720770
user9720770

Reputation:

Could you please check this part of of your code

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

Upvotes: 0

Related Questions