I'm looking for some validation as to how Assume.assumeTrue()
works in JUnit. I'd like to use it in a @BeforeClass
method so that I can avoid running my test suite if a condition isn't met.
However, I was wondering as to the nature of the method. If assumeTrue
receives a parameter with a false value, does it skip the rest of the method (that's annotated with @BeforeClass
) or does it execute the rest of the remaining instructions.
I'm also curious of any other implications it might have for methods annotated with:
@After
@Before
@AfterClass
Edit:
After running it through a pretty basic test, if assumeTrue(false)
is ever run, then the rest of the method will be ignored as well as any methods annotated with @Test
@After
or @Before
.
To me it was a little surprising that the rest of the method was skipped as well as the fact that it also ignored @Before
and @After
in addition to @Test
if assumeTrue
is placed in the @BeforeClass
method. The documentation doesn't specify that sort of behavior (which I checked before asking the question).
Edit2: Using assumeTrue()
in a @BeforeClass
method is very plausible, as there may be environmental variables (i.e. your build/test machine is stressed for resources) that you may want to check before running your test suite. This can help avoid getting test failures (like timeouts) caused by a slow system.
So I'm still not sure why this is getting downvoted so hard...
The test I ran it through looked like this
@BeforeClass
public static void beforeClassMethod()
{
System.out.println("BeforeClass assume");
Assume.assumeTrue(false);
System.out.println("AfterClass assume");
}
@Before
public void beforeMethod()
{
System.out.println("Before");
}
@Test
public void testMethod()
{
System.out.println("Test");
}
@After
public void afterMethod()
{
System.out.println("After");
}
@AfterClass
public static void afterClassMethod()
{
System.out.println("AfterClass");
}
Output:
BeforeClass assume
AfterClass