Spring整合Junit詳解
1,整合Junit4
maven引入spring-test 和 junit4
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.22.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>在test目錄下創(chuàng)建測(cè)試類
@RunWith(SpringJUnit4ClassRunner.class) //啟用Junit4
@ContextConfiguration("classpath:META-INF/context-junit.xml") //加載配置文件
public class SpringJunit4 {
@Autowired
private Machine machine;
@Test
public void test1() throws Exception {
System.out.println(machine.getObject());
}
}2,整合Junit5
1,maven引入spring-test 和 junit5
JUnit 5 =JUnit Platform+JUnit Jupiter+JUnit Vintage
<!-- junit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>2,在test目錄下創(chuàng)建測(cè)試類
@ExtendWith(SpringExtension.class) //啟用Junit5
@ContextConfiguration("classpath:META-INF/context-junit.xml") //加載配置文件
public class SpringJunit5 {
@Autowired
private Machine machine;
@Test
public void test1() throws Exception {
System.out.println(machine.getObject());
}
}說明:在spring5中,可以用復(fù)合注解 @SpringJUnitConfig(locations = {"classpath:META-INF/context-junit.xml"}) 代替@ExtendWith(SpringExtension.class) 和@ContextConfiguration("classpath:META-INF/context-junit.xml")
//@ExtendWith(SpringExtension.class) //啟用Junit5
//@ContextConfiguration("classpath:META-INF/context-junit.xml") //加載配置文件
@SpringJUnitConfig(locations = {"classpath:META-INF/context-junit.xml"})
public class SpringJunit5 {
@Autowired
private Machine machine;
@Test
public void test1() throws Exception {
System.out.println(machine.getObject());
}
}到此這篇關(guān)于Spring整合Junit詳解的文章就介紹到這了,更多相關(guān)Spring Junit內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis Plus代碼生成器(時(shí)間管理大師)
這篇文章主要介紹了Mybatis Plus代碼生成器(時(shí)間管理大師)的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
java如何將map數(shù)據(jù)存入到實(shí)體類對(duì)象中
在Java編程中,經(jīng)常需要將Map集合中的數(shù)據(jù)轉(zhuǎn)換為實(shí)體類對(duì)象,這可以通過反射機(jī)制實(shí)現(xiàn),即通過遍歷Map對(duì)象,使用反射根據(jù)鍵名對(duì)應(yīng)實(shí)體類的屬性名,動(dòng)態(tài)調(diào)用setter方法將值設(shè)置到實(shí)體對(duì)象中,這樣的操作使得數(shù)據(jù)從Map結(jié)構(gòu)轉(zhuǎn)移到了具體的JavaBean中,便于后續(xù)的操作和管理2024-09-09
Spring Boot實(shí)現(xiàn)簡(jiǎn)單的定時(shí)任務(wù)
這篇文章主要給大家介紹了關(guān)于利用Spring Boot實(shí)現(xiàn)簡(jiǎn)單的定時(shí)任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
MyBatis配置的應(yīng)用與對(duì)比jdbc的優(yōu)勢(shì)
這篇文章主要介紹了MyBatis配置的使用與相對(duì)于jdbc的優(yōu)勢(shì),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
java?mybatis如何操作postgresql?array數(shù)組類型
這篇文章主要介紹了java?mybatis如何操作postgresql?array數(shù)組類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
Java HashMap在遍歷時(shí)刪除元素的實(shí)現(xiàn)
本文主要介紹了Java HashMap在遍歷時(shí)刪除元素的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實(shí)現(xiàn)
本文主要介紹了Mybatis-plus數(shù)據(jù)權(quán)限D(zhuǎn)ataPermissionInterceptor實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

