[英]Junit, How to cover the callback code using Junit?
我必須編寫一個測試用例來覆蓋一個將回調作為參數之一的方法。 它看起來像下面的代碼片段。
JAXBElement<MyCustomObject> obj = null;
try {
obj = (JAXBElement<MyCustomObject>) template.marshall("some string", new SoapActionCallback("some string") {
public void doWithMessage(MyMessageClass message) {
// some logic
});
}
如何覆蓋回調邏輯?
我無法弄清楚如何覆蓋回調邏輯。
我不確定這是最好的模式,但我經常做的是使用CompletableFuture
來處理這些回調。
@Test
public void testMethod() throws Exception {
JAXBElement<MyCustomObject> obj = null;
//Instantiate a future before the callback
final CompletableFuture<MyMessageClass> callbackFuture = new CompletableFuture<>();
try {
obj = (JAXBElement<MyCustomObject>) template.marshall("some string", new SoapActionCallback("some string") {
public void doWithMessage(MyMessageClass message) {
//Complete the future within the callback
callbackFuture.complete(message);
});
}
//Wait until the future has completed in your callback and do the verification afterwards
MyMessageClass message = callbackFuture.get(5, TimeUnit.SECONDS);
assertEquals("your verification", message.toString());
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.