Sometimes our tests depends on certain conditions.
It might be anything - an external service shoulde up and running, a specific environmental variable’s value, specific date or/and time…
We already know how to skip tests conditionally in TestNG - How to skip TestNG tests based on condition
In JUnit we can do it easily with Assume
Conditionally skip/execute tests. Examples
Skip test if external service is unavailable
@Test
public void withAssumptionThatExternalServiceIsUp() {
Assume.assumeTrue(isExternalServiceAvailable());
...
}
Skip test on Friday
@Test
public void dontBotherMeWithTestsOnFriday() {
Assume.assumeFalse("Happy Friday! No testing today", TimeAndDatesUtils.isFridayToday());
...
}
Skip tests in Production
@Test
public void skipTestInProduction() {
Assume.assumeFalse("Careful! This is production", EnvironmentUtils.isProduction());
...
}
Skip tests if it’s raining
@Test
public void dontLikeRainTest() {
Assume.assumeFalse("No sun, no work", WheatherUtils.isItRaining());
...
}
You may also find these posts interesting: