I have a stateful bean and some different test cases under @QuarkusTest
for it. But each test case requires clean bean state for correct execution (tests should not depend on each other).
Currently bean preserves its states across the tests, so test2
fails, which doesn’t seems right:
@ApplicationScoped
public class ExampleService {
private final Map<String, Long> state = new HashMap<>();
public Map<String, Long> getState() {
return state;
}
public void put(String key, Long value) {
state.put(key, value);
}
}
@QuarkusTest
class ExampleServiceTest {
@Inject
ExampleService service;
@Test
void test() {
service.put("", 1L);
var state = service.getState();
Assertions.assertEquals(1, state.size());
}
@Test
void test2() {
var state = service.getState();
Assertions.assertEquals(0, state.size());
}
}
How could I reset injected bean state in tests?
P.S. There was a similar question, but it doesn’t seem to have any correct answers How to keep a statefull bean clean or reset during a @QuarkusTest?
Quarkus starts the CDI container for the scope of the test class which is why you keep the state of the application scoped bean between test methods.
To circumvent this, you can for instance:
- Create a
reset()
method on the bean and invoke it in before each callback- You can do this with a subclass of that bean to keep the production code clean if that’s a concern
- In CDI, to make a subclass “replace” the original bean, mark it as
@Alternative @Priority(x)
, see CDI documentation
- Use CDI API
Instance<X>#destroy()
to force-destroy the contextual instance- Any future invocations to the injected normal-scoped bean will result in creation of a new bean instance
- Note that this will not work for non-normal scoped beans, i.e.
@Dependent
ones or@Singleton
!
The former is pretty obvious, the latter should be achievable as follows:
@QuarkusTest
class ExampleServiceTest {
ExampleService service;
@Inject
Instance<ExampleService> serviceInstance;
@BeforeEach
void reset() {
// initialize once; this injects the client proxy for the bean
if (service == null) {
service = serviceInstance.get();
}
// before every test method, force destroy the bean instance
serviceInstance.destroy(service)
}
// Actual tests...
}
I use normally @BeforeEach
to clear the state:
QuarkusTest
class ExampleServiceTest {
@Inject
ExampleService service;
@BeforeEach
void reset() {
// reset state here.
}
// Remaining tests
}
2