亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring6整合JUnit的詳細步驟

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

一:Spring6整合JUnit

1. Spring對JUnit4的支持

準備工作:pom.xml

注:以前是直接使用單元測試Junit,現(xiàn)在使用Spring對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>
    <!--倉庫-->
    <repositories>
        <!--spring里程碑版本的倉庫-->
        <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對junit的支持相關(guān)依賴-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <!--這個版本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("張三") // 通過注解的方式進行賦值
    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>

單元測試:

①以前的寫法

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對Junit4的支持寫法

(1)使用兩個注解:

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

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

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

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

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

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提供的方便主要是這幾個注解:

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

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

2. Spring對JUnit5的支持

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

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

單元測試類

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提供的以下兩個注解,標(biāo)注到單元測試類上,這樣在類當(dāng)中就可以使用@Autowired注解了。

①@ExtendWith(SpringExtension.class)

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

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

第一點:引入的注解不同

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

對于Junit5引入的一個注解時@ExtendWith(SpringExtension.class)

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

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

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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

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

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

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

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

    Java SPI機制原理及代碼實例

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

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

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

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

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

    Spring Boot CLI安裝教程

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

最新評論