I have this base test class in one Spring Boot module:
@ActiveProfiles("test")
@SpringBootTest(classes = {WebServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class BaseWebServiceTest {
//...
}
If for some reason the application cannot start (in my case, if localstack docker image is not started of if Spring Cloud Contract stubs are not available, etc) the tests are still being run and obviously they all fail. Is there any way of skipping all tests if ApplicationContext is not loaded?
Is there any way of skipping all tests if ApplicationContext is not loaded?
No. There is no way to skip tests automatically if the ApplicationContext does not load.
However, you could make use of JUnit 4's assumption support and abort the execution of the tests based on some boolean condition that you choose. For example, if you can check whether the docker image started, you could then do something similar to the following.
public static boolean dockerImagedStarted() {
// return true if the Docker image started...
}
@BeforeClass
public static void ensureDockerImageStarted() {
org.junit.Assume.assumeTrue(dockerImagedStarted());
}
p.s. please note that there is an open JIRA issue requesting a built-in feature to avoid repeated attempts to load an ApplicationContext. See SPR-9548 for details.
You can create a runner that will ignore your tests in case that the ApplicationContext is not loaded
public class CustomRunner extends BlockJUnit4ClassRunner {
public CustomRunner(Class<?> clazz) throws InitializationError {
super(clazz);
}
@Override
protected boolean isIgnored(FrameworkMethod child) {
return shouldIgnore() || super.isIgnored(child);
}
/**
*
* @return if your test should be ignored or not
*/
private boolean shouldIgnore() {
// Some check if your docker is up
return true;
}
}
And the use @RunWith with the CustomRunner you have created
@RunWith(CustomRunner.class)
@ActiveProfiles("test")
@SpringBootTest(classes = {WebServiceApplication.class}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class BaseWebServiceTest {
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With