嗨,我目前正在用jUnit测试REST API端点。 我已经成功测试了所有GET方法的端点,现在还剩下POST和PUT端点。
首先,这是我的测试代码。
public void testPostMethod(String url, String content) throws Exception {
mockMvc.perform(post(url).contentType(MediaType.APPLICATION_JSON).content(content)).andDo(print())
.andExpect(status().isOk()).andExpect(content().contentType(contentType));
}
使用此testPostMethod,
@Test
public void edocument_status() throws Exception {
Map<String, String> map = new HashMap<String,String>();
map.put("locale", locale);
map.put("empKey", empKey);
map.put("applKey", applKey);
map.put("apprStatCd", apprStatCd);
map.put("returnReason", returnReason);
String body = map.toString();
testPostMethod("/edocument/status", body);
}
但是以下控制台消息发生错误。
2018-11-06 11:52:34,257 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader::loadBeanDefinitions:317] Loading XML bean definitions from URL [file:src/main/webapp/WEB-INF/spring/root-context.xml]
2018-11-06 11:52:34,768 INFO [org.springframework.context.support.GenericApplicationContext::prepareRefresh:510] Refreshing org.springframework.context.support.GenericApplicationContext@eafc191: startup date [Tue Nov 06 11:52:34 KST 2018]; root of context hierarchy
2018-11-06 11:52:35,101 INFO [org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker::postProcessAfterInitialization:309] Bean 'taskExecutor' of type [class org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-06 11:52:35,110 INFO [org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker::postProcessAfterInitialization:309] Bean 'customAsyncExecutor' of type [class com.pb.async.CustomAsyncExecutor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-06 11:52:35,113 INFO [org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker::postProcessAfterInitialization:309] Bean 'exceptionHandler' of type [class com.pb.async.CustomAsyncExceptionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-11-06 11:52:35,437 INFO [org.springframework.beans.factory.xml.XmlBeanDefinitionReader::loadBeanDefinitions:317] Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-codes.xml]
2018-11-06 11:52:37,490 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter::initControllerAdviceCache:517] Looking for @ControllerAdvice: org.springframework.test.web.servlet.setup.StubWebApplicationContext@3b718392
runtimeExceptionHandler :: org.springframework.http.converter.HttpMessageNotReadableException
org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('a' (code 97)): was expecting double-quote to start field name
at [Source: java.io.PushbackInputStream@5467eea4; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('a' (code 97)): was expecting double-quote to start field name
at [Source: java.io.PushbackInputStream@5467eea4; line: 1, column: 3]
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:208)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:200)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:146)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:149)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:100)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:77)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:167)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:134)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:144)
at com.isu.ifm.HttpRequestTestMethod.testPostMethod(HttpRequestTestMethod.java:136)
at com.isu.ifm.testcase.EDocumentControllerTest.edocument_status(EDocumentControllerTest.java:75)
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:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
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:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('a' (code 97)): was expecting double-quote to start field name
at [Source: java.io.PushbackInputStream@5467eea4; line: 1, column: 3]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1487)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:518)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportUnexpectedChar(ParserMinimalBase.java:447)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._handleOddName(UTF8StreamJsonParser.java:1919)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._parseName(UTF8StreamJsonParser.java:1617)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextToken(UTF8StreamJsonParser.java:700)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringMap(MapDeserializer.java:461)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:342)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:26)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3562)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2662)
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:205)
... 54 more
MockHttpServletRequest:
HTTP Method = POST
Request URI = /edocument/status
Parameters = {}
Headers = {Content-Type=[application/json]}
Handler:
Type = com.isu.ifm.hr.control.EDocumentController
Method = public com.pb.common.vo.ReturnParam com.isu.ifm.hr.control.EDocumentController.statusChange(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>) throws java.lang.Exception
Async:
Async started = false
Async result = null
Resolved Exception:
Type = org.springframework.http.converter.HttpMessageNotReadableException
ModelAndView:
View name = jsonView
View = null
Attribute = status
value = FAIL
Attribute = message
value = Could not read JSON: Unexpected character ('a' (code 97)): was expecting double-quote to start field name
at [Source: java.io.PushbackInputStream@5467eea4; line: 1, column: 3]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('a' (code 97)): was expecting double-quote to start field name
at [Source: java.io.PushbackInputStream@5467eea4; line: 1, column: 3]
FlashMap:
MockHttpServletResponse:
Status = 500
Error message = null
Headers = {}
Content type = null
Body =
Forwarded URL = jsonView
Redirected URL = null
Cookies = []
2018-11-06 11:52:37,728 INFO [org.springframework.context.support.GenericApplicationContext::doClose:862] Closing org.springframework.context.support.GenericApplicationContext@eafc191: startup date [Tue Nov 06 11:52:34 KST 2018]; root of context hierarchy
我认为Map-> String部分可能缺少某些内容。 谁能告诉我这是怎么回事?
答案 0 :(得分:1)
我用以下修改的代码解决了这个问题。
Student * students = new Student[numStudents];
答案 1 :(得分:1)