1

I absolutely need to use non-static methods with annotations @BeforeAll and @AfterAll. But these annotations work on non-static methods only if we have the

@TestInstance(TestInstance.Lifecycle.PER_CLASS)

@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public abstract class ApplicationTests extends ContainerConfig{

    @Autowired
    protected TestRestTemplate testRestTemplate;

    @Autowired
    protected BasicUserRepository basicUserRepository;
    
}

/**
* If you prefer JUnit Jupiter
* performed all testing methods in a single test instance,
* annotate your test class with @testInstance(Lifecycle.PER_CLASS) .
* When using this mode, a new test instance is created
* will be created once for each test class, that is, all nodes of the test class
* of a tree (that is, of this class) they will use the same object, that is,
* an object of this class, and methods, will be called on this object and use the state,
* which this object has.
* Therefore, be careful if your test nodes will change the properties of this
* an object during the processing of its tests.
* In this case, you can use the @BeforeAll and @AfterAll annotations over non-static
* methods
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Sql(scripts = "/sql/sql-data.sql")
class UserControllerTest extends ApplicationTests  {

    private String NAME_USER;

    @BeforeAll
    void setUp() {

        List<BasicUser> userList = super.basicUserRepository.findAll();
        NAME_USER = userList
                .stream()
                .map(BasicUser::getUsername)
                .findFirst()
                .orElse("");

        if (NAME_USER.isEmpty()) {
            throw new RuntimeException("Not Found users.");
        }
    }

    @Test
    void findOne() {

        String uri = "/users/{username}";

        DetailedUserDto userDto =
                super.testRestTemplate
                        .getForObject(uri,
                                DetailedUserDto.class,
                                NAME_USER);

        Long id = userDto.getId();
        String name = userDto.getUsername();
        Set<String> permissions = userDto.getPermissions();
        int sizeEmptyCollection = 0;
        assertTrue(permissions.size() > sizeEmptyCollection);

        int emptyId = 0;
        assertTrue(id > emptyId);
    }

    @AfterAll
    void clearDb() {
        basicUserRepository.deleteAll();
    }
}

But when using @TestInstance(testInstance.Lifecycle.PER_CLASS), autorun of sql scripts does not work, via annotation @Sql.

Why does it occure and how can i correct that ?

skyho
  • 1,438
  • 2
  • 20
  • 47

1 Answers1

0
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class UserControllerTest extends ApplicationTests {

    private boolean isInitDatabase = false;

    private String NAME_USER;

    @BeforeEach
    void setUp() {

        if(isInitDatabase) return;

        isInitDatabase = true;

      .............

}
@ActiveProfiles("test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Sql(scripts={"/sql/sql-data.sql"}, executionPhase = BEFORE_TEST_METHOD)
public abstract class ApplicationTests extends ContainerConfig{

    @Autowired
    protected TestRestTemplate testRestTemplate;

    @Autowired
    protected BasicUserRepository basicUserRepository;

}

skyho
  • 1,438
  • 2
  • 20
  • 47