Spring入门学习手册 4:Spring单元测试怎么搞?

上一篇在这php

完整代码在这spring

1、什么是单元测试

单元测试(unit testing),是指对软件中的最小可测试单元进行检查和验证。对于单元测试中单元的含义,通常来讲,要根据实际状况去断定其具体含义,如C语言中单元指一个函数,Java里单元指一个类,图形化的软件中能够指一个窗口或一个菜单等。总的来讲,单元就是人为规定的最小的被测功能模块。单元测试是在软件开发过程当中要进行的最低级别的测试活动,软件的独立单元将在与程序的其余部分相隔离的状况下进行测试。(来自百度百科)apache

回想一下前面的例子,每次进行测试的时候都用了一个主函数,在主函数的里调用要测试的类,而单元测试的话就不须要写主函数了。bash

2、Spring单元测试怎么搞?

(下面的例子基于上一个例子app

  1. 准备工做
  • 要准备的包

spring-testeclipse

junitmaven

<?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>SpringLearn</groupId>
    <artifactId>SpringDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>

        <!--单元测试的包-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>4.3.11.RELEASE</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <downloadSources>true</downloadSources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

复制代码
  1. 修改Test类为:
package com.learn.test;

import com.learn.service.ProductService;
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:applicationContext.xml")
public class Test {
    @Autowired
    ProductService ps;

    @org.junit.Test public void test() {
        ps.doSomeService();
    }

}
复制代码

这里要解释一下test()方法前面注解是,是由于Test注解 和 Test类重名了,因此必定要加上包名。函数

修改完成后main函数被去掉了而加上了和单元测试有关的注解。post

@RunWith(SpringJUnit4ClassRunner.class) 放在类名前,用来指明这是一个Spring的测试类。单元测试

@ContextConfiguration("classpath:applicationContext.xml") 放在类名前,找到Spring的配置文件。

@Test 放在要测试的方法前,测试逻辑。

运行的时候要用JUnit的方式运行,以下图所示:

运行结果:

start log:doSomeService
伪装这里是一些核心业务代码!
end log:doSomeService
复制代码
  1. 换个类试试

先修改一下ProductSer类为:

package com.learn.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
@Component("ps")
public class ProductService {

    @Test
    public void doSomeService() {
        System.out.println("伪装这里是一些核心业务代码!");
    }

}
复制代码

运行结果:

伪装这里是一些核心业务代码!
复制代码

能够看到单元测试彻底是独立的,前面配置好的AOP,一点做用都没起。

相关文章
相关标签/搜索