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)建測試類
@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)建測試類
@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中,可以用復合注解 @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());
}
}到此這篇關于Spring整合Junit詳解的文章就介紹到這了,更多相關Spring Junit內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
關于struts返回對象json格式數(shù)據(jù)的方法
以下為大家介紹,關于struts返回對象json格式數(shù)據(jù)的方法,希望對有需要的朋友有所幫助。2013-04-04
Java實現(xiàn)基于UDP協(xié)議的網(wǎng)絡通信UDP編程
在Java中使用UDP編程,仍然需要使用Socket,因為應用程序在使用UDP時必須指定網(wǎng)絡接口(IP地址)和端口號。注意:UDP端口和TCP端口雖然都使用0~65535,但他們是兩套獨立的端口,即一個應用程序用TCP占用了端口1234,不影響另一個應用程序用UDP占用端口12342023-04-04
一文詳解如何在SpringMVC的視圖中渲染模型數(shù)據(jù)
SpringMVC是一個基于Spring框架的Web框架,它提供了一種方便的方式來處理 HTTP 請求和響應,在SpringMVC中,視圖是用來渲染模型數(shù)據(jù)的組件,它們負責將模型數(shù)據(jù)轉換為HTML、JSON、XML等格式的響應,在本文中,我們將討論如何在SpringMVC中的視圖中渲染模型數(shù)據(jù)2023-07-07
詳解SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎
本篇文章主要介紹了SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
MyBatis實現(xiàn)多表聯(lián)查的詳細代碼
這篇文章主要介紹了MyBatis如何實現(xiàn)多表聯(lián)查,通過實例代碼給大家介紹使用映射配置文件實現(xiàn)多表聯(lián)查,使用注解的方式實現(xiàn)多表聯(lián)查,需要的朋友可以參考下2022-08-08

