Abdul Basit
Abdul Basit

Reputation: 33

Spring Boot TestNG with Multiple AbstractTestNGSpringContextTests classes

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

Answers (1)

Issam El-atif
Issam El-atif

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.

  1. Use testng suites
  2. Remove @DirtiesContext and use @RunWith(SpringRunner.class) and @SpringBootTest

Check this https://github.com/ielatif/stackoverflow/tree/master/spring-boot-testng-sample

Upvotes: 2

Related Questions