Question

We are using JUnit to execute integration tests and also the system integration tests which rely on external test systems (not necessarily maintained by our own company).

I wonder where to put the code that checks if the system is available prior to running the test cases? So I can determine if there is a network or other issue and not one with the test itself.

JUnit allows to setup some parts of the test in JUnit-Rules. Is it a good idea to setup the service that communicates with the external system within the rule and do some basic checks ("ping") to the external system within the rule? Or to store the state and within the Test use a JUnit assume(rule.isAvailable) to avoid having the test executed?

Or would it be smarter to put this verification code in a custom JUnit Runner?

Or is there even another way to do this? (simply create some utils?)

The goal is to skip the tests if some conditions are not met since it is obvious the tests will fail. I know this indicates a bad exception handling but there is a lot of legacy code I can't change altogether.

I tried to find some articles myself but it seems the search terms ("test", "external system" and so on) are a little thankless.

thanks!

Was it helpful?

Solution

Consider using org.junit.Assume.* as described here. When assumptions fail, your tests are ignored by default. You can write a custom runner to do something else when your assumptions fail.

The key thing though is that the tests don't fail when assumptions like the availability of your external services fail.

OTHER TIPS

If the ping applies to every single test in the class, I would put the ping call in the @Before method. @Before will get executed before every single test method (i.e., @JUnit -annotated methods).

If the ping does not apply to all tests in the class, then you would have to hit ping explicitly from those methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top