Reputation: 3
How can i run main application before integration tests by 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:
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?
Upvotes: 0
Views: 2761
Reputation: 15173
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(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
. In case you want it to be run on the defined port, then use 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() {}
}
Upvotes: 4
Reputation: 593
You can use the spring-boot-maven-plugin and bind it to the pre-integration-test phase in maven like this:
<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>
Upvotes: 1