0

RestAssuredMockMvc.standaloneSetup()을 통해 게시물 요청을 보내려고 요청에 대해 아래 오류가 표시됩니다. 요청 및 응답 코드가있는 Groovy 파일에 내 계약이 있습니다. 나는 다음의 예외 아래 400 응답을 얻고있다. 상술 한 예외의 끝에 봄 구름 계약서에 게시 요청을 보낼 수 없습니다.


org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.organisation.online.myService.common.model.Payload, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information 
at [Source: [email protected]; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.organisation.onlinebank.myService.common.model.Payload, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information 
at [Source: [email protected]; line: 1, column: 1] 
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:240) 
at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:225) 
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:201) 
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:150) 
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:128) 
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) 
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:160) 
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) 
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) 
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) 
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963) 
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897) 
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) 
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:595) 
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) 
at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:65) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) 
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:155) 
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.performRequest(MockMvcRequestSenderImpl.java:174) 
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.sendRequest(MockMvcRequestSenderImpl.java:404) 
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.post(MockMvcRequestSenderImpl.java:590) 
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.post(MockMvcRequestSenderImpl.java:79) 
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl.post(MockMvcRequestSpecificationImpl.java:752) 
at com.jayway.restassured.module.mockmvc.internal.MockMvcRequestSpecificationImpl.post(MockMvcRequestSpecificationImpl.java:66) 
at org.springframework.cloud.contract.verifier.tests.ContractVerifierTest.validate_should_Create_post_Request(ContractVerifierTest.java:25) 
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.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) 
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.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:363) 
.. 여기서 요청 안에 전송되는 실제 페이로드는 인터페이스가

expected:<[2]00> but was:<[4]00> 
Expected :200 
Actual :400 
<Click to see difference> 

org.junit.ComparisonFailure: expected:<[2]00> but was:<[4]00> 
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 
at org.springframework.cloud.contract.verifier.tests.ContractVerifierTest.validate_should_Create_post_Request(ContractVerifierTest.java:28) 

다음 JUnit을 주장을하며 그가 될 수 알고 요청 페이로드를 생성 할 때의 문제점. 그 외에도, 나는이 오류를 다루는 방법을 모른다. 모든 단서가 인정 될 것입니다.

+1

대부분의 경우 사용자 지정 serializer를 RestAssured lib에 제공해야합니다. 또한 독립형 설정 대신 webcontext를 전달할 수도 있습니다. 어쩌면 시리얼 라이저가 자동으로 적용되는 방식일까요? –

답변

0

스프링 프레임 워크의 MappingJackson2HttpMessageConverter을 사용하여 개체를 deserialize했습니다. 다음은 구현 코드입니다.

아래의 방법으로 정적 클래스를 작성하십시오.

public class MappingJacksonFactory { 

public static MappingJackson2HttpMessageConverter create() { 
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); 
    ObjectMapper objectMapper = converter.getObjectMapper(); 
    objectMapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS); 
    objectMapper.enable(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN); 
    SimpleModule module = new TypeModuleFactory(); 
    objectMapper.registerModule(module); 
    return converter; 
    } 

} 

사용 setMessageConverter() 방법 변환기 내부의 상기 코드.

RestAssuredMockMvc.standaloneSetup(MockMvcBuilders.standaloneSetup(new yourController())) 
      .setMessageConverters(MappingJacksonFactory.create());