嘲笑JAX RS客户端响应

发布于 2025-01-23 06:30:19 字数 4569 浏览 0 评论 0原文

我正在学习如何单位测试。我有一种使用JAX-RS客户端来调用外部服务的方法。我正在尝试嘲笑从JAX-RS客户端收到的响应,但我得到了NullPoInterException。有人可以帮助解决这个问题。

我的商务舱正在使用我试图进行单元测试的方法。

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

我的测试类


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

    }
}

依赖性来自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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

也只是曾经 2025-01-30 06:30:19

我设法找到了解决方案。

这是行时(opconsponse.getStatusInfo())

I managed to find the solution.

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

I just removed getStatusInfo() and NullPointer disappeared.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文