Skip to content
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

Exclude inner classes annotated with @Nested from ClassCanBeStatic rule #956

Closed
uhafner opened this issue Feb 28, 2018 · 3 comments
Closed

Comments

@uhafner
Copy link

uhafner commented Feb 28, 2018

Please answer these questions before submitting your issue. Thanks!

What version of Error Prone are you using?

2.2.0 (with maven.plugin 2.8.2)

Does this issue reproduce with the latest release?

Yes

What did you do?

JUnit 5 provides nested tests using an inner class (not static): See Example at https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested

If I use the rule ClassCanBeStatic on all my classes, then all those tests are flagged.

What did you expect to see?

An inner class with the annotation @org.junit.jupiter.api.Nested should not be flagged as warning.

What did you see instead?

[WARNING] /Users/hafner/Development/jenkins/workspace/Pipeline/src/test/java/edu/hm/hafner/analysis/parser/dry/CodeDuplicationTest.java:[64,5] [ClassCanBeStatic] Inner class is non-static but does not reference enclosing class

@bobtiernay-okta
Copy link

Sadness :(

@dmdashenkov
Copy link

dmdashenkov commented Apr 22, 2019

Hi. Is there any chance this issue is going to be fixed?

With the latest update, this now is also relevant to some production code. For example, I have a number of private methods marked with an annotation. These methods are invoked via reflection. Currently, error-prone treats them as unused.

The same applies to the JUnit 5 factory methods. Those are not annotated with any kind of a special annotation. Instead, they are referenced by the name from method annotations in the same class.

@mohanaraosv
Copy link

mohanaraosv commented Aug 5, 2020

This problem still exists [UnusedMethod] Private method 'errorEnumsProvider' which returns Stream is never used. For @ParameterizedTest

copybara-service bot pushed a commit that referenced this issue Jan 23, 2023
Exclude inner classes annotated with JUnit's `@Nested` annotation from the `ClassCanBeStatic` check. JUnit requires them to not be `static`.

From the [JUnit docs](https://junit.org/junit5/docs/current/user-guide/#writing-tests-nested):

> Only non-static nested classes (i.e. inner classes) can serve as `@Nested` test classes.

Fixes #956.

Fixes #3654

FUTURE_COPYBARA_INTEGRATE_REVIEW=#3654 from ljrmorgan:junit_nested_test_classes_cannot_be_static 3a5bd67
PiperOrigin-RevId: 503544057
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants