1

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.

1
  • You will have to write the verification code somewhere. What's wrong with Assume.assumeTrue(SomeTestFixture.somePrecondition()) ? It essentially stops the execution at that point and mark the test as skipped. Commented May 21, 2014 at 13:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks khmarbaise! It is one of the better solution if there is no hope for something out-of-the-box.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.