我為 Spring Boot 編寫了測試,并且有 2 個類正在測試 API 和存儲庫。下面提供了骨架,@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)@AutoConfigureMockMvcpublic 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}存儲庫測試類,@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 }如何使用單個類來運行它們?例如,如果課程是AppointmentApplicationTests,@RunWith(SpringRunner.class)@SpringBootTestpublic class AppointmentApplicationTests { @Test public void contextLoads() { }}配置此類以調用 API 和 repo 類中的所有測試的最佳方法是什么?
1 回答

哈士奇WWW
TA貢獻1799條經驗 獲得超6個贊
我認為最簡單的方法是創建一個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 {
}
6月5日
@RunWith(JUnitPlatform.class)
@SelectClasses({some.package.AppointmentRepositoryTest,
some.package.AppointmentAPITest.class})
public class JUnit5TestSuiteExample {
}
然而,這并不總是最好的方法。還考慮熟悉如何為 maven toe 創建測試配置文件執行一堆測試或包。
添加回答
舉報
0/150
提交
取消