15

These are my testcases

class Mother { @Before public void setUp() { if (!this.getClass().isAnnotatedWith("Version20")) { // pseudo code /* * stop this test without failing! */ } // further setup } } @Version20 class Child extends Mother { @Test public void test() { // run only when Version == 20 } } 

Is it possible to stop the test in Child in the @Before method of Mother without failing or assertTrue(false)?

edit: I have more Versions @Version19, @Version18 etc. my software is reading a configfile and outputting the result some tests are only applicable for special Versions. I don't want to make the version check within the test method because i have a lot of small tests and don't like code duplication

3
  • 4
    Why not just use @ignore, if you're going to annotate the tests anyway? Commented May 2, 2012 at 11:53
  • How about adding the check for Version20 within the test method? Commented May 2, 2012 at 11:53
  • possible duplicate of Conditionally ignoring tests in JUnit 4 Commented May 2, 2012 at 12:04

1 Answer 1

19

I asked a similar question previously - the result was that you can use the Assume class' methods to disable a test based on a run-time check (whereas @Ignore is a static condition).

That said, if you're always disabling them based on a specific annotation, it would seem that you don't actually need to do this at run-time. Simply annotating the classes with @Ignore as well as @Version20 would do the job, and would be arguably clearer.

Though I suspect you might be ignoring only if the test is running in "1.0 mode" - in which case it is a runtime introspection, and you could do this with something like:

@Before public void setUp() { if (!this.getClass().isAnnotatedWith("Version20")) { final String version = System.getProperty("my.app.test.version"); org.junit.Assume.assumeTrue(version.equals("2.0")); } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.