Getting Started with Testing

By default, every Jmix project already contains the directory structure for automated tests and two test classes in the src/test/java/<base-package>/user directory:

  1. UserTest.java - an integration test for the User entity.

  2. UserUiTest.java - a UI integration test for the user management views.

The UserTest saves a User entity through the Jmix DataManager API. Then the test verifies that when loading the user by its username, the user is found and matches the originally saved user.

UserTest.java
package com.company.demo.user;

import com.company.demo.entity.User;
import com.company.demo.test_support.AuthenticatedAsAdmin;
import io.jmix.core.DataManager;
import io.jmix.core.security.UserRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Sample integration test for the User entity.
 */
@SpringBootTest (1)
@ExtendWith(AuthenticatedAsAdmin.class) (2)
public class UserTest {

    @Autowired (3)
    DataManager dataManager;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    UserRepository userRepository;

    User savedUser;

    @Test (4)
    void test_saveAndLoad() {
        // Create and save a new User
        User user = dataManager.create(User.class);
        user.setUsername("test-user-" + System.currentTimeMillis());
        user.setPassword(passwordEncoder.encode("test-passwd"));
        savedUser = dataManager.save(user);

        // Check the new user can be loaded
        User loadedUser = dataManager.load(User.class).id(user.getId()).one();
        assertThat(loadedUser).isEqualTo(user);

        // Check the new user is available through UserRepository
        UserDetails userDetails = userRepository.loadUserByUsername(user.getUsername());
        assertThat(userDetails).isEqualTo(user); (5)
    }

    @AfterEach (6)
    void tearDown() {
        if (savedUser != null)
            dataManager.remove(savedUser);
    }
}
1 The @SpringBootTest annotation indicates that the Spring context is started during the test execution.
2 AuthenticatedAsAdmin allows the test to interact with the system and the database as the admin user.
3 Dependencies are injected into the test code using the @Autowired Spring dependency injection annotation.
4 Each test case is marked with the JUnit’s @Test annotation.
5 To perform verifications, the assertion library AssertJ is automatically available in Jmix projects.
6 Operations like database cleanup are performed in tear down methods annotated with JUnit’s @AfterEach annotation.

The test itself is a regular Spring Boot integration test. It starts the complete Spring context and interacts with a real database.

By default, the tests use the same database as the application when running locally.

Jmix Studio uses IntelliJ IDEA’s capabilities to execute tests. When you open the UserTest.java class in Jmix Studio, you can run the test cases by clicking the play button on the line of the test class. The IDE will show the result of the test execution and if the test was successful or not.

See more information on how to run tests in the IntelliJ documentation.

The following sections will explain different types of tests in more detail.