The simplest solution would be to split your tests up into two suites: integration tests and pure unit tests. Then develop a script or some other automated means to determine if the server is up, and simply skip the integration test suite if the server is down. But if grouping the tests into suites like this is not practical for some reason, here's an alternative:
You could create a custom Runner that skips tests if the server is unavailable. You could either program the runner to determine server availability on its own or determine it through some external process such as a script that executes before the test phase and sets a system property on the JVM that the runner can check (e.g. pass -Dcom.company.testrun.integration=false
as a command line argument).
You could enable your custom runner using the @RunWith
annotation on your integration test classes and use the built-in runner for all other tests so they are not impacted. Alternatively, you could use your runner for all tests and invent a new annotation (e.g. @IntegrationTest
) that you use to decorate your integration test methods. Using the latter approach, the runner would apply its skip logic only if the server is unavailable and the test method has the special annotation.