欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Spring6整合JUnit的詳細(xì)步驟

 更新時(shí)間:2023年05月09日 08:57:33   作者:@每天都要敲代碼  
這篇文章主要介紹了Spring6整合JUnit的詳細(xì)步驟,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

一:Spring6整合JUnit

1. Spring對(duì)JUnit4的支持

準(zhǔn)備工作:pom.xml

注:以前是直接使用單元測(cè)試Junit,現(xiàn)在使用Spring對(duì)Junit的整合!

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bjpowernode</groupId>
    <artifactId>spring6-014-junit</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <!--倉(cāng)庫(kù)-->
    <repositories>
        <!--spring里程碑版本的倉(cāng)庫(kù)-->
        <repository>
            <id>repository.spring.milestone</id>
            <name>Spring Milestone Repository</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <dependencies>
        <!--spring context依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>6.0.0-M2</version>
        </dependency>
        <!--spring對(duì)junit的支持相關(guān)依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <!--這個(gè)版本spring6,既支持Junit4又支持Junit5依賴-->
            <version>6.0.0-M2</version>
        </dependency>
        <!--junit4依賴-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
</project>

聲明Bean

package com.bjpowernode.spring6.bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component("user") // 納入spring管理
public class User {
    @Value("張三") // 通過注解的方式進(jìn)行賦值
    private String name;
    public User(String name) {
        this.name = name;
    }
    public User() {
    }
    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

spring.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    <!--掃描組件-->
    <context:component-scan base-package="com.bjpowernode.spring6.bean"/>
</beans>

單元測(cè)試:

①以前的寫法

package com.bjpowernode.spring6.test;
import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringJunit4Test {
    @Test
    public void testUser1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        User user = applicationContext.getBean("user", User.class);
        System.out.println(user.getName());
    }
}

②使用Spring對(duì)Junit4的支持寫法

(1)使用兩個(gè)注解:

①@RunWith(SpringJUnit4ClassRunner.class),這個(gè)注解是junit里面的;

②@ContextConfiguration("classpath:spring.xml"),這個(gè)注解時(shí)Spring框架里面的;

使用這兩個(gè)注解就相當(dāng)于new ClassPathXmlApplicationContext("spring.xml");

(2)并且對(duì)于applicationContext.getBean("user", User.class);這段代碼,我們可以先定義一個(gè)User屬性,例如:private User user,然后使用@Autowired注解一注入即可

(3)所以我們以后在編寫測(cè)試代碼,如下:

package com.bjpowernode.spring6.test;
import com.bjpowernode.spring6.bean.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJunit4Test {
    @Autowired
    private User user;
    @Test
    public void testUser2(){
        System.out.println(user.getName());
    }
}

執(zhí)行結(jié)果

在JUnit4當(dāng)中,Spring提供的方便主要是這幾個(gè)注解:

①@RunWith(SpringJUnit4ClassRunner.class)
②@ContextConfiguration("classpath:spring.xml")

單元測(cè)試類上使用這兩個(gè)注解之后,在單元測(cè)試類中的屬性上可以使用@Autowired,比較方便!

2. Spring對(duì)JUnit5的支持

引入JUnit5的依賴,Spring對(duì)JUnit支持的依賴還是:spring-test,如下:

<!--junit5依賴-->
<dependency>
     <groupId>org.junit.jupiter</groupId>
     <artifactId>junit-jupiter</artifactId>
     <version>5.9.0</version>
     <scope>test</scope>
</dependency>

單元測(cè)試類

package com.bjpowernode.spring6.test;
import com.bjpowernode.spring6.bean.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringJunit5Test {
    @Autowired
    private User uer;
    @Test
    public void testUser(){
        System.out.println(uer.getName());
    }
}

在JUnit5當(dāng)中,可以使用Spring提供的以下兩個(gè)注解,標(biāo)注到單元測(cè)試類上,這樣在類當(dāng)中就可以使用@Autowired注解了。

①@ExtendWith(SpringExtension.class)

