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

如何使用Spring-Test對Spring框架進(jìn)行單元測試

 更新時(shí)間:2021年09月06日 09:48:18   作者:W-大泡泡  
這篇文章主要介紹了如何使用Spring-Test對Spring框架進(jìn)行單元測試,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

Spring-Test對Spring框架進(jìn)行單元測試

配置過程:

加載依賴

引入Maven依賴:

        <!--spring單元測試依賴 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework}</version>
            <scope>test</scope>
        </dependency>

編寫SpringTestBase基礎(chǔ)類,加載所需xml文件

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ContextConfiguration(locations = { "classpath:Application-Redis.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class SpringTestBase extends AbstractJUnit4SpringContextTests {
}

將所需加載的xml文件指定為locations的value。

編寫單元測試類 示例

直接繼承SpringTestBase 就可以對Spring框架的內(nèi)容進(jìn)行單元測試。

import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
public class TestRedisCacheDaoImpl extends SpringTestBase {
    @Autowired
    public RedisCacheDaoImpl redisCacheDaoImpl;
    @Test
    public void testPing() {
        boolean reslut = redisCacheDaoImpl.ping();
        Assert.assertEquals(true, reslut);
    }
}

Spring-Test測試數(shù)據(jù)

1、新建一個(gè)maven項(xiàng)目

2、pom.xml當(dāng)中添加依賴

  <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.2.5.RELEASE</version>
        </dependency>   
        
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

spring-test是Spring當(dāng)中的測試包,而junit是單元測試包,結(jié)合起來使用。

添加一個(gè)bean,便于測試。

HelloService.java

@Service
public class HelloService {
    public String hello(String name) {
        return "hello " + name;
    } 
}

添加配置文件掃描包:

<?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 https://www.springframework.org/schema/context/spring-context.xsd">
    <!--將除了 Controller 之外的所有 Bean 注冊到 Spring 容器中-->
    <context:component-scan base-package="org.youyuan" use-default-filters="true">
        <context:exclude-filter type="annotation" expression="org.youyuan.controller"/>
    </context:component-scan> 
</beans>

3、測試方法

@ContextConfiguration("classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class Test { 
    @Autowired
    HelloService helloService;
    @org.junit.Test
    public void test(){
        System.out.println(helloService.hello("youyuan"));
    }
}

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

  • Java的動態(tài)綁定與雙分派_動力節(jié)點(diǎn)Java學(xué)院整理

    Java的動態(tài)綁定與雙分派_動力節(jié)點(diǎn)Java學(xué)院整理

    這篇文章主要介紹了Java的動態(tài)綁定與雙分派,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • java把字符串寫入文件里的簡單方法分享

    java把字符串寫入文件里的簡單方法分享

    這篇文章主要介紹了java把字符串寫入到文件里的簡單方法,這是跟一個(gè)外國朋友學(xué)的代碼,需要的朋友可以參考下
    2014-03-03
  • springboot整合通用Mapper簡化單表操作詳解

    springboot整合通用Mapper簡化單表操作詳解

    這篇文章主要介紹了springboot整合通用Mapper簡化單表操作,通用Mapper是一個(gè)基于Mybatis,將單表的增刪改查通過通用方法實(shí)現(xiàn),來減少SQL編寫的開源框架,需要的朋友可以參考下
    2019-06-06
  • JavaWeb實(shí)現(xiàn)RSA+AES混合加密

    JavaWeb實(shí)現(xiàn)RSA+AES混合加密

    RSA+AES的混合加密時(shí),AES用于給傳輸?shù)臄?shù)據(jù)加密,然后通過RSA給AES的秘鑰加密,本文就來詳細(xì)的介紹一下如何實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-10-10
  • Java7和Java8中的ConcurrentHashMap原理解析

    Java7和Java8中的ConcurrentHashMap原理解析

    這篇文章主要介紹了Java7和Java8中的ConcurrentHashMap原理解析,對ConcurrentHashMap感興趣的讀者,一定要好好看一下
    2021-04-04
  • mybatis之批量添加問題

    mybatis之批量添加問題

    這篇文章主要介紹了mybatis之批量添加問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Mybatis中l(wèi)ike搭配concat的寫法詳解

    Mybatis中l(wèi)ike搭配concat的寫法詳解

    這篇文章主要介紹了Mybatis中l(wèi)ike搭配concat的寫法詳解,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java中讀寫鎖ReadWriteLock的原理與應(yīng)用詳解

    Java中讀寫鎖ReadWriteLock的原理與應(yīng)用詳解

    Java并發(fā)編程提供了讀寫鎖,主要用于讀多寫少的場景,今天我們就重點(diǎn)來講解讀寫鎖ReadWriteLock的原理與應(yīng)用場景,感興趣的可以了解一下
    2022-09-09
  • java創(chuàng)建線程的兩種方法區(qū)別

    java創(chuàng)建線程的兩種方法區(qū)別

    這篇文章主要為大家區(qū)分了java創(chuàng)建線程的兩種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 最新評論