zoukankan      html  css  js  c++  java
  • Spring整合JUnit测试单元

    目的:

    一:配置完之后,不需要我们手动创建Spring的容器,容器有Junit帮我们自动初始化
    二:可以在junit测试类中使用@AutoWired等方式注入对象,直接对其进行调用测试

    步骤

    1. 导入依赖坐标
    2. 在测试类上加入@RunWith注解,指定Spring的运行器
    3. 配置初始化Spring容器的配置文件(配置类)

    <!--spring测试jar包,需要Junit版本是4.12及以上,否则用不了-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.7.RELEASE</version>
    </dependency>

    给测单元添加注解

    /**
    * @RunWith(SpringJUnit4ClassRunner.class)
    * 注解作用:Spring的单元测试运行器替换原有运行器
    *
    * @ContextConfiguration(classes = SpringConfiguration.class)
    * 注解作用:指定配置类或者配置文件所在位置
    * classes属性:设置配置类字节码文件
    * locations属性:设置配置文件所在classpath路径
    */
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(classes = SpringConfiguration.class)
    public class TestPureAnnotation {
    //....省略

    此时则可以在测试单元中使用@AutoWired注解拿到spring容器中的注入对象。

  • 相关阅读:
    【AGC010 C】Cleaning
    【未知来源】火神的鱼
    【2017 北京集训 String 改编版】子串
    【未知来源】记忆
    【2017 江苏集训】子串
    【未知来源】循环移位
    【未知来源】K-th String
    【hdu 6067】Big Integer
    【CERC 2014 E】2048
    【hdu 6155】Subsequence Count
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/13633729.html
Copyright © 2011-2022 走看看