我想为以下类创建一个单元测试:
@Service
public class XService{
public String getSomething(String inputField) {
final SomeEntity someEntity1 = new SomeEntity();
final AtomicReference<Throwable> throwable = new AtomicReference<>();
BiConsumer<Response, Throwable> consumer = (response, error) -> {
if (error != null) {
throwable.set(error);
} else {
SomeEntity someEntity2 = response.readEntity(SomeEntity.class);
someEntity1.setSomeField(someEntity2.getSomeField());
//does some stuff with the response
}
};
WebTarget target = client.target("api_url"+inputField);
target.queryParam("param", param)
.request(MediaType.APPLICATION_JSON)
.acceptLanguage(Locale.ENGLISH)
.header("Authorization", token)
.rx()
.get()
.whenCompleteAsync(consumer);
return someEntity1.getSomeField();
}
}
我已经嘲笑了一切,直到.whenCompleteAsync(consumer)
使用这样的东西:
when(mockWebTarget.queryParam(any(),any())).thenReturn(mockWebTarget);
CompletionStageRxInvoker completionStageRxInvoker = mock(CompletionStageRxInvoker.class);
when(mockBuilder.rx()).thenReturn(completionStageRxInvoker);
CompletionStage<Response> mockResp = mock(CompletionStage.class);
when(completionStageRxInvoker.get()).thenReturn(mockResp);
我目前无法更改课程的设计,只能对其进行测试。
如何模拟消费者对象以使代码在 lambda 内运行?这甚至可能吗?