rspec/rules/S2188/java/rule.adoc
2023-07-19 18:39:53 +02:00

70 lines
1.7 KiB
Plaintext

== 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
[source,java,diff-id=1,diff-type=noncompliant]
----
public class MyClassTest extends MyAbstractTestCase {
private MyClass myClass;
@Override
protected void setUp() throws Exception { // Noncompliant
myClass = new MyClass();
}
}
----
==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
public class MyClassTest extends MyAbstractTestCase {
private MyClass myClass;
@Override
protected void setUp() throws Exception {
super.setUp();
myClass = new MyClass();
}
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Add a "super.[setUp()|tearDown()]" call to this method.
'''
== Comments And Links
(visible only on this page)
=== on 24 Nov 2014, 19:27:21 Nicolas Peru wrote:
\[~ann.campbell.2]This is not relevant since junit 4 might be worth mentionning that it only concerns Junit 3 way of writing tests.
=== on 1 Dec 2014, 17:22:33 Ann Campbell wrote:
done [~nicolas.peru]
endif::env-github,rspecator-view[]