@SpringBootTest 构造函数 运行 多次

@SpringBootTest constructor is running multiple times

我正尝试 运行 在 Spring 引导中测试用例。

:: Spring 启动 :: (v2.3.1.RELEASE)

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@SpringBootTest(
        webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
        classes = com.dineoutsafe.api.dosadmin.DOSAdminAPIApplication.class)
@ActiveProfiles("test")
public class POSTSessionTest {

    public POSTSessionTest() {
        System.out.println("Calling post construct");
    }
    @Test
    public void testOne(){
        assertThat(45,equalTo(30+15));
    }
    @Test
    public void testTwo(){
        assertThat(45,equalTo(30+15));
    }
    @Test
    public void testThree(){
        assertThat(45,equalTo(30+15));
    }
    @Test
    public void testFour(){
        assertThat(45,equalTo(30+15));
    }
    @Test
    public void testFive(){
        assertThat(45,equalTo(30+15));
    }
}

而且我注意到构造函数 运行ning 多次。实际上是 运行ning (@Test -1) 次。 在标准输出中

2020-06-21 16:00:26.668  INFO 93912 --- [         task-1] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-06-21 16:00:26.679  INFO 93912 --- [         task-1] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-06-21 16:00:27.025  INFO 93912 --- [    Test worker] DeferredRepositoryInitializationListener : Spring Data repositories initialized!
2020-06-21 16:00:27.034  INFO 93912 --- [    Test worker] c.d.a.d.i.session.POSTSessionTest        : Started POSTSessionTest in 5.511 seconds (JVM running for 6.414)
Calling post construct
Calling post construct
Calling post construct
Calling post construct

我注意到@PostConstruct 的行为相同。 @SpringBootTest 正常吗?

这是 JUnit5 的默认行为,您可以通过在 class 上注释每个 class 生命周期来更改它:https://junit.org/junit5/docs/5.0.1/api/org/junit/jupiter/api/TestInstance.Lifecycle.html