diff --git a/settings.gradle.kts b/settings.gradle.kts index 5aad31c670..313149df98 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -6,6 +6,7 @@ include("inline", "proxy", "extTest", "groovyTest", + "groovyInlineTest", "kotlinTest", "kotlinReleaseCoroutinesTest", "android", diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/BytecodeGenerator.java b/src/main/java/org/mockito/internal/creation/bytebuddy/BytecodeGenerator.java index 392392d2d9..5040c17573 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/BytecodeGenerator.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/BytecodeGenerator.java @@ -4,6 +4,12 @@ */ package org.mockito.internal.creation.bytebuddy; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.matcher.ElementMatcher; + +import static net.bytebuddy.matcher.ElementMatchers.*; +import static net.bytebuddy.matcher.ElementMatchers.named; + public interface BytecodeGenerator { Class mockClass(MockFeatures features); @@ -13,4 +19,16 @@ public interface BytecodeGenerator { void mockClassStatic(Class type); default void clearAllCaches() {} + + static ElementMatcher isGroovyMethod(boolean inline) { + ElementMatcher.Junction matcher = + isDeclaredBy(named("groovy.lang.GroovyObjectSupport")) + .or(isAnnotatedWith(named("groovy.transform.Internal"))); + if (inline) { + return matcher.or( + named("$getStaticMetaClass").and(returns(named("groovy.lang.MetaClass")))); + } else { + return matcher; + } + } } diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java index 10b73c81a2..72ab81adbd 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/InlineBytecodeGenerator.java @@ -117,15 +117,19 @@ public InlineBytecodeGenerator( .or(isEquals()) .or(isDefaultFinalizer()))) .and( - not( - isDeclaredBy(nameStartsWith("java.")) + not(isDeclaredBy(nameStartsWith("java.")) .and( - isPackagePrivate()))), + isPackagePrivate())) + .and( + not( + BytecodeGenerator + .isGroovyMethod( + true)))), Advice.withCustomMapping() .bind(MockMethodAdvice.Identifier.class, identifier) .to(MockMethodAdvice.class)) .method( - isStatic(), + isStatic().and(not(BytecodeGenerator.isGroovyMethod(true))), Advice.withCustomMapping() .bind(MockMethodAdvice.Identifier.class, identifier) .to(MockMethodAdvice.ForStatic.class)) diff --git a/src/main/java/org/mockito/internal/creation/bytebuddy/SubclassBytecodeGenerator.java b/src/main/java/org/mockito/internal/creation/bytebuddy/SubclassBytecodeGenerator.java index f67fdfc5a6..d27b5f82c3 100644 --- a/src/main/java/org/mockito/internal/creation/bytebuddy/SubclassBytecodeGenerator.java +++ b/src/main/java/org/mockito/internal/creation/bytebuddy/SubclassBytecodeGenerator.java @@ -12,12 +12,9 @@ import static net.bytebuddy.matcher.ElementMatchers.any; import static net.bytebuddy.matcher.ElementMatchers.hasParameters; import static net.bytebuddy.matcher.ElementMatchers.hasType; -import static net.bytebuddy.matcher.ElementMatchers.isAnnotatedWith; -import static net.bytebuddy.matcher.ElementMatchers.isDeclaredBy; import static net.bytebuddy.matcher.ElementMatchers.isEquals; import static net.bytebuddy.matcher.ElementMatchers.isHashCode; import static net.bytebuddy.matcher.ElementMatchers.isPackagePrivate; -import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.returns; import static net.bytebuddy.matcher.ElementMatchers.whereAny; import static org.mockito.internal.util.StringUtil.join; @@ -29,9 +26,7 @@ import java.lang.reflect.Modifier; import java.lang.reflect.Type; import java.util.ArrayList; -import java.util.Collection; import java.util.Iterator; -import java.util.LinkedList; import java.util.Random; import net.bytebuddy.ByteBuddy; import net.bytebuddy.description.method.MethodDescription; @@ -224,7 +219,7 @@ public Class mockClass(MockFeatures features) { byteBuddy .subclass(features.mockedType) .name(name) - .ignoreAlso(isGroovyMethod()) + .ignoreAlso(BytecodeGenerator.isGroovyMethod(false)) .annotateType( features.stripAnnotations ? new Annotation[0] @@ -282,22 +277,6 @@ public void mockClassConstruction(Class type) { "The subclass byte code generator cannot create construction mocks"); } - private Collection> getAllTypes(Class type) { - Collection> supertypes = new LinkedList<>(); - supertypes.add(type); - Class superType = type; - while (superType != null) { - supertypes.add(superType); - superType = superType.getSuperclass(); - } - return supertypes; - } - - private static ElementMatcher isGroovyMethod() { - return isDeclaredBy(named("groovy.lang.GroovyObjectSupport")) - .or(isAnnotatedWith(named("groovy.transform.Internal"))); - } - private boolean isComingFromJDK(Class type) { // Comes from the manifest entry : // Implementation-Title: Java Runtime Environment diff --git a/subprojects/groovyInlineTest/groovyInlineTest.gradle b/subprojects/groovyInlineTest/groovyInlineTest.gradle new file mode 100644 index 0000000000..1618ac6009 --- /dev/null +++ b/subprojects/groovyInlineTest/groovyInlineTest.gradle @@ -0,0 +1,11 @@ +apply plugin: 'groovy' + +description = "Integration test for using mockito-inline with Groovy." + +apply from: "$rootDir/gradle/dependencies.gradle" + +dependencies { + testImplementation project(":inline") + testImplementation libraries.groovy + testImplementation libraries.junit4 +} diff --git a/subprojects/groovyInlineTest/src/test/groovy/org/mockito/groovy/GroovyMockitoTest.groovy b/subprojects/groovyInlineTest/src/test/groovy/org/mockito/groovy/GroovyMockitoTest.groovy new file mode 100644 index 0000000000..c327d0e1bb --- /dev/null +++ b/subprojects/groovyInlineTest/src/test/groovy/org/mockito/groovy/GroovyMockitoTest.groovy @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 Mockito contributors + * This program is made available under the terms of the MIT License. + */ +package org.mockito.groovy + +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.InjectMocks +import org.mockito.Mock +import org.mockito.junit.MockitoJUnitRunner + +import static org.mockito.Mockito.verify + +@RunWith(MockitoJUnitRunner) +class GroovyMockitoTest { + + @Mock Helper helper + @InjectMocks ClassUnderTest classUnderTest + + /** + * Test that the Groovy class under test can call methods on a mocked Groovy + * helper class. + */ + @Test + void testCallGroovyFromGroovy() { + classUnderTest.methodUnderTest() + verify(helper).helperMethod() + } + + static class ClassUnderTest { + private final Helper helper + + ClassUnderTest(Helper helper) { + this.helper = helper + } + + void methodUnderTest() { + helper.helperMethod() + } + } + + static class Helper { + void helperMethod() { } + } +}