From 1847d7557f6cacbed0d09bdf5ed89c986f3b5fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leonard=20Br=C3=BCnings?= Date: Fri, 26 Aug 2022 18:37:53 +0200 Subject: [PATCH] Fix test failures with Java 17 + Gradle 7.6 Prior to version 7.5, Gradle added some `--add-opens` flags, that hid potential issues with tests, for Java 16+. https://github.com/gradle/gradle/pull/20999 --- .../mock/CannotCreateMockException.java | 2 +- .../condition/ConditionEvaluation.groovy | 3 +-- .../smoke/condition/ConditionRendering.groovy | 9 +++---- .../mock/GroovySpiesThatAreGlobal.groovy | 6 +++-- .../smoke/mock/JavaSpies.groovy | 26 ++++++++++++++++--- .../spring/MockInjectionExample.groovy | 25 ++++++++++++------ .../spring/docs/MockExamples.groovy | 5 ++++ 7 files changed, 55 insertions(+), 21 deletions(-) diff --git a/spock-core/src/main/java/org/spockframework/mock/CannotCreateMockException.java b/spock-core/src/main/java/org/spockframework/mock/CannotCreateMockException.java index ae87ea9fa4..350d597b16 100755 --- a/spock-core/src/main/java/org/spockframework/mock/CannotCreateMockException.java +++ b/spock-core/src/main/java/org/spockframework/mock/CannotCreateMockException.java @@ -32,6 +32,6 @@ public CannotCreateMockException(Class mockType, Throwable cause) { } public CannotCreateMockException(Class mockType, String message, Throwable cause) { - super(String.format("Cannot create mock for %s %s", mockType, message), cause); + super(String.format("Cannot create mock for %s%s", mockType, message), cause); } } diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy index b9620e677d..aa10f1822f 100755 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionEvaluation.groovy @@ -209,7 +209,7 @@ class ConditionEvaluation extends EmbeddedSpecification { def "PropertyExpression"() { expect: - [1, 2, 3].size == 3 + new Person(age: 3).age == 3 (new Properties().next.next.next.x = 10) == 10 Integer.MIN_VALUE < Integer.MAX_VALUE } @@ -404,4 +404,3 @@ class ConditionEvaluation extends EmbeddedSpecification { def eat(args) { args } } } - diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionRendering.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionRendering.groovy index cb8e4fac6f..644973957b 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionRendering.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ConditionRendering.groovy @@ -488,13 +488,13 @@ null def "PropertyExpression"() { expect: isRendered """ -a.size == null -| | | -| 1 false +a.empty == true +| | | +| false false [9] """, { def a = [9] - assert a.size == null + assert a.empty == true } isRendered """ @@ -903,4 +903,3 @@ false String toString() { "p" } } } - diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/mock/GroovySpiesThatAreGlobal.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/mock/GroovySpiesThatAreGlobal.groovy index 5e48bef018..6a73819ea4 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/mock/GroovySpiesThatAreGlobal.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/mock/GroovySpiesThatAreGlobal.groovy @@ -15,6 +15,7 @@ package org.spockframework.smoke.mock import spock.lang.* +import spock.util.environment.Jvm import java.lang.reflect.Modifier import java.util.regex.Pattern @@ -173,7 +174,8 @@ class GroovySpiesThatAreGlobal extends Specification { } def "mock static method"() { - GroovySpy(Collections, global: true) + // Use objenesis to create instance for Java 17 and later + GroovySpy(Collections, global: true, useObjenesis: Jvm.current.java17Compatible) when: Collections.emptyList() @@ -186,7 +188,7 @@ class GroovySpiesThatAreGlobal extends Specification { } def "mock dynamic static method"() { - GroovySpy(Collections, global: true) + GroovySpy(Collections, global: true, useObjenesis: Jvm.current.java17Compatible) when: Collections.foo() diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/mock/JavaSpies.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/mock/JavaSpies.groovy index 25b4f02af4..607958791b 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/mock/JavaSpies.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/mock/JavaSpies.groovy @@ -16,6 +16,7 @@ package org.spockframework.smoke.mock import org.spockframework.mock.CannotCreateMockException import org.spockframework.runtime.SpockException +import org.spockframework.util.SpockDocLinks import spock.lang.* class JavaSpies extends Specification { @@ -204,10 +205,11 @@ class JavaSpies extends Specification { @Issue("https://github.com/spockframework/spock/issues/822") def "inferred type is ignored for instance mocks"() { when: - List list = new ArrayList<>() - List second = Spy(list) + Being person = new Person() + Being second = Spy(person) then: + second instanceof Person noExceptionThrown() } @@ -223,6 +225,20 @@ class JavaSpies extends Specification { thrown(SpockException) } + @Requires( + value = { jvm.java17Compatible }, + reason = "Only happens on 17+, without an explicit --add-opens" + ) + def "known issue copy fields"() { + when: + List second = Spy(new ArrayList()) + + then: + CannotCreateMockException e = thrown() + e.message == "Cannot create mock for class java.util.ArrayList. Cannot copy fields.\n" + + SpockDocLinks.SPY_ON_JAVA_17.link + } + static class Constructable { int arg1 int arg2 @@ -246,7 +262,11 @@ class JavaSpies extends Specification { } } - static class Person { + interface Being { + + } + + static class Person implements Being { String name int age diff --git a/spock-spring/src/test/groovy/org/spockframework/spring/MockInjectionExample.groovy b/spock-spring/src/test/groovy/org/spockframework/spring/MockInjectionExample.groovy index a1e81e91a0..429d25c21c 100644 --- a/spock-spring/src/test/groovy/org/spockframework/spring/MockInjectionExample.groovy +++ b/spock-spring/src/test/groovy/org/spockframework/spring/MockInjectionExample.groovy @@ -17,30 +17,39 @@ package org.spockframework.spring import org.spockframework.mock.MockUtil +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.test.context.ContextConfiguration +import spock.lang.IgnoreIf import spock.lang.Specification import javax.inject.Named -import org.springframework.beans.factory.annotation.Autowired -import org.springframework.test.context.ContextConfiguration - +@IgnoreIf( + value = { jvm.java17Compatible }, + reason = "Spring 4s reflection doesn't work on 17+" +) @ContextConfiguration(locations = "MockExamples-context.xml") class MockInjectionExample extends Specification { - @Autowired @Named('serviceMock') + @Autowired + @Named('serviceMock') IService1 serviceMock - @Autowired @Named('serviceStub') + @Autowired + @Named('serviceStub') IService1 serviceStub - @Autowired @Named('serviceSpy') + @Autowired + @Named('serviceSpy') IService2 serviceSpy - @Autowired @Named('service2') + @Autowired + @Named('service2') IService2 service2 - @Autowired @Named('nonMock') + @Autowired + @Named('nonMock') ArrayList concreteSpy; def "Injected services are mocks"() { diff --git a/spock-spring/src/test/groovy/org/spockframework/spring/docs/MockExamples.groovy b/spock-spring/src/test/groovy/org/spockframework/spring/docs/MockExamples.groovy index 4df7db0f91..c043edd2df 100644 --- a/spock-spring/src/test/groovy/org/spockframework/spring/docs/MockExamples.groovy +++ b/spock-spring/src/test/groovy/org/spockframework/spring/docs/MockExamples.groovy @@ -16,6 +16,7 @@ package org.spockframework.spring.docs +import spock.lang.IgnoreIf import spock.lang.Specification import javax.inject.Named @@ -81,6 +82,10 @@ class DetachedJavaConfigExample extends MockExampleBase { } +@IgnoreIf( + value = { jvm.java17Compatible }, + reason = "Spring 4s reflection doesn't work on 17+" +) @ContextConfiguration(locations = "classpath:org/spockframework/spring/docs/MockDocu-context.xml") class DetachedXmlExample extends MockExampleBase { }