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

TestsShouldNotBePublic should not reduce visibility if test classes are extended #309

Closed
timtebeek opened this issue Feb 7, 2023 · 7 comments · Fixed by #524
Closed
Assignees
Labels
bug Something isn't working

Comments

@timtebeek
Copy link
Contributor

TestsShouldNotBePublic should not reduce visibility of classes used in subsequent test classes, even if those are in Groovy. As seen in openrewrite/rewrite#2800.

Compilation fails as JavaTypeSignatureBuilderTest and JavaTypeMappingTest should not have reduced visibility for Groovy.

/home/tim/Documents/workspace/rewrite/rewrite-groovy/src/test/java/org/openrewrite/groovy/GroovyTypeSignatureBuilderTest.java:24: error: JavaTypeSignatureBuilderTest is not public in org.openrewrite.java; cannot be accessed from outside package
import org.openrewrite.java.JavaTypeSignatureBuilderTest;
                           ^
/home/tim/Documents/workspace/rewrite/rewrite-groovy/src/test/java/org/openrewrite/groovy/GroovyTypeSignatureBuilderTest.java:35: error: JavaTypeSignatureBuilderTest is not public in org.openrewrite.java; cannot be accessed from outside package
public class GroovyTypeSignatureBuilderTest implements JavaTypeSignatureBuilderTest {
                                                       ^
/home/tim/Documents/workspace/rewrite/rewrite-groovy/src/test/java/org/openrewrite/groovy/GroovyTypeMappingTest.java:23: error: JavaTypeMappingTest is not public in org.openrewrite.java; cannot be accessed from outside package
import org.openrewrite.java.JavaTypeMappingTest;
                           ^
/home/tim/Documents/workspace/rewrite/rewrite-groovy/src/test/java/org/openrewrite/groovy/GroovyTypeMappingTest.java:34: error: JavaTypeMappingTest is not public in org.openrewrite.java; cannot be accessed from outside package
class GroovyTypeMappingTest implements JavaTypeMappingTest {
                                       ^
warning: unknown enum constant When.MAYBE
  reason: class file for javax.annotation.meta.When not found
4 errors
4 warnings

Might be fairly niche; as such I would not consider it high priority for now.

@timtebeek
Copy link
Contributor Author

Also occurs for Java classes that are extended: openrewrite/rewrite-kubernetes#41

@timtebeek
Copy link
Contributor Author

And also for method access in GradleWrapperTest: openrewrite/rewrite#2948

@mkonietzka
Copy link

Reducing visibilty also breaks constructs like

import com.example.SomeGatewayIT;

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({SomeGatewayIT.class})
public class BranchITSuite {
}

SomeGatewayIT cannot be referenced when it becomes non public.

@timo-abele
Copy link
Contributor

I think I found another one, someone called their @AfterEach annotated method "finalize", (unwittingly?) overriding Object.finalize. As overriding must not reduce visibility, this breaks the build.

@mbruggmann
Copy link

mbruggmann commented Jun 13, 2024

We ran into this as well. Here is a testcase to reproduce it:

  @Test
  void baseclassForTestsNeedsToStayPublic() {
    rewriteRun(
        spec -> spec.recipe(new TestsShouldNotBePublic(true)),
        java(
            // base class for tests should stay public
            """
            package com.hello;

            import org.junit.jupiter.api.BeforeEach;

            public class MyTestBase {
              @BeforeEach
              void setUp() {
              }
            }
            """),
        java(
            // test class extends base class from another package
            """
            package com.world;

            import com.hello.MyTestBase;
            import org.junit.jupiter.api.Test;

            class MyTest extends MyTestBase {
              @Test
              void isWorking() {
              }
            }
            """));
  }

@timtebeek
Copy link
Contributor Author

Thanks for the helpful runnable test; I've created this PR which addresses the issue and welcome a review

@timtebeek timtebeek self-assigned this Jun 13, 2024
timtebeek added a commit that referenced this issue Jun 13, 2024
…ic` (#524)

* Do not reduce visibility on extended classes

Fixes #309

* Update src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@mbruggmann
Copy link

@timtebeek Awesome, that looks like a good fix, thanks for implementing it 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
4 participants