最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

一:Spring6整合JUnit

1. Spring對JUnit4的支持

準(zhǔn)備工作: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>
            <!--這個(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>

單元測試:

①以前的寫法

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)使用兩個(gè)注解:

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

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

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

(2)并且對于applicationContext.getBean("user", User.class);這段代碼,我們可以先定義一個(gè)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提供的方便主要是這幾個(gè)注解:

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

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

①@ExtendWith(SpringExtension.class)

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

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

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

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

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

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

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

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

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

相關(guān)文章

  • MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)

    MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn)

    本文主要介紹了MyBatis的CRUD中的不同參數(shù)綁定查詢實(shí)現(xiàn),主要包括單個(gè)參數(shù)傳遞綁定,序號參數(shù)傳遞綁定,注解參數(shù)傳遞綁定,pojo(對象)參數(shù)傳遞綁定,map參數(shù)傳遞綁定這幾種類型,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-12-12
  • JavaWeb實(shí)現(xiàn)文件上傳與下載實(shí)例詳解

    JavaWeb實(shí)現(xiàn)文件上傳與下載實(shí)例詳解

    在Web應(yīng)用程序開發(fā)中,文件上傳與下載功能是非常常用的功能,下面通過本文給大家介紹JavaWeb實(shí)現(xiàn)文件上傳與下載實(shí)例詳解,對javaweb文件上傳下載相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧
    2016-02-02
  • 詳解Java 序列化與反序列化(Serialization)

    詳解Java 序列化與反序列化(Serialization)

    這篇文章主要介紹了Java 序列化與反序列化(Serialization),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí) 吧
    2019-03-03
  • Java設(shè)計(jì)模式--適配器模式詳解

    Java設(shè)計(jì)模式--適配器模式詳解

    這篇文章主要介紹了java設(shè)計(jì)模式之適配器模式Adapter的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • mybatis-parameterType傳入map條件方式

    mybatis-parameterType傳入map條件方式

    這篇文章主要介紹了mybatis-parameterType傳入map條件方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java swing中實(shí)現(xiàn)拖拽功能示例

    java swing中實(shí)現(xiàn)拖拽功能示例

    這篇文章主要介紹了java swing中實(shí)現(xiàn)拖拽功能示例,需要的朋友可以參考下
    2014-04-04
  • Java實(shí)現(xiàn)雪花算法的工具類介紹

    Java實(shí)現(xiàn)雪花算法的工具類介紹

    雪花 (SnowFlake )算法是一種分布式唯一ID生成算法,可以生成全局唯一的ID標(biāo)識符,就像自然界中雪花一般沒有相同的雪花,本文和大家分享了一個(gè)雪花算法工具類,需要的可以收藏一下
    2023-05-05
  • springcloud部署提示 找不到url的解決

    springcloud部署提示 找不到url的解決

    這篇文章主要介紹了springcloud部署提示 找不到url的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • Springboot接入MyBatisPlus的實(shí)現(xiàn)

    Springboot接入MyBatisPlus的實(shí)現(xiàn)

    最近web端比較熱門的框架就是SpringBoot和Mybatis-Plus,這里簡單總結(jié)集成用法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-09-09
  • LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情

    LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情

    這篇文章主要介紹了LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情,文章基于Java的相關(guān)資料展開對LeetCode?動態(tài)規(guī)劃的詳細(xì)介紹,需要的小伙伴可以參考一下
    2022-04-04

最新評論

台北县| 铜川市| 都安| 海淀区| 新郑市| 曲沃县| 祁阳县| 谷城县| 江油市| 凤凰县| 晴隆县| 巨鹿县| 福泉市| 十堰市| 阳朔县| 府谷县| 云浮市| 巴林左旗| 镶黄旗| 兴和县| 鸡西市| 镇平县| 龙泉市| 安康市| 开远市| 涞源县| 十堰市| 会同县| 保山市| 柘荣县| 阿勒泰市| 托克托县| 越西县| 聂拉木县| 青冈县| 毕节市| 花垣县| 富顺县| 阜宁县| 尉氏县| 垫江县|