Skip to content

Commit

Permalink
Fix test failures with Java 17 + Gradle 7.6
Browse files Browse the repository at this point in the history
Prior to version 7.5, Gradle added some `--add-opens` flags,
that hid potential issues with tests, for Java 16+.

gradle/gradle#20999
  • Loading branch information
leonard84 committed Feb 16, 2023
1 parent c83ec47 commit 4d7be3a
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 21 deletions.
Expand Up @@ -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);
}
}
Expand Up @@ -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
}
Expand Down Expand Up @@ -404,4 +404,3 @@ class ConditionEvaluation extends EmbeddedSpecification {
def eat(args) { args }
}
}
Expand Up @@ -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 """
Expand Down Expand Up @@ -903,4 +903,3 @@ false
String toString() { "p" }
}
}

Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand All @@ -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
Expand All @@ -246,7 +262,11 @@ class JavaSpies extends Specification {
}
}
static class Person {
interface Being {
}
static class Person implements Being {
String name
int age
Expand Down
Expand Up @@ -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"() {
Expand Down
Expand Up @@ -16,6 +16,7 @@

package org.spockframework.spring.docs

import spock.lang.IgnoreIf
import spock.lang.Specification

import javax.inject.Named
Expand Down Expand Up @@ -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 {
}

0 comments on commit 4d7be3a

Please sign in to comment.