②@ContextConfiguration("classpath:spring.xml")

總結(jié):對(duì)于Spring對(duì)Junit4和Junit5的支持當(dāng)中,代碼主要有兩點(diǎn)不同:

第一點(diǎn):引入的注解不同

對(duì)于Junit4引入的一個(gè)注解是@RunWith(SpringJUnit4ClassRunner.class)

對(duì)于Junit5引入的一個(gè)注解時(shí)@ExtendWith(SpringExtension.class)

第二點(diǎn):使用@Test注解的時(shí)導(dǎo)入的包不同

對(duì)于Junit4導(dǎo)入的包時(shí)org.junit.Test

對(duì)于Junit5導(dǎo)入的包時(shí)org.junit.jupiter.api.Test

到此這篇關(guān)于Spring6整合JUnit的文章就介紹到這了,更多相關(guān)Spring6整合JUnit內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot獲取客戶端的IP地址的實(shí)現(xiàn)示例

    SpringBoot獲取客戶端的IP地址的實(shí)現(xiàn)示例

    在Web應(yīng)用程序中,獲取客戶端的IP地址是一項(xiàng)非常常見的需求,本文主要介紹了SpringBoot獲取客戶端的IP地址的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • Java數(shù)組的初始化方法詳解

    Java數(shù)組的初始化方法詳解

    在Java程序開發(fā)中,數(shù)組是一個(gè)非常常用的數(shù)據(jù)類型,數(shù)組的初始化是使用數(shù)組來存儲(chǔ)和處理數(shù)據(jù)的關(guān)鍵步驟之一,但是,關(guān)于Java數(shù)組的初始化,經(jīng)常會(huì)讓人感到迷惑,本文將詳細(xì)介紹Java數(shù)組的初始化方法,幫助讀者從此告別關(guān)于Java數(shù)組初始化的困惑
    2023-11-11
  • @RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)

    @RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)

    這篇文章主要介紹了@RequestMapping 如何使用@PathVariable 從URI中獲取參數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Mybatis 動(dòng)態(tài)表名+Map參數(shù)傳遞+批量操作詳解

    Mybatis 動(dòng)態(tài)表名+Map參數(shù)傳遞+批量操作詳解

    這篇文章主要介紹了Mybatis 動(dòng)態(tài)表名+Map參數(shù)傳遞+批量操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-12-12
  • java后臺(tái)防止表單重復(fù)提交方法詳解

    java后臺(tái)防止表單重復(fù)提交方法詳解

    這篇文章主要介紹了后臺(tái)防止表單重復(fù)提交,利用Session防止表單重復(fù)提交,判斷請(qǐng)求url和數(shù)據(jù)是否和上一次相同,利用Spring AOP和redis的鎖需要的朋友可以參考下
    2022-12-12
  • java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式

    java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式

    這篇文章主要介紹了java讀取配置文件(properties)的時(shí)候,unicode碼轉(zhuǎn)utf-8方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java SPI機(jī)制原理及代碼實(shí)例

    Java SPI機(jī)制原理及代碼實(shí)例

    這篇文章主要介紹了Java SPI機(jī)制原理及代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • SpringSession 請(qǐng)求與響應(yīng)重寫的實(shí)現(xiàn)

    SpringSession 請(qǐng)求與響應(yīng)重寫的實(shí)現(xiàn)

    這篇文章主要介紹了SpringSession 請(qǐng)求與響應(yīng)重寫的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-11-11
  • 淺談java常用的幾種線程池比較

    淺談java常用的幾種線程池比較

    下面小編就為大家?guī)硪黄獪\談java常用的幾種線程池比較。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Spring Boot CLI安裝教程

    Spring Boot CLI安裝教程

    Spring Boot是一個(gè)命令行工具,用于使用Spring進(jìn)行快速原型搭建。本文重點(diǎn)給大家介紹Spring Boot CLI安裝教程,感興趣的朋友參考下吧
    2017-08-08

最新評(píng)論