I want to have a conditional teardown in my junit test cases, something like
@Test
testmethod1()
{
//condition to be tested
}
@Teardown
{
//teardown method here
}
in teardown i want to have a condition like
if(pass)
then execute teardown
else skip teardown
is such a scenario possible using junit?
You can do this with a TestRule.
TestRule
allows you to execute code before and after a test method. If the test throws an exception (or AssertionError for a failed assertion), then the test has failed, and you can skip the tearDown(). An example would be:Note that you're calling tearDown manually, so you don't want to have the @After annotation on the method, otherwise it gets called twice. For more examples, look at ExternalResource.java and ExpectedException.java.