Document About Testing

Our team wants to write ui tests(jmix screen) and unit tests.

Will the jmix document be added in the future about Unit Test and Web Integration Tests

like: https://doc.cuba-platform.com/manual-7.2/testing.html

By the way, Is there any estimate when a more complete document will be released?

2 Likes

We are constantly working on the documentation and updating it as soon as new sections are ready, usually each week.

Until there is something about testing in the docs, I will explain how to write integration tests for business logic and UI with examples.

Firstly, a Jmix application is quite a standard Spring Boot app, so you can write tests running in the Spring container using @SpringBootTest. Such a test is generated in any new project. Below is an example demonstrating how to write tests working with your entities or services:

@SpringBootTest
class UiTestDemoApplicationTests {

	@Autowired
	SystemAuthenticator authenticator;

	@Autowired
	DataManager dataManager;

	@BeforeEach
	void setUp() {
		authenticator.begin();
	}

	@AfterEach
	void tearDown() {
		authenticator.end();
	}

	@Test
	void adminUserExists() {
		Optional<User> adminOpt = dataManager.load(User.class)
				.query("e.username = ?1", "admin")
				.optional();
		assertTrue(adminOpt.isPresent());
	}
}

The only specific to Jmix here is SystemAuthenticator - it provides a current authentication to your code, otherwise the framework access control won’t allow you to work with data.

To see more helpful output in the log, you can add the following application properties:

logging.level.io.jmix = debug
logging.level.eclipselink.logging.sql = debug

Testing UI screens is more complicated, as it requires the setup of the presentation layer. The jmix-ui-test-assist module simplifies the task and provides the infrastructure for writing UI tests with the Spock framework. Below is an example of using it for testing the UserBrowse screen of a new project.

In build.gradle, add groovy plugin and test dependency to jmix-ui-test-assist:

plugins {
    id 'io.jmix' version '1.0.0'
    id 'java'
    id 'groovy'
}
// ...
dependencies {
    // ...
    testImplementation 'io.jmix.ui:jmix-ui-test-assist'
}

Add the src/test/groovy source root and the test class inside:

@ContextConfiguration(classes = UiTestDemoApplication)
class UserBrowseTest extends ScreenSpecification {

    def "admin user is shown"() {
        def mainScreen = screens.create(MainScreen, OpenMode.ROOT)
        screens.show(mainScreen)

        def userBrowseScreen = screens.create(UserBrowse)
        screens.show(userBrowseScreen)

        when:
        CollectionContainer<User> usersDc = userBrowseScreen.screenData.getContainer('usersDc')
        Table<User> usersTable = userBrowseScreen.window.getComponent('usersTable')

        then:
        usersDc.items.find(user -> user.username == 'admin') != null
        usersTable.getItems().items.find(user -> user.username == 'admin') != null
    }
}

ScreenSpecification base class is provided by the jmix-ui-test-assist module, it’s a subclass of Spock’s Specification. The @ContextConfiguration annotation specifies the main application class.

At the moment, the authentication in the UI test is implemented in the base test class and uses the admin user with admin password. We’ll probably change this in the recent update, so the authentication will be done by SystemAuthenticator and not be bound to admin credentials.

See the entire example project attached:
ui-test-demo.zip (81.5 KB)

Regards,
Konstantin

3 Likes