Reputation: 326
I have a jUnit test case to test spring managed beans of helpers, utility, services etc. So that I have set the unit test class with SpringJUnit4ClassRunner and contextConfiguration. e.g. -
@ContextConfiguration({ "classpath:context-file.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
public class UnitTests {
@Test
public void test1() {
//...
}
@Test
public void test2() {
//...
}
@Test
public void test3() {
//...
}
@Test
public void test4() {
//...
}
}
I want to run these four(or more) unit test on some condition at run time e.g. if internet is available then run test UnitTests.test1(), if database is connected then run UnitTests.test2(), if image files directory is present then run test UnitTests.test3(), if a remote server is responding properly then run test UnitTests.test4().
Although I have went through jUnit org.junit.Assume but it doesn't suit, it can either enable the test framework to run all tests or skip all tests. @Ignore is also not applicable for these scenario. Also went through junit-ext (https://code.google.com/p/junit-ext/), but its dependency is not available on maven-central repository (junit-ext-maven-dependency). Please anybody help me out with some peace of code here.
Upvotes: 1
Views: 1103
Reputation: 97359
Based on the docs it looks exactly like what you need:
@Test
public void filenameIncludesUsername() {
assumeThat(File.separatorChar, is('/')); //Check here the condition
assertThat(new User("optimus").configFileName(), is("configfiles/optimus.cfg"));
}
@Test public void correctBehaviorWhenFilenameIsNull() {
assumeTrue(bugFixed("13356")); // bugFixed is not included in JUnit
assertThat(parse(null), is(new NullDocument()));
}
If the assumeThat() fail it will ignore the test case. If you do an assume in @Before or @BeforeClass than it will the whole class.
Excerpt from the docs:
A failing assumption in a @Before or @BeforeClass method will have the same effect as a failing assumption in each @Test method of the class.
Upvotes: 2