zoukankan      html  css  js  c++  java
  • Junit集成测试

    Spring4.x高级话题(七):Spring的测试

    . 点睛

    测试是开发工作中不可缺少的部分,单元测试只针对当前开发的类和方法进行测试,可以简单通过模拟依赖来实现,对运行环境没有依赖;但是仅仅单元测试是不够的,它只能验证当前类或者方法能否正常工作,而我们想要知道系统的各个部分组合在一起是否能够正常工作,这就涉及到了集成测试。

    集成测试一般需要来自不同层的不同对象的交互,如数据库,网络连接,Ioc容器等。其实我们也经常通过运行程序,然后通过自己操作来完成类似于集成测试的流程。集成测试为我们提供了一种无须部署或运行程序来完成验证系统各部分功能是否能正常协同工作的能力。

    Spring通过Spring TestContext Framework对集成测试提供顶级支持。它不依赖特定的测试框架,即可使用Junit,也可使用TestNG。

    基于maven构建的项目结构默认有关于测试的目录是:src/test/java(测试代码),src/test/resources(测试资源),它们区别于src/main/java(项目源码),src/main/resource(项目资源)。

    Spring提供了一个SpringJUnit4ClassRunner类,它提供了Spring TestContext Framework的功能。通过@ContextConfiguration来配置Application Context,通过@ActiveProfiles确定活动的profile。

    在使用了Spring测试后,我们前面文章中例子的”运行”部分都可以用Spring测试来检验功能能否正常运作。

    集成测试涉及程序中的各个分层,本节只对简单配置的Application Context和在测试中注入Bean做演示。

    . 示例

    1.准备

    增加Spring测试的依赖包到maven:

    <!-- Spring test 支持 -->

            <dependency>

                <groupId>org.springframework</groupId>

                <artifactId>spring-test</artifactId>

                <version>${spring-framework.version}</version>

            </dependency>

            <dependency>

                <groupId>junit</groupId>

                <artifactId>junit</artifactId>

                <version>4.11</version>

            </dependency>

    2. 业务代码

    在src/main/java下的源码:

    package org.light4j.sping4.senior.fortest;

     

    public class TestBean {

        private String content;

     

        public TestBean(String content) {

            super();

            this.content = content;

        }

     

        public String getContent() {

            return content;

        }

     

        public void setContent(String content) {

            this.content = content;

        }

     

    }

    3. 配置类

    在src/main/java下的源码:

    package org.light4j.sping4.senior.fortest;

     

    import org.springframework.context.annotation.Bean;

    import org.springframework.context.annotation.Configuration;

    import org.springframework.context.annotation.Profile;

    @Configuration

    public class TestConfig {

        @Bean

        @Profile("dev")

        public TestBean devTestBean() {

            return new TestBean("from development profile");

        }

     

        @Bean

        @Profile("prod")

        public TestBean prodTestBean() {

            return new TestBean("from production profile");

        }

     

    }

    4. 测试

    在src/test/java下的源码:

    package org.light4j.sping4.senior.fortest;

     

     

    import org.junit.Assert;

    import org.junit.Test;

    import org.junit.runner.RunWith;

    import org.springframework.beans.factory.annotation.Autowired;

    import org.springframework.test.context.ActiveProfiles;

    import org.springframework.test.context.ContextConfiguration;

    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

     

    @RunWith(SpringJUnit4ClassRunner.class) //①

    @ContextConfiguration(classes = {TestConfig.class}) //②

    @ActiveProfiles("prod") //③

    public class DemoBeanIntegrationTests {

        @Autowired //④

        private TestBean testBean;

     

        @Test //⑤

        public void prodBeanShouldInject(){

            String expected = "from production profile";

            String actual = testBean.getContent();

            Assert.assertEquals(expected, actual);

        }

    }

    代码解释:

    ① SpringJUnit4ClassRunner 在Junit环境下提供Spring TestContext Framework的功能。
    ② @ContextConfiguration 用来加载配置ApplicationContext,其中classes属性用来加载配置类。
    ③ @ActiveProfiles用来声明活动的profile
    ④ 可使用普通的@Autowired注入Bean。
    ⑤ 测试代码,通过JUnit的Assert来校验结果是否和预期一致。

    运行结果如下图所示:

    将@ActiveProfiles("prod")改为@ActiveProfiles("dev"),演示测试不能通过的情景,运行结果如下图所示:

    引入的三个jar包

     

  • 相关阅读:
    (Toolbar)Android中如何消除Toolbar左边的空白
    (TextView)Android中为TextView赋初始值
    (Edittext)Android中界面中有多个Edittext,如何默认让第二个获取焦点
    (警告)Android中报Custom view `&#215;&#215;&#215;` has setOnTouchListener called on it but does not override performClick警告
    (Toolbar)Android中app:showASAction的值及含义
    个人课程总结
    (list)关于list清空问题的解决
    Ubuntu hive 安装过程中遇到的一些问题
    学习进度——第十七周
    个人课程总结
  • 原文地址:https://www.cnblogs.com/wangchaonan/p/10731529.html
Copyright © 2011-2022 走看看