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

Problem with using JUnit4to5Migration #457

Closed
timtebeek opened this issue Jan 14, 2024 · 3 comments
Closed

Problem with using JUnit4to5Migration #457

timtebeek opened this issue Jan 14, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@timtebeek
Copy link
Contributor

Discussed in openrewrite/rewrite#3911

Originally posted by ErhardSiegl January 13, 2024
I'm rather new to OpenRewrite. I tried to migrate a project from Junit4 to Junit5 with org.openrewrite.java.testing.junit5.JUnit4to5Migration. The Java files worked fine, but the the Jupiter dependencies were not added to the pom.xml. When I changed the recipe in the following way it worked:

3c3
< name: org.openrewrite.java.testing.junit5.JUnit4to5Migration
---
> name: org.openrewrite.java.testing.junit5.JUnit4to5Migration1
68c68
<       onlyIfUsing: org.junit.jupiter.api.Test
---
>       onlyIfUsing: org.junit.Test
70a71,79
>       groupId: org.mockito
>       artifactId: mockito-junit-jupiter
>       version: 5.x
>       onlyIfUsing: org.mockito.runners.MockitoJUnitRunner
>       acceptTransitive: true
>   - org.openrewrite.java.dependencies.RemoveDependency:
>       groupId: org.mockito
>       artifactId: mockito-all
>   - org.openrewrite.java.dependencies.AddDependency:

It seems that the onlyIfUsing needs the original class (org.junit.Test) and not the classname which is created by the recipe (org.junit.jupiter.api.Test). Is this a bug in the recipe or did I miss anything? I use the following configuration:

            <plugin>
                <groupId>org.openrewrite.maven</groupId>
                <artifactId>rewrite-maven-plugin</artifactId>
                <version>5.17.1</version>
                <dependencies>
                    <dependency>
                        <groupId>org.openrewrite.recipe</groupId>
                        <artifactId>rewrite-migrate-java</artifactId>
                        <version>2.5.0</version>
                    </dependency>
                    <dependency>
                        <groupId>org.openrewrite.recipe</groupId>
                        <artifactId>rewrite-testing-frameworks</artifactId>
                        <version>2.1.5</version>
                    </dependency>
                </dependencies>
            </plugin>

and start it with mvn rewrite:run -Drewrite.activeRecipes=org.openrewrite.java.testing.junit5.JUnit4to5Migration1
rewrite.yml.txt

@timtebeek timtebeek added the bug Something isn't working label Jan 14, 2024
@timtebeek timtebeek transferred this issue from openrewrite/rewrite Jan 14, 2024
@timtebeek
Copy link
Contributor Author

Hi @ErhardSiegl ; From the above it seems we might want to change the UseMockitoExtension recipe as defined here

type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.junit5.UseMockitoExtension
displayName: Use Mockito JUnit Jupiter extension
description: Migrate uses of `@RunWith(MockitoJUnitRunner.class)` (and similar annotations) to `@ExtendWith(MockitoExtension.class)`.
tags:
- testing
- junit
- mockito
recipeList:
- org.openrewrite.java.testing.mockito.Mockito1to4Migration
- org.openrewrite.java.testing.mockito.MockitoJUnitRunnerSilentToExtension
- org.openrewrite.java.testing.junit5.RunnerToExtension:
runners:
- org.mockito.runners.MockitoJUnitRunner
- org.mockito.junit.MockitoJUnitRunner
- org.mockito.runners.MockitoJUnit44Runner
- org.mockito.junit.MockitoJUnit44Runner
extension: org.mockito.junit.jupiter.MockitoExtension

Or maybe even better: add the above to mockito.yml such that it's included as part of UseMockitoExtension. Would you want to get credit for that change by opening a pull request here?

@ErhardSiegl
Copy link

Hi @timtebeek,
I'd rather not do the pull request since I'm unsure about the consequences and how to test the changes apart from my code.
I still don't understand the onlyIfUsing: org.junit.jupiter.api.Test. According to the examples in mockito.yml and junit5.yml this seems to be correct. However I had to change it to onlyIfUsing: org.junit.Test to make it work.
The

>   - org.openrewrite.java.dependencies.RemoveDependency:
>       groupId: org.mockito
>       artifactId: mockito-all

is apparently not included in the recipes. But maybe mockito-all was not the best choice in the first place. My code is a couple of years old.

@timtebeek
Copy link
Contributor Author

Hi @ErhardSiegl ; not problem at all; I've applied part of your suggested changes in 216983b just now.

The onlyIfUsing looks at the lossless semantic tree type elements to see if the argument class is used in the current tree before adding a dependency. That conditionality allows recipes to always run, but only make changes when those types are used. It is sensitive to recipe ordering, and I suspect you had trouble coming from Mockito 1.x here, as evidenced by the use of mockito-all.

To answer your question about how recipe changes are tested, you can have a look at this example test that's part of the Mockito and JUnit Jupiter migration.

class RunnerToExtensionTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "junit-4.13", "mockito-all-1.10"))
.recipe(new RunnerToExtension(
List.of("org.mockito.runners.MockitoJUnitRunner"),
"org.mockito.junit.jupiter.MockitoExtension"
)
);
}
@DocumentExample
@Test
void mockito() {
rewriteRun(
//language=java
java(
"""
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class MyTest {
}
""",
"""
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
@ExtendWith(MockitoExtension.class)
public class MyTest {
}
"""
)
);
}
}

Similar tests can be added if you feel there are cases left uncovered. Thanks for reporting your issue and suggested improvements!

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
Development

No branches or pull requests

2 participants