SpringBoot单元测试(三)

669 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第24天,点击查看活动详情

本文系作者 不太自律的程序猿原创,转载请私信并在文章开头附带作者和原文地址链接。

@TestPropertySource 设置System Property

@TestPropertySource 可以用来覆盖掉来自于系统环境变量、Java系统属性、@PropertySource的属性。

同时@TestPropertySource(properties=…)优先级高于@TestPropertySource(locations=…)。

利用它我们可以很方便的在测试代码里微调、模拟配置(比如修改操作系统目录分隔符、数据源等)。

Spring、Spring Boot和TestNG测试指南 - @TestPropertySource
segmentfault.com/a/119000001…

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
@TestPropertySource(properties = {"myproperty = foo"})
public class TestWarSpringContext {
...
}

How to set environment variable or system property in spring tests?
stackoverflow.com/questions/1…


@ClassRule 和 @Rule

junit中的 @ClassRule,可以在所有类方法开始前进行一些初始化调用,比如创建临时文件,

package com.jdriven;
 
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
 
import java.io.File;
import java.io.IOException;
 
public class JUnitClassRuleTest {
 
 
@ClassRule
public static TemporaryFolder temporaryFolder = new TemporaryFolder();
 
public static File tempFile;
 
@BeforeClass
public static void createTempFile() throws IOException {
tempFile = temporaryFolder.newFile("tempFile.txt");
}
 
@Test
public void testJUnitClassRule_One() {
//Your test should go here, which uses tempFile
}
 
@Test
public void testJUnitClassRule_Two() {
//Your test should go here and uses the same tempFile
}
}

其中,@ClassRule中指定创建临时文件夹,这是在所有的测试方法前会创建文件夹,并且会在所有测试完成后,递归删除其下的子目录和子文件夹。

@Rule 是方法级别的,每个测试方法执行时都会调用被注解的Rule,而@ClassRule是类级别的,在执行一个测试类的时候只会调用一次被注解的Rule

junit中的@classrule,@rule

@RunWith

@RunWith指定JUnit使用的单元测试执行类,使用@RunWith注解可以改变JUnit的默认执行类
@Runwith放在测试类名之前,用来确定这个类怎么运行的。也可以不标注,会使用默认运行器。

常见的运行器有:

  • 参数化运行器
    @RunWith(Parameterized.class) 参数化运行器,配合@Parameters使用junit的参数化功能
  • 测试集运行器
    @RunWith(Suite.class)
    @SuiteClasses({ATest.class,BTest.class,CTest.class})
    测试集运行器配合使用测试集功能
  • junit4的默认运行器
    @RunWith(JUnit4.class)
    junit4的默认运行器
  • 兼容junit3.8的运行器
    @RunWith(JUnit38ClassRunner.class)
    用于兼容junit3.8的运行器
  • SpringJUnit4ClassRunner
    @RunWith(SpringJUnit4ClassRunner.class)集成了spring的一些功能

@Test

在junit3中,是通过对测试类和测试方法的命名来确定是否是测试,且所有的测试类必须继承junit的测试基类。在junit4中,定义一个 测试方法变得简单很多,只需要在方法前加上@Test就行了。

注意:测试方法必须是public void,即公共、无返回数据。可以抛出异常。


@ContextConfiguration

@ContextConfiguration 注解用于指定 spring 配置文件所在的路径,有以下几个常用的属性:

locations 配置文件路径

locations 可以通过该属性手工指定 Spring 配置文件所在的位置,可以指定一个或多个 Spring 配置文件。如下所示:

@ContextConfiguration(locations = {"xx/yy/beans1.xml","xx/yy/beans2.xml"})
@ContextConfiguration(locations = "classpath*:spring-ctx-*.xml")

inheritLocations 是否继承父类配置

inheritLocations:是否要继承父测试用例类中的 Spring 配置文件,默认为 true。如下面的例子:

@ContextConfiguration(locations={"base-context.xml"})
public class BaseTest {

}
@ContextConfiguration(locations={"extended-context.xml"})
public class ExtendedTest extends BaseTest {

}

如果 inheritLocations 设置为 false,则 ExtendedTest 仅会使用 extended-context.xml 配置文件,否则将使用 base-context.xml 和 extended-context.xml 这两个配置文件。

classes 指定配置类

classes 属性可指定一些 Configuration 配置类,例如:

@SpringBootTest
@ContextConfiguration(classes = MyConfiguration.class)
public class ConsulLockTest {
}

感谢诸君的观看,文中如有纰漏,欢迎在评论区来交流。如果这篇文章帮助到了你,欢迎点赞👍关注。