How can I configure JUnit to ignore @Before and @After annotations
I don't think you can flick a JUnit 'switch' to disable this behaviour. JUnit offers two ways of changing test execution behaviour:
- Extend
BlockJUnit4ClassRunner
- Add a
Rule
to your test case
Both of these require you to change your test cases, of these the second option might be least invasive (though that does depend on whether your test cases are already using customised runners).
Given this simple JUnit runner:
public class IgnoreBeforeAndAfter extends BlockJUnit4ClassRunner {
public IgnoreBeforeAndAfter(Class<?> klass) throws InitializationError {
super(klass);
}
protected Statement withBefores(FrameworkMethod method, Object target,
Statement statement) {
return statement;
}
protected Statement withAfters(FrameworkMethod method, Object target,
Statement statement) {
return statement;
}
}
The following test case will pass (thereby proving that the @Before
and @After
methods are not invoked):
@RunWith(IgnoreBeforeAndAfter.class)
public class SimpleTest {
@Before
public void setUp() {
Assert.fail("The @Before method should be ignored!");
}
@After
public void tearDown() {
Assert.fail("The @After method should be ignored!");
}
@Test
public void canIgnoreBeforeAndAfter() {
assertTrue(true);
}
}
A structural search+replace on your code base could be used to add this annotation to all test cases which contain at least one of @Before
or @After
.
Though of course a structural search+replace could also remove all @Before
and @After
annotations from your code.
An approach which requires no changes to your existing test cases (but which is somewhat non standard) would be to provide no-op implementations of ...
org.junit.internal.runners.statements.RunAfters
org.junit.internal.runners.statements.RunBefores
... in your own code base, these would 'replace' the JUnit equivalents and if their evaluate()
method body was empty then your use of the @Before
and @After
annotations would have no effect.
Customize execution of Junit test suite in modules
As NamshubWriter commented, org.junit.ClassRule is found in JUnit4.9. @ClassRule
instead of @Rule
; TestRule
instead of MethodRule
.
See the sample:
public static class CustomCounter extends TestRule {
public int count = 0;
@Override
protected Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
count++;
base.evaluate();
}
};
}
}
public static class ExampleTestWithCustomClassRule {
@ClassRule
public static CustomCounter counter= new CustomCounter();
@Test
public void firstTest() {
assertEquals(1, counter.count);
}
@Test
public void secondTest() {
assertEquals(1, counter.count);
}
}
Related Topics
Password Protected Zip File in Java
Error Upon Assigning Layout: Boxlayout Can't Be Shared
Putting Char into a Java String for Each N Characters
Getting Java.Sql.Sqlexception: Operation Not Allowed After Resultset Closed
How to Compare Two Java Objects
How to Run Linux Commands in Java
What Makes Reference Comparison (==) Work for Some Strings in Java
Why Does the Ternary Operator Unexpectedly Cast Integers
How to Add a Resources Folder to My Java Project in Eclipse
List Versus Arraylist as Reference Type
How to Populate a Listview in Javafx Using Custom Objects
Java Date() Giving the Wrong Date
What Is the List of Valid @Suppresswarnings Warning Names in Java
How to Count Frequency of Characters in a String