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

JUnit 4 to 5 migration does not add the mockito-junit-jupiter dependency #640

Open
FieteO opened this issue Nov 8, 2024 · 2 comments
Open
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@FieteO
Copy link
Contributor

FieteO commented Nov 8, 2024

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v5.22.0

How are you running OpenRewrite?

I am using the command line invocation:

mvn -U org.openrewrite.maven:rewrite-maven-plugin:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-spring:RELEASE -Drewrite.activeRecipes=org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration -Drewrite.exportDatatables=true

What is the smallest, simplest way to reproduce the problem?

import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class MyClass {}
...
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

What did you expect to see?

...
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-junit-jupiter</artifactId>
      <scope>test</scope>
  </dependency>
</dependencies>

What did you see instead?

The mockito-junit-jupiter dependency is not added.

...
<dependencies>
  <dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <scope>test</scope>
  </dependency>
  <dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

Are you interested in contributing a fix to OpenRewrite?

No

@FieteO FieteO added the bug Something isn't working label Nov 8, 2024
@timtebeek
Copy link
Contributor

Thanks for the report! As a bit of context we do have a recipe step to add that dependency:

- org.openrewrite.java.dependencies.AddDependency:
groupId: org.mockito
artifactId: mockito-junit-jupiter
version: 3.x
onlyIfUsing: org.mockito.junit.jupiter.*
acceptTransitive: true

Which is part of our org.openrewrite.java.testing.mockito.Mockito1to3Migration & org.openrewrite.java.testing.mockito.Mockito1to4Migration.

You've specifically ran org.openrewrite.java.spring.boot2.SpringBoot2JUnit4to5Migration, which does include org.openrewrite.java.testing.junit5.JUnit4to5Migration.
https://github.com/openrewrite/rewrite-spring/blob/729164fd713cbd0bfce44e582b0d613f4d1965b0/src/main/resources/META-INF/rewrite/spring-boot-24.yml#L100-L113

That JUnit4to5Migration also includes UseMockitoExtension, which includes the Mockito1to4Migration:

name: org.openrewrite.java.testing.junit5.JUnit4to5Migration
displayName: JUnit Jupiter migration from JUnit 4.x
description: Migrates JUnit 4.x tests to JUnit Jupiter.
tags:
- testing
- junit
preconditions:
- org.openrewrite.java.search.DoesNotUseType:
fullyQualifiedTypeName: org.testng..*
includeImplicit: true
recipeList:
- org.openrewrite.java.testing.junit5.UseWiremockExtension
- org.openrewrite.java.testing.junit5.IgnoreToDisabled
- org.openrewrite.java.testing.junit5.ThrowingRunnableToExecutable
- org.openrewrite.java.testing.junit5.RemoveObsoleteRunners:
obsoleteRunners:
- org.junit.runners.JUnit4
- org.junit.runners.BlockJUnit4ClassRunner
- org.openrewrite.maven.RemovePluginDependency:
pluginGroupId: org.apache.maven.plugins
pluginArtifactId: maven-surefire-plugin
groupId: org.apache.maven.surefire
artifactId: surefire-junit*
- org.openrewrite.java.testing.junit5.UpgradeSurefirePlugin
- org.openrewrite.java.testing.junit5.AddHamcrestJUnitDependency
- org.openrewrite.java.testing.junit5.UseHamcrestAssertThat
- org.openrewrite.java.testing.junit5.MigrateAssumptions
- org.openrewrite.java.testing.junit5.UseMockitoExtension

@timtebeek timtebeek transferred this issue from openrewrite/rewrite-spring Nov 11, 2024
@timtebeek
Copy link
Contributor

I've moved the issue as it seems more closely related to rewrite-testing-frameworks. I wonder if running UseMockitoExtension later in JUnit4to5Migration would help the conditional onlyIfUsing: org.mockito.junit.jupiter.* in the AddDependency to detect that we should add the dependency. Probably best tried with a unit test that has a class with JUnit 4 and the Mockito runner, as well as a pom.xml, and then run JUnit4to5Migration and assert the dependency is added.

@timtebeek timtebeek added the good first issue Good for newcomers label Nov 11, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
Status: Backlog
Development

No branches or pull requests

2 participants