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

@SpringBootTest with spock and spring boot 3.0.0 doesn't load application context correctly #33376

Closed
mariopagano opened this issue Nov 27, 2022 · 3 comments
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid

Comments

@mariopagano
Copy link

Using spring boot 3.0.0 and the following spock dependencies:

              <dependency>
			<groupId>org.apache.groovy</groupId>
			<artifactId>groovy</artifactId>
			<version>4.0.6</version>
			<type>pom</type>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.spockframework</groupId>
			<artifactId>spock-core</artifactId>
			<version>2.3-groovy-4.0</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.spockframework</groupId>
			<artifactId>spock-spring</artifactId>
			<version>2.3-groovy-4.0</version>
			<scope>test</scope>
		</dependency>

sampleTestIT.groovy test class:

package com.example.spocktest

import org.spockframework.util.Assert
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import spock.lang.Specification

@SpringBootTest(classes = [SpockTestApplication])
class sampleTestIT extends Specification {
    @Autowired
    TestRestTemplate testRestTemplate

    @Autowired
    SampleService sampleService;

    def 'app context'() {
        expect:
        Assert.notNull(testRestTemplate)
    }

    def 'app context2'() {
        expect:
        Assert.notNull(sampleService)
    }
}

Output:

Condition failed with Exception:

Assert.notNull(testRestTemplate)
|      |       |
|      |       null
|      org.spockframework.util.InternalSpockError: argument is null
|      	at org.spockframework.util.Assert.notNull(Assert.java:35)
|      	at org.spockframework.util.Assert.notNull(Assert.java:30)
|      	at com.example.spocktest.sampleTestIT.app context(sampleTestIT.groovy:19)
class org.spockframework.util.Assert


	at com.example.spocktest.sampleTestIT.app context(sampleTestIT.groovy:19)
Caused by: org.spockframework.util.InternalSpockError: argument is null
	at org.spockframework.util.Assert.notNull(Assert.java:35)
	at org.spockframework.util.Assert.notNull(Assert.java:30)
	... 1 more


Condition failed with Exception:

Assert.notNull(sampleService)
|      |       |
|      |       null
|      org.spockframework.util.InternalSpockError: argument is null
|      	at org.spockframework.util.Assert.notNull(Assert.java:35)
|      	at org.spockframework.util.Assert.notNull(Assert.java:30)
|      	at com.example.spocktest.sampleTestIT.app context2(sampleTestIT.groovy:25)
class org.spockframework.util.Assert


	at com.example.spocktest.sampleTestIT.app context2(sampleTestIT.groovy:25)
Caused by: org.spockframework.util.InternalSpockError: argument is null
	at org.spockframework.util.Assert.notNull(Assert.java:35)
	at org.spockframework.util.Assert.notNull(Assert.java:30)
	... 1 more

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 27, 2022
@wilkinsona
Copy link
Member

I'm afraid this is out of Spring Boot's control. Spock's Spring Extension tries to use org.springframework.test.util.MetaAnnotationUtils which was deprecated in Spring Framework 5.3 and has been removed in 6.0. This prevents it from finding the @BootstrapWith annotation with which @SpringBootTest is meta-annotated. You can work around the problem by using @ContextConfiguration directly on your test class. Spock's Spring Extension is able to find this annotation.

This has already been reported to the Spock Team: spockframework/spock#1539.

@wilkinsona wilkinsona closed this as not planned Won't fix, can't repro, duplicate, stale Nov 28, 2022
@wilkinsona wilkinsona added status: invalid An issue that we don't feel is valid for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 28, 2022
@mohankowsu
Copy link

dependencies are not compatible

@jamesdh
Copy link

jamesdh commented May 17, 2023

FWIW, this issue was fixed in spockframework/spock#1541. A formal release has not been made yet that includes it, but you can still get the fix using their 2.4-M1 milestone release. Or you can work around it by simply adding the @ContextConfiguration annotation to your test/spec that @wilkinsona mentioned above. .

j-sandy added a commit to j-sandy/kork that referenced this issue Dec 18, 2024
… spring security from 5.x to 6.x and ninja-squad upgrade with spring boot upgrade to 3.x

While upgrading spring security from 5.x to 6.x, encountered the below error:
```
Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.ok request(GenericExceptionHandlersMvcSpec.groovy:42)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.not found(GenericExceptionHandlersMvcSpec.groovy:50)

Cannot invoke method postForEntity() on null object
java.lang.NullPointerException: Cannot invoke method postForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.method not supported(GenericExceptionHandlersMvcSpec.groovy:58)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.missing request param(GenericExceptionHandlersMvcSpec.groovy:66)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.should map IllegalArgumentException as 400(GenericExceptionHandlersMvcSpec.groovy:74)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.should handle IllegalStateException(GenericExceptionHandlersMvcSpec.groovy:82)
```
The root cause of this issue is a incompatibility bug in spockframework 2.3-groovy-4.0 version.
In order to fix this issue upgraded the spockframe to 2.4-M1-groovy-4.0
spring-projects/spring-boot#33376
https://spockframework.org/spock/docs/2.4-M1/release_notes.html#_2_4_m1_2022_11_30

