Skip to content

Commit

Permalink
Fixes #2204: Ignore Groovy methods annotated with Internal (#2207)
Browse files Browse the repository at this point in the history
Starting from Groovy 3.0, compiler-generated methods such as
`getMetaClass()` are no longer marked synthetic, therefore ByteBuddy
stopped automatically ignoring them. Instead they are now annotated with
`@groovy.transform.Internal`. Update `isGroovyMethod()` to also check
this new annotation.
  • Loading branch information
rtandy committed Feb 15, 2021
1 parent c2488e0 commit 8477455
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gradle/dependencies.gradle
Expand Up @@ -35,6 +35,8 @@ libraries.osgi = 'org.osgi:osgi.core:8.0.0'
libraries.equinox = 'org.eclipse.platform:org.eclipse.osgi:3.16.100'
libraries.bndGradle = 'biz.aQute.bnd:biz.aQute.bnd.gradle:5.2.0'

libraries.groovy = 'org.codehaus.groovy:groovy:3.0.7'

def kotlinVersion = '1.4.30'
libraries.kotlin = [
version: kotlinVersion,
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Expand Up @@ -5,6 +5,7 @@ plugins {
include("deprecatedPluginsTest",
"inline",
"extTest",
"groovyTest",
"kotlinTest",
"kotlinReleaseCoroutinesTest",
"android",
Expand Down
Expand Up @@ -226,7 +226,8 @@ private <T> Collection<Class<? super T>> getAllTypes(Class<T> type) {
}

private static ElementMatcher<MethodDescription> isGroovyMethod() {
return isDeclaredBy(named("groovy.lang.GroovyObjectSupport"));
return isDeclaredBy(named("groovy.lang.GroovyObjectSupport"))
.or(isAnnotatedWith(named("groovy.transform.Internal")));
}

private boolean isComingFromJDK(Class<?> type) {
Expand Down
11 changes: 11 additions & 0 deletions subprojects/groovyTest/groovyTest.gradle
@@ -0,0 +1,11 @@
apply plugin: 'groovy'

description = "Integration test for using Mockito from Groovy."

apply from: "$rootDir/gradle/dependencies.gradle"

dependencies {
testCompile project(":")
testCompile libraries.groovy
testCompile libraries.junit4
}
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2021 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() { }
}
}

0 comments on commit 8477455

Please sign in to comment.