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.2'
    id 'java'
    id 'groovy'
}

apply plugin: 'org.springframework.boot'

jmix {
    bomVersion = '1.0.1'
}

// ...
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 UiTestAssistSpecification {

    @Autowired
    SystemAuthenticator authenticator

    @Override
    protected void setupAuthentication() {
        authenticator.begin()
    }

    @Override
    protected void cleanupAuthentication() {
        authenticator.end()
    }

    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
    }
}

UiTestAssistSpecification 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.

In your test class, you should implement the setupAuthentication() and cleanupAuthentication() methods similarly to how you do it in the first example of testing business logic.

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

Regards,
Konstantin

UPDATED 2021-08-06 to conform changes in Jmix 1.0.1, particularly changed the base class for UI test, added setupAuthentication() and cleanupAuthentication() methods.

4 Likes

Please notice that I updated my answer above to conform changes in Jmix 1.0.1.
Changed the base class, added setupAuthentication() and cleanupAuthentication() methods in UI test.

Given the importante of Testing, maybe this thread can be pinned until the documentation includes this?

Thx @krivopustov for the examples, they’ll come in handy! It also looks like we won’t need addons for UI testing like in CUBA!

@marc & @a616101 You probably saw https://github.com/Haulmont/jmix-masquerade#readme is now available
Thanks JMIX team!

3 Likes