Тестирование Junit: org.springframework.http.InvalidMediaTypeException: неверный тип пантомимы - PullRequest
0 голосов
/ 30 апреля 2018

Я пытаюсь написать 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

Ответы [ 3 ]

0 голосов
/ 30 апреля 2018

Интересная часть вашей трассы есть:

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)

Мы можем прочитать, что метод MockHttpServletRequestBuilder.contentType(String contentType) устанавливает заголовок Content-Type запроса на построение, но он получил в качестве параметра недопустимый объект: сам объект JSON.
Он точно говорит, что does not contain '/', потому что Content-Type должен иметь в своем значении символ /, такой как this .
Поэтому вы должны проверить свой реальный код, чтобы создать запрос, поскольку вы, кажется, передаете объект JSON в качестве заголовка в заголовке Content-Type, в то время как вы должны передать значение String, например application/json.

0 голосов
/ 30 апреля 2018

После просмотра журнала ошибок становится ясно, что 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 '/'

Проверьте еще раз конфигурации и код, который вы обязательно узнаете сами.

0 голосов
/ 30 апреля 2018

Не могли бы вы проверить эту часть вашего кода

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