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

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

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

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

配置過(guò)程:

加載依賴(lài)

引入Maven依賴(lài):

        <!--spring單元測(cè)試依賴(lài) -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${springframework}</version>
            <scope>test</scope>
        </dependency>

編寫(xiě)SpringTestBase基礎(chǔ)類(lèi),加載所需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。

編寫(xiě)單元測(cè)試類(lèi) 示例

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

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測(cè)試數(shù)據(jù)

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

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

  <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)中的測(cè)試包,而junit是單元測(cè)試包,結(jié)合起來(lái)使用。

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

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 注冊(cè)到 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、測(cè)試方法

@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的動(dòng)態(tài)綁定與雙分派_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

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

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

    java把字符串寫(xiě)入文件里的簡(jiǎn)單方法分享

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

    springboot整合通用Mapper簡(jiǎn)化單表操作詳解

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

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

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

    Java7和Java8中的ConcurrentHashMap原理解析

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

    mybatis之批量添加問(wèn)題

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

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

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

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

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

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

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