방배동놈
95
2018-11-06 12:46:36
4
2157

REST API 관련 POST 테스트 중 JSON 문제? 도움 부탁드립니다.


안녕하세요 점심 식사는 맛있게들 하셨나요?

현재 REST API의 각 Endpoint들을 junit을 사용하여 테스트 중에 있습니다. 아직 신입이라 테스트에 관해 부족한 점이 너무나도 많습니다..

http request method 중 get은 문제 없이 잘 작동하고 있으나, body를 전달하는 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));
    }


@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);
	}

아마도 map->string으로 변환하는 과정에서 무언가 빠진 것 같은데 무엇을 추가해야할지 감이 안오네요..


String body = "{\"locale\":\"값\",\"empKey\":\"값\",\"applKey\":\"값\",\"apprStatCd\":\"값\",\"returnReason\":\"값\"}";

위와 같이 body를 주었을 때 문제가 없이 잘 작동하는 것을 보면 다른 코드 상의 문제는 없는 것 같습니다.

도움을 주시면 정말 감사하겠습니다!


다음은 junit 테스트 구동 시 출력되는 콘솔 메시지입니다.

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


0
0
  • 답변 4

  • 아야로
    1k
    2018-11-06 12:56:06

    json 포맷은 모든 변수명엔 큰따옴표가 붙고, 숫자는 따옴표를 붙이지 않는다와 같은 엄격한 규칙이 존재합니다.

    현재 Map 을 바로 투스트링 하는 코드를 사용하곻계신대 한 번 출력해보세요. 따옴표들이 날아간 모습을 보실 수 있지않을까 싶네요.

    String body = map.toString();
    // body를 바로 출력해보세요
    1
  • 방배동놈
    95
    2018-11-06 13:09:59
    네 맞습니다. 따옴표들이 날아간 형태로 출력되네요ㅠㅠ 이를 해결하기 위해선 어떻게 해야할까요?
    {applKey=${applKey}, returnReason=${returnReason}, apprStatCd=${apprStartCd}, empKey=${empKey}, locale=${locale}}


    0
  • 아야로
    1k
    2018-11-06 13:18:33

    방배동놈

    Json 포맷을 지원하는 라이브러리들을 사용해야하며, 다행이 스프링엔 Jackson 라이브러리가 기본 포함되어 있습니다.

    ObjectMapper mapper = new ObjectMapper();
    String json = "";
    
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("name", "mkyong");
    map.put("age", 29);
    
    // convert map to JSON string
    json = mapper.writeValueAsString(map);
    
    System.out.println(json);

    출처: https://www.mkyong.com/java/how-to-convert-java-map-to-from-json-jackson/


    0
  • 방배동놈
    95
    2018-11-06 13:36:16

    넵! 말씀하신 대로 코드 변경하여 해결하였습니다! 감사합니다:)

    	@Test
    	public void edocument_status() throws Exception {
    		ObjectMapper mapper = new ObjectMapper();
    		String body = "";
    
    		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);
    
    		body = mapper.writeValueAsString(map);
    		testPostMethod("/edocument/status", body);
    	}


    0
  • 로그인을 하시면 답변을 등록할 수 있습니다.