Java rule S5786 (added in SONARJAVA-3362) does not account for abstract test classes or interfaces.
Product
SonarQube 8.6.1
Example
import static org.junit.jupiter.api.Assertions.assertNotNull;
import org.junit.jupiter.api.Test;
public abstract class ExampleTestHelper {
protected abstract Object delegate();
@Test
protected void test_toString() {
assertNotNull(this.delegate().toString());
}
protected abstract void test_implSpecific();
}
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class ExampleTest extends ExampleTestHelper {
@Override
protected String delegate() {
return "test";
}
@Test
@Override
protected void test_implSpecific() {
assertEquals("test", this.delegate());
}
}
3 false positives occur in the above example:
-
public abstract class ExampleTestHelper
→ “Remove this ‘public’ modifier.” -
protected void test_toString()
→ “Remove this ‘protected’ modifier.” -
@Override protected void test_implSpecific()
→ “Remove this ‘protected’ modifier.”
Reducing visibility for JUnit5 test classes is great, and I want to keep this rule enabled, but need the ability to refactor test code into abstract classes and/or interfaces. Note: the ExampleTestHelper
class have been an interface with a default @Test
method but still yields the same false-positive.
It would also be great to see a warning when a @Test
method is overridden without a @Test
annotation.