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

Add failing test for CleanupMockitoImports #336

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -40,4 +40,5 @@ dependencies {

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-groovy")
testImplementation("org.junit-pioneer:junit-pioneer:2.0.1")
}
Original file line number Diff line number Diff line change
@@ -16,8 +16,10 @@
package org.openrewrite.java.testing.mockito;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.ExpectedToFail;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
@@ -30,7 +32,7 @@ class CleanupMockitoImportsTest implements RewriteTest {
public void defaults(RecipeSpec spec) {
spec
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "mockito-all-1.10.19"))
.classpathFromResources(new InMemoryExecutionContext(), "mockito-core-3.12.4"))
.recipe(new CleanupMockitoImports());
}

@@ -207,6 +209,123 @@ public class MockitoArgumentMatchersTest {
)
);
}

@Test
@ExpectedToFail
@Issue("https://github.com/openrewrite/rewrite/issues/3111") // maybe not exactly the same issue
void removeUnusedArgumentMatchersOnlyWithLombok() {
//language=java
rewriteRun(
java(
"""
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor
Comment on lines +221 to +222
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These annotations are not supported in the input strings; not sure how it affects the test, but if we can replicate the issue without these that'd be better.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import lombok.RequiredArgsConstructor;
@RequiredArgsConstructor

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems like my issue is related to combination of lombok + var so that would make it hard to write a test to reproduce it :/

class MyObject {
private final String field;
String doAThing(Object other, MyObject myObject){return value;}
}
"""
),
java(
"""
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.after;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
class MyObjectTest {
@Mock
MyObject myObject;
void test() {
MyObject testObject = new MyObject("field");
when(myObject.doAThing(any(), eq(testObject))).thenReturn("testValue");
}
}
""",
"""
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
class MyObjectTest {
@Mock
MyObject myObject;
void test() {
MyObject testObject = new MyObject("field");
when(myObject.doAThing(any(), eq(testObject))).thenReturn("testValue");
}
}
"""
)
);
}

@Test
void removeUnusedArgumentMatchersOnlyWithoutLombok() {
//language=java
rewriteRun(
java(
"""
class MyObject {
private final String field;
MyObject(String field){
this.field = field;
}
String doAThing(Object other, MyObject myObject){return value;}
}
"""
),
java(
"""
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.after;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
class MyObjectTest {
@Mock
MyObject myObject;
void test() {
MyObject testObject = new MyObject("field");
when(myObject.doAThing(any(), eq(testObject))).thenReturn("testValue");
}
}
""",
"""
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
class MyObjectTest {
@Mock
MyObject myObject;
void test() {
MyObject testObject = new MyObject("field");
when(myObject.doAThing(any(), eq(testObject))).thenReturn("testValue");
}
}
"""
)
);
}
}