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

Spring整合JUnit詳解

 更新時間:2023年04月17日 11:13:46   作者:@每天都要敲代碼  
Spring?是目前主流的?Java?Web?開發(fā)框架,是?Java?世界最為成功的框架。該框架是一個輕量級的開源框架,這篇文章主要介紹如何配置數(shù)據(jù)源、注解開發(fā)以及整合Junit,感興趣的同學(xué)可以參考一下

一:Spring整合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>
            <!--這個版本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)使用兩個注解:

①@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

以上就是Spring整合JUnit詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring整合JUnit的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • java算法導(dǎo)論之FloydWarshall算法實現(xiàn)代碼

    java算法導(dǎo)論之FloydWarshall算法實現(xiàn)代碼

    這篇文章主要介紹了算法導(dǎo)論之FloydWarshall算法實現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Spring Cloud詳解實現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法

    Spring Cloud詳解實現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法

    這篇文章主要介紹了Spring Cloud實現(xiàn)聲明式微服務(wù)調(diào)用OpenFeign方法,OpenFeign 是 Spring Cloud 家族的一個成員, 它最核心的作用是為 HTTP 形式的 Rest API 提供了非常簡潔高效的 RPC 調(diào)用方式,希望對大家有所幫助。一起跟隨小編過來看看吧
    2022-07-07
  • Java8新特性之Stream使用詳解

    Java8新特性之Stream使用詳解

    這篇文章主要介紹了Java8新特性之Stream使用詳解,流是用來處理集合中的數(shù)據(jù),以聲明的形式操作集合,它就像SQL語句,我們只需告訴流需要對集合進(jìn)行什么操作,它就會自動進(jìn)行操作,并將執(zhí)行結(jié)果交給你,無需我們自己手寫代碼,需要的朋友可以參考下
    2023-08-08
  • Java用自定義的類作為HashMap的key值實例

    Java用自定義的類作為HashMap的key值實例

    下面小編就為大家?guī)硪黄狫ava用自定義的類作為HashMap的key值實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • SpringBoot 集成 Druid過程解析

    SpringBoot 集成 Druid過程解析

    這篇文章主要介紹了SpringBoot 集成 Druid過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2019-09-09
  • MyBatis執(zhí)行SQL的兩種方式小結(jié)

    MyBatis執(zhí)行SQL的兩種方式小結(jié)

    本文主要介紹了MyBatis執(zhí)行SQL的兩種方式小結(jié),主要包括SqlSession 發(fā)送SQL和SqlSession獲取Mapper接口,通過Mapper接口發(fā)送SQL,具有一定的參考價值,感興趣的可以了解一下
    2023-10-10
  • 使用Java visualVM監(jiān)控遠(yuǎn)程JVM的流程分析

    使用Java visualVM監(jiān)控遠(yuǎn)程JVM的流程分析

    我們經(jīng)常需要對我們的開發(fā)的軟件做各種測試, 軟件對系統(tǒng)資源的使用情況更是不可少,JDK1.6開始自帶的VisualVM就是不錯的監(jiān)控工具,本文給大家分享使用Java visualVM監(jiān)控遠(yuǎn)程JVM的問題,感興趣的朋友跟隨小編一起看看吧
    2021-05-05
  • SpringBoot如何進(jìn)行對象復(fù)制的實踐

    SpringBoot如何進(jìn)行對象復(fù)制的實踐

    本文主要介紹了SpringBoot 如何進(jìn)行對象復(fù)制,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • java實現(xiàn)簡單學(xué)生管理系統(tǒng)項目

    java實現(xiàn)簡單學(xué)生管理系統(tǒng)項目

    這篇文章主要介紹了java實現(xiàn)簡單學(xué)生管理系統(tǒng)項目,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • Java入門交換數(shù)組中兩個元素的位置

    Java入門交換數(shù)組中兩個元素的位置

    在Java中,交換數(shù)組中的兩個元素是基本的數(shù)組操作,下面我們將詳細(xì)介紹如何實現(xiàn)這一操作,以及在實際應(yīng)用中這種技術(shù)的重要性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09

最新評論

西峡县| 麻江县| 扎兰屯市| 广平县| 金川县| 托里县| 东乡族自治县| 犍为县| 呼伦贝尔市| 永川市| 德惠市| 东兰县| 松溪县| 姜堰市| 四子王旗| 桦南县| 五原县| 台前县| 同心县| 疏勒县| 台南市| 东城区| 星子县| 龙井市| 靖江市| 建水县| 绍兴县| 五常市| 揭西县| 方正县| 资中县| 玉屏| 郎溪县| 彝良县| 当阳市| 隆尧县| 德格县| 社会| 高阳县| 交口县| 孝感市|