I am new to JUnit testing in general and to Quarkus testing specifically.
We have a Quarkus – Vertx application and we want to test some functionality from different services.
For that i created a test class with the following annotations:
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@QuarkusTest
public class MyTest
inside the class i am injecting service, vertx and create some static fields
@Inject
MyService myService
@Inject
Vertx vertx
private static Map<String, String> myMap = new HashMap<>();
private static List<MyObject> myList = new ArrayList<>();
private static final CompletableFuture<Void> setupFuture = new CompletableFuture<>();
in setUp method, annotated with @BeforeAll i am wating to consume some event (which would mean that all necessary data is available now) and then I am filling a map and a list with data from myService.
@BeforeAll
public void setUp() {
vertx.eventBus().consumer("myEvent", message -> {
myList = myService.getMyData();
myMap = myService.getDataForMyMap();
setupFuture.complete(null);
})
setupFuture.get(10, TimeUnit.SECONDS);
}
I have a test method that should execute test for each object to check the necessary functionality. And i have a getMyObjects method that supplies objects to the test.
@ParameterizedTest
@DisplayName("test provider response")
@MethodSource("getMyObjects")
void myTestMethod(MyObject object) {
// testing logic
}
private Stream<MyObject> getMyObjects() {
return myMap.keySet().stream();
}
The problem is that in getMyObjects() method the map is empty, even though i get there after setUp() execution is finished (and i am waiting for the data to be filled). Also when i get to getMyObjects() method both vertx and myService are null, like this method is out of context somehow. However in setUp method both vertx and myService are injected properly and in myTestMethod the map and the list are filled with data.
I tried to use Thread.sleep() in getMyObjects to see if its just not enough time.
Also tried to comment out setUp() method but i still see that the myService and vertx fields are not injected in getMyObjects.
I am using quarkus-junit5-mockito and vertx-junit5 dependencies.
Why is this happening and how this could be fixed?
D_Sunny is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.