亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring boot 測試 API 和存儲庫的多個類

Spring boot 測試 API 和存儲庫的多個類

catspeake 2022-06-15 09:23:52
我為 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 創建測試配置文件執行一堆測試或包。


查看完整回答
反對 回復 2022-06-15
  • 1 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號