'Mocking JAX RS Client response

I'm learning how to unit test. I have a method that is using a jax-rs client to call external service. I'm trying to mock a response received from jax-rs client but I'm getting NullPointerException. Could someone please help with the issue.

My business class with the method I'm trying to unit test.

@Stateless
public class SinglePaymentManager {

    private JAXRSClientWrapper httpClient;
    private Client client;
    public SinglePaymentManager() {
    }

    @Inject
    public SinglePaymentManager(JAXRSClientWrapper httpClient) {
        this.httpClient = httpClient;
        this.client = httpClient.createClient(true);
    }

    public BankResponse initiatePayment(PaymentContext paymentContext) {
        // Extracting data from parameters

        Invocation.Builder invocationBuilder = client.target(uri)
            .request()
            // more headers
            .header("content-type", MediaType.APPLICATION_JSON);

        String responseAsString;
        Response.Status responseStatus;
        try (Response response = invocationBuilder.post(Entity.json(payload))) {
            responseAsString = response.readEntity(String.class);
            responseStatus = response.getStatusInfo().toEnum();
        }

        return new BankResponse(responseStatus, responseAsString);
    }

My test class


public class SinglePaymentManagerTest {

    private final String responseAsString = //expected response;
    private final Response.Status responseStatus = Response.Status.CREATED;

    private final JAXRSClientWrapper mockHTTPClient = mock(JAXRSClientWrapper.class);
    private final Client mockClient = mock(Client.class);
    private final WebTarget mockWebTarget = mock(WebTarget.class);
    private final Invocation.Builder mockBuilder = mock(Invocation.Builder.class);
    private final Response mockResponse = mock(Response.class);

    @Test
    public void testSomeMethod() {

        when(mockHTTPClient.createClient(true)).thenReturn(mockClient);
        SinglePaymentManager manager = new SinglePaymentManager(mockHTTPClient);

        when(mockClient.target(anyString())).thenReturn(mockWebTarget);

        when(mockWebTarget.request()).thenReturn(mockBuilder);

        when(mockBuilder.header("accept", MediaType.APPLICATION_JSON)).thenReturn(mockBuilder);

        when(mockBuilder.post(any(Entity.class))).thenReturn(mockResponse);
        when(mockResponse.readEntity(any(Class.class))).thenReturn(responseAsString);
        when(mockResponse.getStatusInfo().toEnum()).thenReturn(responseStatus); // this is where I get NullPointer
       
        //requestData and bank object creation is omitted for brevity        
        PaymentContext context = new PaymentContext(requestData, bank);

        BankResponse response = new BankResponse(responseStatus, responseAsString);

        BankResponse serviceResponse = manager.initiatePayment(context);

        Assertions.assertEquals(response, serviceResponse);

    }
}

Dependecties from POM.xml

<dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.6.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-core</artifactId>
            <version>4.5.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>4.5.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>8.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>


Solution 1:[1]

I managed to find the solution.

It was the line when(mockResponse.getStatusInfo()).thenReturn(Response.Status.CREATED);

I just removed getStatusInfo() and NullPointer disappeared.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marko