Also encountered below error during test execution of kork-plugins-tck module:
```
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at com.ninjasquad.springmockk.MockkContextCustomizer.customizeContext(MockkContextCustomizer.kt:21)
	at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:435)
	at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:606)
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:386)
```
In order to fix this issue, upgraded the ninja-squad dependency from 2.0.3 to 4.0.2 for the compatibility with Spring boot 3.x and Java 17+.
https://github.com/Ninja-Squad/springmockk?tab=readme-ov-file#versions-compatibility
j-sandy added a commit to j-sandy/kork that referenced this issue Dec 19, 2024
… spring security from 5.x to 6.x and ninja-squad upgrade with spring boot upgrade to 3.x

While upgrading spring security from 5.x to 6.x, encountered the below error:
```
Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.ok request(GenericExceptionHandlersMvcSpec.groovy:42)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.not found(GenericExceptionHandlersMvcSpec.groovy:50)

Cannot invoke method postForEntity() on null object
java.lang.NullPointerException: Cannot invoke method postForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.method not supported(GenericExceptionHandlersMvcSpec.groovy:58)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.missing request param(GenericExceptionHandlersMvcSpec.groovy:66)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.should map IllegalArgumentException as 400(GenericExceptionHandlersMvcSpec.groovy:74)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.should handle IllegalStateException(GenericExceptionHandlersMvcSpec.groovy:82)
```
The root cause of this issue is a incompatibility bug in spockframework 2.3-groovy-4.0 version.
In order to fix this issue upgraded the spockframe to 2.4-M1-groovy-4.0
spring-projects/spring-boot#33376
https://spockframework.org/spock/docs/2.4-M1/release_notes.html#_2_4_m1_2022_11_30

Also encountered below error during test execution of kork-plugins-tck module:
```
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at com.ninjasquad.springmockk.MockkContextCustomizer.customizeContext(MockkContextCustomizer.kt:21)
	at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:435)
	at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:606)
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:386)
```
In order to fix this issue, upgraded the ninja-squad dependency from 2.0.3 to 4.0.2 for the compatibility with Spring boot 3.x and Java 17+.
https://github.com/Ninja-Squad/springmockk?tab=readme-ov-file#versions-compatibility
j-sandy added a commit to j-sandy/kork that referenced this issue Dec 19, 2024
… spring security from 5.x to 6.x and ninja-squad upgrade with spring boot upgrade to 3.x

While upgrading spring security from 5.x to 6.x, encountered the below error:
```
Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.ok request(GenericExceptionHandlersMvcSpec.groovy:42)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.not found(GenericExceptionHandlersMvcSpec.groovy:50)

Cannot invoke method postForEntity() on null object
java.lang.NullPointerException: Cannot invoke method postForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.method not supported(GenericExceptionHandlersMvcSpec.groovy:58)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.missing request param(GenericExceptionHandlersMvcSpec.groovy:66)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.should map IllegalArgumentException as 400(GenericExceptionHandlersMvcSpec.groovy:74)

Cannot invoke method getForEntity() on null object
java.lang.NullPointerException: Cannot invoke method getForEntity() on null object
	at com.netflix.spinnaker.kork.web.exceptions.GenericExceptionHandlersMvcSpec.should handle IllegalStateException(GenericExceptionHandlersMvcSpec.groovy:82)
```
The root cause of this issue is a incompatibility bug in spockframework 2.3-groovy-4.0 version.
In order to fix this issue upgraded the spockframe to 2.4-M1-groovy-4.0
spring-projects/spring-boot#33376
https://spockframework.org/spock/docs/2.4-M1/release_notes.html#_2_4_m1_2022_11_30

Also encountered below error during test execution of kork-plugins-tck module:
```
java.lang.NoClassDefFoundError: org/springframework/beans/factory/config/InstantiationAwareBeanPostProcessorAdapter
	at java.base/java.lang.ClassLoader.defineClass1(Native Method)
	at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
	at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
	at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:862)
	at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:760)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:681)
	at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:639)
	at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
	at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
	at com.ninjasquad.springmockk.MockkContextCustomizer.customizeContext(MockkContextCustomizer.kt:21)
	at org.springframework.boot.test.context.SpringBootContextLoader$ContextCustomizerAdapter.initialize(SpringBootContextLoader.java:435)
	at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:606)
	at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:386)
```
In order to fix this issue, upgraded the ninja-squad dependency from 2.0.3 to 4.0.2 for the compatibility with Spring boot 3.x and Java 17+.
https://github.com/Ninja-Squad/springmockk?tab=readme-ov-file#versions-compatibility
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

5 participants