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)文章
關(guān)于struts返回對(duì)象json格式數(shù)據(jù)的方法
以下為大家介紹,關(guān)于struts返回對(duì)象json格式數(shù)據(jù)的方法,希望對(duì)有需要的朋友有所幫助。2013-04-04
Java實(shí)現(xiàn)基于UDP協(xié)議的網(wǎng)絡(luò)通信UDP編程
在Java中使用UDP編程,仍然需要使用Socket,因?yàn)閼?yīng)用程序在使用UDP時(shí)必須指定網(wǎng)絡(luò)接口(IP地址)和端口號(hào)。注意:UDP端口和TCP端口雖然都使用0~65535,但他們是兩套獨(dú)立的端口,即一個(gè)應(yīng)用程序用TCP占用了端口1234,不影響另一個(gè)應(yīng)用程序用UDP占用端口12342023-04-04
一文詳解如何在SpringMVC的視圖中渲染模型數(shù)據(jù)
SpringMVC是一個(gè)基于Spring框架的Web框架,它提供了一種方便的方式來處理 HTTP 請(qǐng)求和響應(yīng),在SpringMVC中,視圖是用來渲染模型數(shù)據(jù)的組件,它們負(fù)責(zé)將模型數(shù)據(jù)轉(zhuǎn)換為HTML、JSON、XML等格式的響應(yīng),在本文中,我們將討論如何在SpringMVC中的視圖中渲染模型數(shù)據(jù)2023-07-07
詳解SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎
本篇文章主要介紹了SpringBoot+Thymeleaf 基于HTML5的現(xiàn)代模板引擎,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
MyBatis實(shí)現(xiàn)多表聯(lián)查的詳細(xì)代碼
這篇文章主要介紹了MyBatis如何實(shí)現(xiàn)多表聯(lián)查,通過實(shí)例代碼給大家介紹使用映射配置文件實(shí)現(xiàn)多表聯(lián)查,使用注解的方式實(shí)現(xiàn)多表聯(lián)查,需要的朋友可以參考下2022-08-08

