Spring5t整合JUnit5的單元測試

Spring 5 全面接納了函數式範例,並支持 JUnit 5 及其新的函數式測試風格。還提供了對 JUnit 4 的向後兼容性,以確保不會破壞舊代碼。

導入依賴:

<dependency>
         <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
       <version>RELEASE</version>
       <scope>test</scope>
</dependency>

或者在程序中提示引入
在這裏插入圖片描述

創建測試類,使用註解完成:

@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemoJunit5 {
    @Autowired
    UserService userService;
    @Test
    public void test(){
        userService.accountMoney();
    }
}

或者使用一個複合註解替代上面兩個註解完成整合

@SpringJUnitConfig(locations = "classpath:applicationContext.xml")
public class TestDemoJunit5 {
    @Autowired
    UserService userService;

    @Test
    public void test(){
        userService.accountMoney();
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章