[英]Run main springboot application before integration tests
How can i run main application before integration tests by maven?我如何在 Maven 集成测试之前运行主应用程序? Now i have a really bad solution.
现在我有一个非常糟糕的解决方案。 Tests with commented code work properly but i need good practices way.
使用注释代码进行的测试可以正常工作,但我需要良好的实践方式。
@Slf4j
@SpringBootTest
@RunWith(SpringRunner.class)
@Category(Integration.class)
@ActiveProfiles("test")
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public class MyTestClass {
@BeforeClass
public static void setUp(){
/*SpringApplication.run(MyApplication.class).close();
System.setProperty("spring.profiles.active", "test");
MyApplication.main(new String[0]);*/
}
I want run tests by maven with arguments:我想通过带有参数的 maven 运行测试:
clean integration-test -Dgroups=xxx.annotation.type.Integration -Drun.jvmArguments=-Dspring.profiles.active=test
but it doesn't work.但它不起作用。 How can i correct this maven command line?
我怎样才能更正这个maven命令行?
To run your application on a specific profile for your integration test, you need to annotate the test class with @SpringBootTest
and @ActiveProfiles
with parameters as below:要在集成测试的特定配置文件上运行您的应用程序,您需要使用
@SpringBootTest
和@ActiveProfiles
注释测试类,参数如下:
@SpringBootTest(classes = {MyApplication.class}, webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
The application you define in classes = {MyApplication.class}
would be started on a random port when you provide webEnvironment = WebEnvironment.RANDOM_PORT
using the profile specified on @ActiveProfiles
.当您使用在
@ActiveProfiles
上指定的配置文件提供webEnvironment = WebEnvironment.RANDOM_PORT
时,您在classes = {MyApplication.class}
中定义的应用程序将在随机端口上启动。 In case you want it to be run on the defined port, then use WebEnvironment.DEFINED_PORT
.如果您希望它在定义的端口上运行,请使用
WebEnvironment.DEFINED_PORT
。 In case you need to get port (random port), you can autowire its value to local field to test like this:如果您需要获取端口(随机端口),您可以将其值自动连接到本地字段以进行测试,如下所示:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TestClass {
@LocalServerPort
private int port;
@Test public void someTest() {}
}
You can use the spring-boot-maven-plugin and bind it to the pre-integration-test phase in maven like this:您可以使用spring-boot-maven-plugin并将其绑定到 Maven 中的预集成测试阶段,如下所示:
<project> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.0.4.RELEASE</version> <executions> <execution> <id>pre-integration-test</id> <goals> <goal>start</goal> </goals> </execution> <execution> <id>post-integration-test</id> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.