[英]How to configure latest Spring Boot application to run unit tests at startup
Recently I started working on demo Spring Boot application and I was unable to run the unit test when starting the application.最近我开始研究演示 Spring Boot 应用程序,但在启动应用程序时无法运行单元测试。 The purpose of this approach is we need to run all unit tests before starting the application.
这种方法的目的是我们需要在启动应用程序之前运行所有单元测试。
Code of the Sample Demo Application is as follows.示例演示应用程序的代码如下。
Main class DemoApplication,主类DemoApplication,
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Model class School,示范班学校,
package com.example.demo;
import org.springframework.stereotype.Component;
@Component
public class School {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Main class Unit test,主类单元测试,
package com.example.demo;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
class DemoApplicationTests {
@Autowired
private School school;
@Test
void contextLoads() {
school.setName("test");
String schoolName = school.getName();
Assertions.assertThat(schoolName).isNotNull();
}
@Test
void main() {
DemoApplication.main(new String[]{});
school.setName("test");
String schoolName = school.getName();
Assertions.assertThat(schoolName).isNotNull();
}
}
School Unit test,学校单元测试,
package com.example.demo;
import org.junit.Assert;
import org.junit.Test;
public class SchoolTests {
@Test
public void getTodoTest() {
School school = new School();
String name = "test";
school.setName(name);
Assert.assertEquals(name, school.getName());
}
}
pom.xml file, 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.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
What are the additional configurations that need to be done to run the unit tests at the startup?在启动时运行单元测试需要做哪些额外的配置?
Some suggestions:一些建议:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.