SpringBoot與單元測試JUnit的結合


有些人認為,寫單元測試就是在浪費時間 ,寫完代碼,依然還是能夠進行測試的。但是,還是建議寫單元測試的,可以讓你的條理更加清晰,而且當某個功能出現問題時,可能通過單元測試很容易的定位和解決問題。本文主要總結下在Spring及SpringBoot項目中,使用單元測試時的方法。將JUnit4和JUnit5對比着來寫,因為我發現我身邊的同事經常搞不明白要怎么用。

Juint版本說明

這里主要說明下它們在Maven下的依賴包

Junit4

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <!--請注意這個scope的用法--> <scope>test</scope> </dependency> 

Junit5

<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.6.2</version> <!--請注意這個scope的用法--> <scope>test</scope> </dependency> 

在上邊的依賴中,兩個依賴分別寫了scope屬性,這里做一個講解:

一個標准的maven項目結構如下圖:

寫Java代碼的地方有兩個src/main/java和src/test/java。如果我們不在上邊依賴中添加scope為test屬性,就可以在這兩個地方任意地方寫@Test測試方法,但是,如果添加了這個屬性,就只能在src/test/java下寫單元測試代碼,這個就是maven所謂的test域。從上圖也可以看出,test域可以有自己的配置文件,如果沒有的話就會去加載main下的resources的配置文件,如果有,則以自己的為優先。

Junit5常見注解及其用法

不管使用哪一種方法,一個標准的單元測試方法如下:

public class TestDemo { @Test void fun1(){ System.out.println("歡迎關注我的微信公眾號——小魚與Java"); } } 

但是對於Junit4而言,所有的測試方法應當是public聲明的,而Junit5不用。只不過不同的版本,這個@Test的類是不同的:

Junit4: org.junit.Test
Junit5: org.junit.jupiter.api.Test

相比Junit4而言,5添加了新的一些注解,但是常用的注解還是相同的,主要有以下:

注解 Description
@Test 寫在一個測試類中的測試方法中的元注解,也就是說,在每一個單元測試方法上都應加上它才會生效
@ParameterizedTest 參數化測試,就是在你的測試方法執行時,自動添加一些參數
@RepeatedTest 重復此測試方法
@TestFactory 動態測試的工廠方法
@TestTemplate 測試模板
@TestMethodOrder 測試方法的執行順序,默認是按照代碼的前后順序執行的
@DisplayName 自定義測試方法的名稱顯示
@DisplayNameGeneration 自定義名稱生成器
@BeforeEach 在Junit4中,這個注解叫@Before。就是會在每一個測試方法執行前都會執行的方法,包括@Test@RepeatedTest@ParameterizedTest,或者 @TestFactory注解的方法
@AfterEach 和上邊很相似,在Junit4中,這個注解叫@After。就是會在每一個測試方法執行之后都會執行的方法,包括@Test@RepeatedTest@ParameterizedTest, 或者@TestFactory注解的方法.
@BeforeAll 在當前測試類中的方法執行前執行,只會執行一次,在Junit4中是@BeforeClass
@AfterAll 在當前測試類中的所有測試方法執行完之后執行,只會執行一次,在Junit4中是@AfterClass
@Nested 表示一個非靜態的測試方法,也就是說@BeforeAll和@AfterAll對此方法無效,如果單純地執行此方法,並不會觸發這個類中的@BeforeAll和@AfterAll方法
@Tag 自定義tag,就是可以自定義一個屬於自己的@Test一樣功能的注解
@Disabled 表明此方法不可用,並不會執行,在JUnit4中的@Ignore
@Timeout 設定方法執行的超時時間,如果超過,就會拋出異常

以上是在JUnit5中最常用的注解,可以自己挨個試下,一下子就會明白其用法。關注我,后續為您遞上具體用法。

在普通Maven項目中使用Junit

引入相關依賴后,然后在對應的位置進行測試就可以了,這里不做演示,可以自行下載代碼查看

在Spring項目中使用Junit

這里的Spring和SpringBoot項目也是基於Maven構建的,和普通Maven項目的最大區別就是加載Sprign容器而已,一般來說,使用Spring提供的上下文ApplicationContext就可以從配置文件件或者配置類加載Spring容器。如下代碼:

/** * 使用普通的Spring上下文來加載Spring容器 * * @auther 微信公眾號:小魚與Java * 2020/4/23 */ public class MyMain { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml"); Teacher teacher = (Teacher) ctx.getBean("teacher"); System.out.println(teacher.getName()); } } 

但是,我們可以通過引入Spring相關的test依賴來讓其自動加載Spring上下文,這樣我們就能利用如@Autowired這樣的自動注入方式來獲取bean了

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.5.RELEASE</version> </dependency> 

但是這里對於JUnit4和JUnit5寫測試方法時有一點兒不同之處,如下:

Junit4

@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"classpath:application.xml"}) public class TestDemo { @Resource private Teacher teacher; @Test public void fun(){ System.out.println(teacher.getName()); } } 

Junit5

@SpringJUnitConfig //指定配置文件路徑,會先從test域中找 @ContextConfiguration("classpath:application.xml") public class SpringTest { @Resource private Teacher teacher; @Test void fun(){ System.out.println(teacher.getName()); } } 

它們都加了額外的注解來加載Spring上下文的

在SpringBoot項目中使用Junit

在SpringBoot中,為我們提供了一個SpringBootTest的注解來加載Spring容器。在SpringBoot2.2.0以前是JUnit4,在SpringBoot之后是JUnit5。但是我建議最應該使用JUnit5。

Junit4

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.1.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.1.6.RELEASE</version> <!--表示只能在maven的測試域中使用--> <scope>test</scope> </dependency> </dependencies> 
@SpringBootTest @RunWith(SpringJUnit4ClassRunner.class) public class TestDemo { @Resource private Student student; @Test public void fun1(){ System.out.println(student.getName()); } } 

Junit5

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.2.6.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.2.6.RELEASE</version> <!--表示只能在maven的測試域中使用--> <scope>test</scope> <exclusions> <!--這個是JUnit5中為了支持使用JUint4所做的一個過度 也就是說,你只需要在你的JUnit4舊項目中添加這個依賴, 就能完美過渡,而不用修改之前代碼 這里用不到,自然也就排除了。當然,這里,它無關緊要 --> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> 
@SpringBootTest //它默認會為我們加載Spring容器, public class TestDemo { @Resource private Student student; @Test void fun1(){ System.out.println(student.getName()); } } 

為什么在SpringBoot中不用指定Spring容器的配置文件?

​ 其實他是會自動加載類路徑下的那個SpringBoot的啟動類的,就算指定配置文件,也是指定那個啟動類為配置類。如果你寫的包結構不符合它的要求,就需要自己使用@ContextConfiguration注解來指定Spring的配置類了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM