Why is this an issue?
Overriding a parent class method prevents that method from being called unless an explicit super
call is made in the overriding
method. In some cases, not calling the parent method is fine. However, setUp
and tearDown
provide some shared logic that is
called before all test cases. This logic may change over the lifetime of your codebase. To make sure that your test cases are set up and cleaned up
consistently, your overriding implementations of setUp
and tearDown
should call the parent implementations explicitly.
How to fix it
Add an explicit call to super.setUp()
and super.tearDown()
in the overriding methods.
Code examples
Noncompliant code example
public class MyClassTest extends MyAbstractTestCase {
private MyClass myClass;
@Override
protected void setUp() throws Exception { // Noncompliant
myClass = new MyClass();
}
}
Compliant solution
public class MyClassTest extends MyAbstractTestCase {
private MyClass myClass;
@Override
protected void setUp() throws Exception {
super.setUp();
myClass = new MyClass();
}
}