使用ClientHttpRequestInterceptor进行单元测试Rest客户端

时间:2016-10-07 09:49:57

标签: unit-testing logging spring-boot

以下单元测试失败。我正在打印请求和响应,并且可以确认MockRestServiceServer在调用端点时返回模拟的JSON。当我更改测试以直接与服务器通信时,单元测试通过。不确定我做错了什么

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {Application.class, Beans.class})
public class BillingSystemClientImplTest {

    private MockRestServiceServer mockServer;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private Properties properties;

    @Autowired
    private BillingSystemClient client;

    @Before
    public void setUp() {
        mockServer = MockRestServiceServer.createServer(restTemplate);
    }

    @Test
    public void testGetAccount() throws Exception {

        Resource resource = new ClassPathResource("/account.json", getClass());

        UriComponents uri = UriComponentsBuilder.fromUriString(properties.getAccountResource())
                .buildAndExpand("53737803");

        mockServer.expect(requestTo(uri.toUriString()))
                .andExpect(method(HttpMethod.GET))
                .andRespond(withSuccess(resource, MediaType.APPLICATION_JSON));

        AccountResponse response = client.getAccount("53737803");

        Assert.assertNotNull(response.getAccountNumber());

        mockServer.verify();

    }

}

以下异常会抱怨响应对象为null。此对象从响应

映射
    response = restTemplate.getForObject(uriComponents.toUri(), AccountResponse.class);

异常

java.lang.NullPointerException
    at com.something.ws.client.BillingSystemClientImplTest.testGetAccount(BillingSystemClientImplTest.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    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:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    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:193)

事实证明这是因为我如何设置我的restTemplate

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        BufferingClientHttpRequestFactory bufferingClientHttpRequestFactory = new BufferingClientHttpRequestFactory(requestFactory);
        requestFactory.setOutputStreaming(false);

        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getInterceptors().add(mpxLoggingRequestInterceptor());
        restTemplate.setErrorHandler(mpxErrorHandler());
        restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
        restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
        restTemplate.setRequestFactory(new BufferingClientHttpRequestFactory(new SimpleClientHttpRequestFactory()));
        restTemplate.getInterceptors().add(new GenericRequestInterceptor());
        restTemplate.setRequestFactory(bufferingClientHttpRequestFactory);

如果我删除了RequestFactory和Interceptor,测试就会通过。这只是单元测试的问题。实际的代码在生产中顺利运行

3 个答案:

答案 0 :(得分:0)

在使用MockRestServiceServer来测试带有拦截器的Rest客户端时遇到了相同的问题。
使用MockRestServiceServer.reset()不能解决问题 因此,我更改了单元测试方法,并使用mockito注入模拟restTemplate

 @RunWith(SpringJUnit4ClassRunner.class)

    public class BillingSystemClientImplTest {


        @Mock
        private RestTemplate restTemplate;


        @InjectMocks
        private BillingSystemClient client = new BillingSystemClient();


        @Test
        public void testGetAccount() throws Exception {

            Resource resource = new ClassPathResource("/account.json", getClass());

            when(restTemplate.exchange(any(String.class), eq(HttpMethod.GET), eq(null), eq(Resource.class)))
                    .thenReturn(new ResponseEntity(resource, HttpStatus.OK));

            AccountResponse response = client.getAccount("53737803");

            Assert.assertNotNull(response.getAccountNumber());


        }

    }

但是,如果您要测试拦截器逻辑,这可能无济于事

答案 1 :(得分:0)

您需要启用对响应主体的重复读取,因此在创建MockRestServiceServer时,请像这样调用bufferContent():

        mockServer = MockRestServiceServer
            .bindTo(restTemplate)
            .ignoreExpectOrder(true)
            .bufferContent()
            .build();

答案 2 :(得分:0)

这在我的案例中有效:

  • 正在测试的我的REST服务使用BufferingClientHttpRequestFactory,因为出于HTTP调试目的,需要对HTTP响应进行缓冲并且可读多次。
  • 未安装用于HTTP日志记录的spring HTTP请求拦截器时,测试通过。
  • 激活日志记录时,restTemplate是使用拦截器自定义的:然后测试失败。
  • 替换我的初始代码

    mockServer = MockRestServiceServer.createServer(restTemplate);
    

    与建议

    mockServer = MockRestServiceServer
                .bindTo(restTemplate)
                .ignoreExpectOrder(true)
                .bufferContent()
                .build();
    

    解决了该问题。现在可以通过测试,即使在HTTP调试模式下也可以通过

谢谢!!