有些人認(rèn)為,寫單元測試就是在浪費時間 ,寫完代碼,依然還是能夠進(jìn)行測試的。但是,還是建議寫單元測試的,可以讓你的條理更加清晰,而且當(dāng)某個功能出現(xiàn)問題時,可能通過單元測試很容易的定位和解決問題。
本文主要總結(jié)下在Spring及SpringBoot項目中,使用單元測試時的方法。將JUnit4和JUnit5對比著來寫,因為我發(fā)現(xiàn)我身邊的同事經(jīng)常搞不明白要怎么用。
Juint版本說明
這里主要說明下它們在Maven下的依賴包
Junit4
1
2
3
4
5
6
7
|
< dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >4.13</ version > <!--請注意這個scope的用法--> < scope >test</ scope > </ dependency > |
Junit5
1
2
3
4
5
6
7
|
< dependency > < groupId >org.junit.jupiter</ groupId > < artifactId >junit-jupiter</ artifactId > < version >5.6.2</ version > <!--請注意這個scope的用法--> < scope >test</ scope > </ dependency > |
在上邊的依賴中,兩個依賴分別寫了scope屬性,這里做一個講解:
一個標(biāo)準(zhǔn)的maven項目結(jié)構(gòu)如下圖:
寫Java代碼的地方有兩個src/main/java和src/test/java。
如果我們不在上邊依賴中添加scope為test屬性,就可以在這兩個地方任意地方寫@Test測試方法,但是,如果添加了這個屬性,就只能在src/test/java下寫單元測試代碼,這個就是maven所謂的test域。
從上圖也可以看出,test域可以有自己的配置文件,如果沒有的話就會去加載main下的resources的配置文件,如果有,則以自己的為優(yōu)先。
Junit5常見注解及其用法
不管使用哪一種方法,一個標(biāo)準(zhǔn)的單元測試方法如下:
1
2
3
4
5
6
|
public class TestDemo { @Test void fun1(){ System.out.println( "歡迎關(guān)注我的微信公眾號——小魚與Java" ); } } |
但是對于Junit4而言,所有的測試方法應(yīng)當(dāng)是public聲明的,而Junit5不用。只不過不同的版本,這個@Test的類是不同的:
1
2
|
Junit4: org.junit.Test Junit5: org.junit.jupiter.api.Test |
相比Junit4而言,5添加了新的一些注解,但是常用的注解還是相同的,主要有以下:
注解 | Description |
---|---|
@Test | 寫在一個測試類中的測試方法中的元注解,也就是說,在每一個單元測試方法上都應(yīng)加上它才會生效 |
@ParameterizedTest | 參數(shù)化測試,就是在你的測試方法執(zhí)行時,自動添加一些參數(shù) |
@RepeatedTest | 重復(fù)此測試方法 |
@TestFactory | 動態(tài)測試的工廠方法 |
@TestTemplate | 測試模板 |
@TestMethodOrder | 測試方法的執(zhí)行順序,默認(rèn)是按照代碼的前后順序執(zhí)行的 |
@DisplayName | 自定義測試方法的名稱顯示 |
@DisplayNameGeneration | 自定義名稱生成器 |
@BeforeEach | 在Junit4中,這個注解叫@Before。就是會在每一個測試方法執(zhí)行前都會執(zhí)行的方法,包括@Test, @RepeatedTest, @ParameterizedTest,或者 @TestFactory注解的方法 |
@AfterEach | 和上邊很相似,在Junit4中,這個注解叫@After。就是會在每一個測試方法執(zhí)行之后都會執(zhí)行的方法,包括@Test, @RepeatedTest, @ParameterizedTest, 或者@TestFactory注解的方法. |
@BeforeAll | 在當(dāng)前測試類中的方法執(zhí)行前執(zhí)行,只會執(zhí)行一次,在Junit4中是@BeforeClass |
@AfterAll | 在當(dāng)前測試類中的所有測試方法執(zhí)行完之后執(zhí)行,只會執(zhí)行一次,在Junit4中是@AfterClass |
@Nested | 表示一個非靜態(tài)的測試方法,也就是說@BeforeAll和@AfterAll對此方法無效,如果單純地執(zhí)行此方法,并不會觸發(fā)這個類中的@BeforeAll和@AfterAll方法 |
@Tag | 自定義tag,就是可以自定義一個屬于自己的@Test一樣功能的注解 |
@Disabled | 表明此方法不可用,并不會執(zhí)行,在JUnit4中的@Ignore |
@Timeout | 設(shè)定方法執(zhí)行的超時時間,如果超過,就會拋出異常 |
以上是在JUnit5中最常用的注解,可以自己挨個試下,一下子就會明白其用法。關(guān)注我,后續(xù)為您遞上具體用法。
在普通Maven項目中使用Junit
引入相關(guān)依賴后,然后在對應(yīng)的位置進(jìn)行測試就可以了,這里不做演示,可以自行下載代碼查看
在Spring項目中使用Junit
這里的Spring和SpringBoot項目也是基于Maven構(gòu)建的,和普通Maven項目的最大區(qū)別就是加載Sprign容器而已,一般來說,使用Spring提供的上下文ApplicationContext就可以從配置文件件或者配置類加載Spring容器。如下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
/** * 使用普通的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相關(guān)的test依賴來讓其自動加載Spring上下文,這樣我們就能利用如@Autowired這樣的自動注入方式來獲取bean了
1
2
3
4
5
|
< dependency > < groupId >org.springframework</ groupId > < artifactId >spring-test</ artifactId > < version >5.2.5.RELEASE</ version > </ dependency > |
但是這里對于JUnit4和JUnit5寫測試方法時有一點兒不同之處,如下:
Junit4
1
2
3
4
5
6
7
8
9
10
|
@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
1
2
3
4
5
6
7
8
9
10
11
|
@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。但是我建議最應(yīng)該使用JUnit5。
Junit4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< 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 > |
1
2
3
4
5
6
7
8
9
10
|
@SpringBootTest @RunWith (SpringJUnit4ClassRunner. class ) public class TestDemo { @Resource private Student student; @Test public void fun1(){ System.out.println(student.getName()); } } |
Junit5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
< 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舊項目中添加這個依賴, 就能完美過渡,而不用修改之前代碼 這里用不到,自然也就排除了。當(dāng)然,這里,它無關(guān)緊要 --> < exclusion > < groupId >org.junit.vintage</ groupId > < artifactId >junit-vintage-engine</ artifactId > </ exclusion > </ exclusions > </ dependency > </ dependencies > |
1
2
3
4
5
6
7
8
9
|
@SpringBootTest //它默認(rèn)會為我們加載Spring容器, public class TestDemo { @Resource private Student student; @Test void fun1(){ System.out.println(student.getName()); } } |
為什么在SpringBoot中不用指定Spring容器的配置文件?
?
其實他是會自動加載類路徑下的那個SpringBoot的啟動類的,就算指定配置文件,也是指定那個啟動類為配置類。如果你寫的包結(jié)構(gòu)不符合它的要求,就需要自己使用@ContextConfiguration注解來指定Spring的配置類了
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://www.cnblogs.com/youngdeng/p/12868989.html