zoukankan      html  css  js  c++  java
  • Spring:SpringRunner 和 SpringJUnit4ClassRunner

    环境

    1. jdk 7
    2. 4.3.24.RELEASE

    背景

    在使用 spring-test 的过程中,有两个 runner 可以选择,分别是 SpringRunner 和 SpringJUnit4ClassRunner。
    如果是在 4.3 之前,只能选择 SpringJUnit4ClassRunner,如果是 4.3 之后,建议选择 SpringRunner。
    SpringRunner 对 junit 的版本有要求,需要 4.12 及以上。

    使用示例

    加入依赖

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>4.3.24.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
    

    SpringJUnit4ClassRunner

    package jiangbo.springweb;
    
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration
    public class SpringJUnit4ClassRunnerTest {
    
        @Test
        public void testDemo() throws Exception {
    
            assertTrue(true);
        }
    
        @Configuration
        static class config {
        }
    }
    

    SpringRunner

    package jiangbo.springweb;
    
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringRunner;
    
    @RunWith(SpringRunner.class)
    @ContextConfiguration
    public class SpringRunnerTest {
    
        @Test
        public void testDemo() throws Exception {
    
            assertTrue(true);
        }
    
        @Configuration
        static class config {
        }
    }
    
  • 相关阅读:
    前端+php实现概率抽奖
    rem.js的用法及在浏览器端的适配
    python 使用记录及问题
    python 工具链 虚拟环境和包管理工具 pipenv
    python 工具链 多版本管理工具 pyenv
    python 工具链 包管理工具 pip
    ansible 使用记录
    mongodb connection refused because too many open connections: 819
    wordpress 常用操作
    服务器硬件测试
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/12851232.html
Copyright © 2011-2022 走看看