SpringRunner教程展示了如何使用SpringRunner测试 Spring 应用。
Spring 是流行的 Java 应用框架。 在本教程中,我们使用 Spring 5 版本。
SpringRunner
SpringRunner是SpringJUnit4ClassRunner的别名,该别名将JUnit测试库与 Spring TestContext Framework 结合在一起。 我们将其与@RunWith(SpringRunner.class)一起使用。
使用SpringRunner,我们可以实现基于 JUnit 4 的标准单元测试和集成测试。
Spring TestContext Framework 提供了通用的,注解驱动的单元和集成测试支持,这些支持与使用中的测试框架(JUnit,TestNG)无关。
SpringRunner示例
在以下应用中,我们使用SprigRunner测试一个简单的服务。 该应用是一个 Spring 独立控制台应用。
该应用包含两个属性文件:一个文件用于生产应用,另一个文件用于测试。
pom.xmlsrc├───main│ ├───java│ │ └───com│ │ └───zetcode│ │ │ Application.java│ │ ├───config│ │ │ AppConfig.java│ │ └───service│ │ HelloService.java│ └───resources│ application.properties│ logback.xml└───test├───java│ └───com│ └───zetcode│ └───service│ HelloServiceTest.java└───resourcesappTest.properties
这是项目结构。
pom.xml
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zetcode</groupId><artifactId>springrunnerex</artifactId><version>1.0-SNAPSHOT</version><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><spring-version>5.1.3.RELEASE</spring-version></properties><dependencies><dependency><groupId>ch.qos.logback</groupId><artifactId>logback-classic</artifactId><version>1.2.3</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>${spring-version}</version></dependency><dependency><groupId>org.hamcrest</groupId><artifactId>hamcrest-all</artifactId><version>1.3</version><scope>test</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.codehaus.mojo</groupId><artifactId>exec-maven-plugin</artifactId><version>1.6.0</version><configuration><mainClass>com.zetcode.Application</mainClass></configuration></plugin></plugins></build></project>
这是 Maven 构建文件。 我们具有以下依赖项:logback-classic用于记录日志,spring-context和spring-core是基本的 Spring 依赖项,spring-test用于测试,hamcrest-all包含 Hamcrest 匹配库的所有模块,而JUnit是用于单元测试的库。
exec-maven-plugin帮助执行系统和 Java 程序。
resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?><configuration><logger name="org.springframework" level="ERROR"/><logger name="com.zetcode" level="INFO"/><appender name="consoleAppender" class="ch.qos.logback.core.ConsoleAppender"><encoder><Pattern>%d{HH:mm:ss.SSS} %blue(%-5level) %magenta(%logger{36}) - %msg %n</Pattern></encoder></appender><root><level value="INFO" /><appender-ref ref="consoleAppender" /></root></configuration>
logback.xml是 Logback 日志库的配置文件。
resources/application.properties
app.message=Hello there!
application.properties包含一个消息属性,由HelloMessage服务显示。
com/zetcode/AppConfig.java
package com.zetcode.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;@Configuration@ComponentScan(basePackages = "com.zetcode")@PropertySource("application.properties")public class AppConfig {}
AppConfig配置组件扫描并从提供的文件中加载属性。
com/zetcode/servide/HelloService.java
package com.zetcode.service;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Service;@Servicepublic class HelloService {@Value("${app.message}")private String message;public String sayHello() {return message;}}
HelloService返回从application.properties文件检索到的消息。
com/zetcode/Application.java
package com.zetcode;import com.zetcode.config.AppConfig;import com.zetcode.service.HelloService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.AnnotationConfigApplicationContext;import org.springframework.stereotype.Component;@Componentpublic class Application {private static final Logger logger = LoggerFactory.getLogger(Application.class);public static void main(String[] args) {var ctx = new AnnotationConfigApplicationContext(AppConfig.class);var app = ctx.getBean(Application.class);app.run();ctx.close();}@Autowiredprivate HelloService helloService;private void run() {logger.info("Calling hello service");logger.info(helloService.sayHello());}}
应用使用HelloService将消息打印到控制台。
$ mvn -q exec:java17:50:54.118 INFO com.zetcode.Application - Calling hello service17:50:54.118 INFO com.zetcode.Application - Hello there!
我们运行该应用。
resources/appTest.properties
app.message=Testing hello message
appTest.properties专用于测试。
com/zetcode/service/HelloServiceTest.java
package com.zetcode.service;import com.zetcode.config.AppConfig;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Value;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.TestPropertySource;import org.springframework.test.context.junit4.SpringRunner;import static org.hamcrest.CoreMatchers.equalTo;import static org.junit.Assert.assertThat;@RunWith(SpringRunner.class)@ContextConfiguration(classes={HelloService.class})@TestPropertySource("/appTest.properties")public class HelloServiceTest {@Value("${app.message}")private String message;@Autowiredprivate HelloService helloService;@Testpublic void testHelloMessage() {var message = helloService.sayHello();assertThat(message, equalTo(message));}}
HelloServiceTest用于测试HelloService类。
@RunWith(SpringRunner.class)@ContextConfiguration(classes={HelloService.class})@TestPropertySource("/appTest.properties")public class HelloServiceTest {
测试类用@RunWith(SpringRunner.class)注解。 @ContextConfiguration定义了类级别的元数据,用于确定如何加载和配置用于集成测试的应用上下文。 此外,我们还提供了@TestPropertySource自定义测试属性文件。
@Value("${app.message}")private String message;
我们从appTest.properties文件注入消息。
@Autowiredprivate HelloService helloService;
我们注入HelloMessage服务类。 这是要测试的类。
@Testpublic void testHelloMessage() {var message = helloService.sayHello();assertThat(message, equalTo(message));}
我们测试来自service方法的消息是否等于注入的字符串值。
$ mvn -q test-------------------------------------------------------T E S T S-------------------------------------------------------Running com.zetcode.service.HelloServiceTestTests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.489 secResults :Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
我们运行测试。
在本教程中,我们展示了如何使用SpringRunner在 Spring 应用中创建测试。 Spring MockMvc 教程, Spring @PropertySource教程, Java 教程。
