Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to fail fast Spring tests if the application cannot start?

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?

like image 215
vicusbass Avatar asked Nov 15 '25 16:11

vicusbass


2 Answers

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.

like image 62
Sam Brannen Avatar answered Nov 18 '25 19:11

Sam Brannen


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 {
//...
}
like image 25
Daniel Taub Avatar answered Nov 18 '25 21:11

Daniel Taub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!