Spring 为 API 和存储库启动测试多个 类

Spring boot Test multiple classes for API and repository

我为 Spring 启动编写了测试,并且有 2 个 class 正在测试 API 和存储库。下面提供了骨架,

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
@AutoConfigureMockMvc
public class AppointmentAPITest {


    /*
     * we need a system to simulate this behavior without starting a
     * full HTTP server. MockMvc is the Spring class that does that.
     * */
    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private AppointmentAPI api;


    // now the tests are going on

}

存储库测试class、

@ActiveProfiles("test")
@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class AppointmentRepositoryTest {


    @Autowired
    private TestEntityManager entityManager;

    @Autowired
    private AppointmentRepository repository;


    // write the test here 

}

如何使用单个 class 来 运行 全部?例如,如果 class 将是 AppointmentApplicationTests

@RunWith(SpringRunner.class)
@SpringBootTest
public class AppointmentApplicationTests {

    @Test
    public void contextLoads() {

    }

}

配置此 class 的最佳方法是什么,以便它调用 API 和 repo class 中的所有测试?

我认为最简单的方法是创建一个 Suite 来收集要 运行 的测试,例如:

JUnit 4

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    some.package.AppointmentRepositoryTest,
    some.package.AppointmentApplicationTests
})
public class MySuite {
}

Junit5

@RunWith(JUnitPlatform.class)
@SelectClasses({some.package.AppointmentRepositoryTest,
    some.package.AppointmentAPITest.class})
public class JUnit5TestSuiteExample {
}

然而,这并不总是最好的方法。还要考虑熟悉如何为 maven toe 创建测试配置文件以执行一系列测试或包。