-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] DetachedTestCase reports abstract methods #1831
Comments
@ylexus abstract methods can be annotated if desired, so I see no reason to whitelist any abstract methods… if this was a utility, using However, I'm unsure as to why the rule flags package-private methods… JUnit only allows for public methods, so a package-private can't be a test… at most it would be a candidate for |
@jsotuyod sorry abstract methods can be annotated with what? Do you mean @SuppressWarnings? Sure we do that now, but clearly they are not detached test cases, they are template methods for subclass test cases to implement. I agree that these should really be protected. |
@ylexus I meant JUnit annotations such as If it's not meant to be a JUnit method, then it should probably be |
Hello, I saw it's a good first issue and decided to fix it. First of all, the code in original post won't generate a violation, it requires an import of Second, JUnit5 now supports package-private methods to be tests. It's not clear why can't a method have an annotation? I mean, if I add From my point of view, either this rule should stay as it is, or it should somehow take into account whether class is abstract or not. WDYT? |
This case was for a template method pattern applied to a hierarchy of unit tests. Say, a superclass asking a subclass to do something. A tool like pmd should not force me as a test designer to use or not use a particular legal pattern. Such abstract methods are legal for java and junit and are clearly not detached test cases. |
I think, first thing we need, is a example, that reproduces the problem. Is the following a complete sample of the problem? import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public abstract class BaseTest {
@Test
public void someTest() { }
@BeforeEach
abstract void setup(); // doesn't trigger DetachedTestCase because of @Before annotation
@Test
abstract void abstractTest(); // doesn't trigger DetachedTestCase
public abstract void thisShouldAlsoBeATest(); // triggers DetachtedTestCase
abstract void doSetup(); // this triggers DetachedTestCase
abstract void doSetup(int param); // doesn't trigger DetachedTestCase because of param
abstract void thisShouldBeATest(); // this triggers DetachedTestCase
protected abstract void doSetup2(); // this doesn't trigger DetachedTestCase because protected
} (Note: I didn't run it, so I'm not even sure it compiles or makes sense at all) It seems to be, that we either could say: we don't report abstract methods - then we have a false-negative for Is there any reliable way, we can determine, whether a method is indeed a detached test case? |
Why do we have a false negative on thisShouldBeATest if we do not report abstract methods? On this class, it’s not a test. It’s only a test on the subclass implementing this class, if that class implements this test method. |
First of all, @adangel is correct in terms of an example, everything is like you said. First I wanted to say that abstract classes can be ignored by this rule but then thought about public methods that shouldn't exist in a base abstract class. If you want something outside of this package, it's better to create a utility class or helper or something else. From the other point of view, why should PMD report about anything in abstract class? Just implement your methods in a child class and deal with your problems there. But in that case if you @Override
void thisShouldBeATest then there will be no violations (as it has an annotation). If you do not override it, there will be a violation (there is no annotation but in fact this method is overriden) Actually I'm stuck. |
Hello Devs, I think an abstract method should only be implemented on a child class level as a result it is perhaps not a good idea to annotate it with any JUnit annotations. I have raised a PR for fixing this please let me know your valuable thoughts #4706 |
This is my first contribution can I be assigned as an assignee @ylexus ? |
With the solution @Debamoy provided, abstract methods are now not anymore reported. Note: this will be part of PMD 7.0.0. |
Affects PMD Version:
6.14.0
Rule:
DetachedTestCase
Description:
Reports abstract methods. They are surely not detached test cases.
Code Sample demonstrating the issue:
Running PMD through: [CLI | Ant | Maven | Gradle | Designer | Other]
Maven
The text was updated successfully, but these errors were encountered: