Reputation: 81
public class A{
@Autowired
B b;
public List<String> method1(){
CompletableFuture<List<String>> completableFuture = b.getCompletableFutureList();
// further processing
// returning a list of strings
return completableFuture.get();
}
}
@RunWith(MockitoJUnitRunner.class)
public class TestA{
private A cut;
@Mock B b;
List<String> prepareStringList(){
List<String> stringList = new ArrayList<>();
stringList.add("Random String");
return stringList;
}
CompletableFuture<List<String>> prepareCompletableStringList(){
CompletableFuture<List<String>> completableFuture = new CompletableFuture<>();
completableFuture.completedFuture(prepareStringList);
return completableFuture;
}
@Test
public void method1Test(){
when(b.getCompletableFutureList()).thenReturn(prepareCompletableStringList);
List<String> list = cut.method1();
//verifications()
}
}
In this class I want to mock method call getCompletableFutureList() and Want to return a custom CompletableFutureObject. Please tell me whether I am completely wrong in my approach to this JUnit. because simply mocking CompletableFuture in my test class neither my test is failing nor it is passing. And If I am wrong What is the correct approach. My Junit also does not terminates.
Upvotes: 2
Views: 712
Reputation: 111
CompletableFuture.completedFuture is a static method that returns a new completed CompletableFuture. What you want instead is CompletableFuture.complete, which will terminate the current CompletableFuture.
Upvotes: 1