嘲笑JAX RS客户端响应
我正在学习如何单位测试。我有一种使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法找到了解决方案。
这是行
时(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.