what needs to be initialized for spring-boot integration tests to run standalone

120 views Asked by At

This is what my base test class looks like -

@SpringApplicationConfiguration(classes = ServiceApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port:8084")
public class BaseTestStartService {

    //TODO:add basic service test

}

Not sure what is to be implemented in this base class? What is the correct process? The base class is implemented in the tests classes as -

 @RunWith(SpringJUnit4ClassRunner.class)
    public class TestClass extends BaseTestAPIService{
//add assertions
}

I am trying to implement the correct process so looking for some advice.

1

There are 1 answers

0
tiago On

Maybe you could give more insight on what exactly your service does and what you want to test, e.g. REST calls with RestTemplate or direct method calls to the component bean.

Either way, some tips might be interesting:

  • you may need to take advantage of the random port assignment, by specifying server.port:0 and eventually autowiring an int with @Value("${local.server.port}")
    • otherwise the test runner may reuse context and try to start the service a second time, on the same port, when running a second implementation;
  • you can use @Before and @After anotations to setup a common set of fixtures on the base class;
  • the base class should be abstract;
  • the main SpringApplicationConfiguration class is usually called "SomethingTestedHost".

Either way, a common good practice is to keep your services modular and have one different Test Case per @Test annotated method. Judging by your configuration it seems you might have a to large service for which you are structuring the tests. Which could be a good practice if you are using Test Driven Development and plan to segregate the tested service later on.