Jmix 2 UI testing - injecting mocks into the view

Is it possible to use mocks when testing Jmix 2 views?

From my understanding, the way that testing should be done is by using the viewNavigators.view(View.class).navigate()

However, is there a way to inject mocks into a custom load delegate for example?

I have tried adding
withAfterNavigationHandler(navEvent → {
navEvent.getView().setDataManager(mockDataManager);
}

but it doesn’t because its applied after the custom loadDelegate is run.

Hello!

Yes, as JavaDoc says, it is applied after all views’ lifecycle events. During navigation, changing the view is not possible, so simply setting mocked instances will not work.

UI tests in Jmix are integration tests. These tests start the Spring context and all Jmix backend components. I would suggest connecting to a test database and filling it with test data. You can use an in-memory database such as hsqldb. This way, you won’t need to mock the DataManager, as it will function as expected. You can use Spring profiles to separate connection details or just specify them in the test environment. In this way, your tests will be closer to the production environment.