Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing Rest client with ClientHttpRequestInterceptor

The below unit test fails. I am printing the requests and responses and can confirm that MockRestServiceServer returns the mocked JSON when the endpoint is invoked. When I change the test to talk directly to the server the unit test passes. Not sure what I'm doing wrong

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

    }

}

The exception below complains that the response object is null. This object is mapped from the response

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

And the exception

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)

Turns out this is happening because of how I setup my 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);

If I remove the RequestFactory and Interceptor the tests would pass. This is an issue with the unit tests only. The actual code runs in production without a hitch

like image 862
Maro Avatar asked Oct 19 '22 00:10

Maro


1 Answers

You need to enable repeated reads of response body, so call bufferContent() when create MockRestServiceServer like this:

        mockServer = MockRestServiceServer
            .bindTo(restTemplate)
            .ignoreExpectOrder(true)
            .bufferContent()
            .build();
like image 180
Eyad Eyadian Avatar answered Nov 15 '22 10:11

Eyad Eyadian