• 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 {
        }
    }
    
  • 相关阅读:
    Python之异常篇 [待更新]
    python脚本工具 - 4 获取系统当前时间
    python脚本工具 - 3 目录遍历
    数字签名和数字证书到底是个神马玩意?
    CSRF攻击[转]
    Python之数据结构篇
    Python之模块篇
    Python之函数篇
    python脚本工具-2 去除扩展名后提取目录下所有文件名并保存
    python脚本工具-1 制作爬虫下载网页图片
  • 原文地址:https://www.cnblogs.com/jiangbo44/p/12851232.html
Copyright © 2020-2023  润新知