Reputation: 33
Spring Boot TestNG with Multiple AbstractTestNGSpringContextTests classes initializes Spring Boot Context per Test Classes.
I have defined multiple TestNG Test Classes like this :-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class TestNGClass1 extends AbstractTestNGSpringContextTests{
@LocalServerPort
public int port;
.......
}
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class TestNGClass2 extends AbstractTestNGSpringContextTests{
@LocalServerPort
public int port;
.......
}
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@DirtiesContext
public class TestNGClass3 extends AbstractTestNGSpringContextTests{
@LocalServerPort
public int port;
.......
}
Is there a way out to reuse Spring Context across all those classes ?
Upvotes: 2
Views: 4958
Reputation: 2496
@DirtiesContext Test annotation which indicates that the ApplicationContext associated with a test is dirty and should therefore be closed and removed from the context cache.
@DirtiesContext
and use @RunWith(SpringRunner.class) and @SpringBootTest
Check this https://github.com/ielatif/stackoverflow/tree/master/spring-boot-testng-sample
Upvotes: 2