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

Don't remove mockStatic() calls in resources of try-with-resources #426

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx)

if (MOCKED_STATIC_MATCHER.matches(mi)) {
determineTestGroups();
if (getCursor().firstEnclosing(J.Assignment.class) == null) {
if (!getCursor().getPath(o -> o instanceof J.Assignment || o instanceof J.Try.Resource).hasNext()) {
//noinspection DataFlowIssue
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/META-INF/rewrite/mockito.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ recipeList:
- org.openrewrite.java.testing.mockito.CleanupMockitoImports
- org.openrewrite.java.testing.mockito.MockUtilsToStatic
- org.openrewrite.java.testing.junit5.MockitoJUnitToMockitoExtension
- org.openrewrite.java.testing.mockito.ReplacePowerMockito
# https://github.com/openrewrite/rewrite-testing-frameworks/issues/360
# - org.openrewrite.java.testing.mockito.ReplacePowerMockito
knutwannheden marked this conversation as resolved.
Show resolved Hide resolved
- org.openrewrite.java.dependencies.AddDependency:
groupId: org.mockito
artifactId: mockito-junit-jupiter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,10 @@ void testSomething() { }
)
);
}

@Test
void tearDownMethodOfTestNGWithAnnotationRemainsUntouched() {
//language=java
//language=java
rewriteRun(
java(
"""
Expand Down Expand Up @@ -303,6 +304,7 @@ void testSomething() { }
)
);
}

@Test
void tearDownMethodOfTestNGHasAnnotationWithSameArgumentsAsTheTestThatCallsMockStatic() {
//language=java
Expand Down Expand Up @@ -539,7 +541,7 @@ void extensionOfPowerMockTestCaseGetsRemoved() {
public class PowerMockTestCase {}
"""
),
java(
java(
"""
import org.powermock.modules.testng.PowerMockTestCase;

Expand All @@ -548,7 +550,7 @@ public class MyPowerMockTestCase extends PowerMockTestCase {}
"""
public class MyPowerMockTestCase {}
""")
);
);
}

@Test
Expand All @@ -562,7 +564,7 @@ void extensionOfPowerMockConfigurationGetsRemoved() {
public class PowerMockConfiguration {}
"""
),
java(
java(
"""
import org.powermock.configuration.PowerMockConfiguration;

Expand All @@ -574,12 +576,38 @@ public class MyPowerMockConfiguration {}
));
}

@Test
void mockStaticInTryWithResources() {
//language=java
rewriteRun(
java(
"""
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;

import java.nio.file.Files;

import static org.mockito.Mockito.mockStatic;

class A {
@Test
void testTryWithResource() {
try (MockedStatic<Files> mocked = mockStatic(Files.class)) {
// test logic that uses mocked
}
}
}
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/358")
void doesNotExplodeOnTopLevelMethodDeclaration() {
rewriteRun(
groovy(
"def myFun() { }"
));
"def myFun() { }"
));
}
}
Loading