I have a method which starts generating some report in another thread while the main thread returns a link to a google folder where created report should be uploaded. After report was created, it inserts a record with some information in the database.
public Link startReportCreation(ReportRequest reportRequest) {
Link link = /** some logic **/;
taskExecutor.execute(() -> {
/** report creation logic **/
});
return link;
So what is the best way to test the info of the inserted record without modifying the startReportCreation
method?
Update
Currently I have while (taskExecutor.getActiveCount() != 0);
kind of lock, which indicates that taskExecutor
ended its execution and I can check the database. But I think there is a better approach.
Btw sry for misleading, that is not a unit test, it's just an executor to manually test methods on real data.
Solved
So I've managed to refactor my startReportCreation
method and instead of simple taskExecutor
I used CompletableFuture
, so now it's looks like this:
public CompletableFuture<Link> startReportCreation(ReportRequest reportRequest) {
/** some logic **/
return CompletableFuture.supplyAsync(() -> {
Link link = /** some logic **/;
return link;
}, taskExecutor);
}
So in production code I use CompletableFuture.getNow()
method and in tests I use CompletableFuture.get()
to wait for the report creation.