运行 在命令行中从可执行 jar 测试 SpringBootTest
Run Tests SpringBootTest from executable jar in command line
我需要 运行 一个包含所有测试的罐子。
我可以在 jar 中构建所有测试 classes 但我不知道如何在 springboot 中启动主 class 到 运行 junit 测试。
有没有可能我 运行 命令行到 运行 jar 并开始所有测试并忽略 mainclass.
测试Class
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:${test.config.env:application-local.properties}")
@TestMethodOrder(OrderAnnotation.class)
public class RunIntegrationTest {
// All tests
}
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.3.2.RELEASE</version>
<relativePath />
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<name>...</name>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<!-- get all compiled class tests into source class -->
<testOutputDirectory>target/classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
为了 运行在命令行上从构建的 jar 中执行所有测试,我需要执行什么?
java -jar build-with-class-test-and-junit-in-it.jar # are there more parameters?
我有一个解决方案 运行 在 jar 中使用 junit4
和 SpringRunner
进行 springboot 测试。
创建测试Class
import org.junit.FixMethodOrder;
import org.junit.Test; // don't use org.junit.jupiter.api.test for this case
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class) // using SpringRunner instead of @ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-${spring.profiles.active:local}.properties")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RunIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
// ... all integration test
@Test
// ... all integration test
}
正在创建文件 pom-test-jar.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.3.2.RELEASE</version>
<relativePath />
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<name>...</name>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- all test class will be compiled with all source -->
<!-- <scope>test</scope> -->
</dependency>
</dependencies>
<build>
<!-- all test class will be compiled with all source -->
<testOutputDirectory>target/classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- changing launcher to exec junit instead of spring main class -->
<mainClass>org.junit.runner.JUnitCore</mainClass>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
就这些了:)
让我们创建包含测试的 jar 和 运行 所有测试
# BUILD TEST JAR:
mvn -f "pom-test-jar.xml" clean package -DskipTests
# RUN TEST INTEGRATION
export SPRING_PROFILES_ACTIVE=hom
java -jar target/myjar.jar br.com.package.test.RunIntegrationTest
unset SPRING_PROFILES_ACTIVE
我需要 运行 一个包含所有测试的罐子。 我可以在 jar 中构建所有测试 classes 但我不知道如何在 springboot 中启动主 class 到 运行 junit 测试。 有没有可能我 运行 命令行到 运行 jar 并开始所有测试并忽略 mainclass.
测试Class
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:${test.config.env:application-local.properties}")
@TestMethodOrder(OrderAnnotation.class)
public class RunIntegrationTest {
// All tests
}
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.3.2.RELEASE</version>
<relativePath />
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<name>...</name>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<!-- get all compiled class tests into source class -->
<testOutputDirectory>target/classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
为了 运行在命令行上从构建的 jar 中执行所有测试,我需要执行什么?
java -jar build-with-class-test-and-junit-in-it.jar # are there more parameters?
我有一个解决方案 运行 在 jar 中使用 junit4
和 SpringRunner
进行 springboot 测试。
创建测试Class
import org.junit.FixMethodOrder;
import org.junit.Test; // don't use org.junit.jupiter.api.test for this case
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
@RunWith(SpringRunner.class) // using SpringRunner instead of @ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
@SpringBootTest
@TestPropertySource(locations = "classpath:application-${spring.profiles.active:local}.properties")
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class RunIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
// ... all integration test
@Test
// ... all integration test
}
正在创建文件 pom-test-jar.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.3.2.RELEASE</version>
<relativePath />
</parent>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<name>...</name>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!-- all test class will be compiled with all source -->
<!-- <scope>test</scope> -->
</dependency>
</dependencies>
<build>
<!-- all test class will be compiled with all source -->
<testOutputDirectory>target/classes</testOutputDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<!-- changing launcher to exec junit instead of spring main class -->
<mainClass>org.junit.runner.JUnitCore</mainClass>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>
</plugins>
</build>
</project>
就这些了:)
让我们创建包含测试的 jar 和 运行 所有测试
# BUILD TEST JAR:
mvn -f "pom-test-jar.xml" clean package -DskipTests
# RUN TEST INTEGRATION
export SPRING_PROFILES_ACTIVE=hom
java -jar target/myjar.jar br.com.package.test.RunIntegrationTest
unset SPRING_PROFILES_ACTIVE