Skip to content

Commit

Permalink
refactor(test): upgrade spockframework to fix issue during upgrade of…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
j-sandy committed Dec 18, 2024
1 parent 0c0412b commit f402830
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.boot.web.server.LocalServerPort
import org.springframework.boot.test.web.server.LocalServerPort
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.http.HttpStatus
import org.springframework.security.config.annotation.web.builders.HttpSecurity
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
import org.springframework.security.web.SecurityFilterChain
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
import spock.lang.Specification

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestControllersConfiguration)
Expand Down Expand Up @@ -95,12 +96,12 @@ class GenericExceptionHandlersMvcSpec extends Specification {

@Configuration
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.headers().disable()
.authorizeRequests().anyRequest().permitAll()
class WebSecurityConfig implements WebMvcConfigurer {
@Bean
protected SecurityFilterChain configure(HttpSecurity http) throws Exception {
http.csrf().disable().headers().disable()
http.authorizeHttpRequests().anyRequest().permitAll()
return http.build()
}
}

Expand Down
4 changes: 2 additions & 2 deletions spinnaker-dependencies/spinnaker-dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ dependencies {
api(platform("software.amazon.awssdk:bom:${versions.awsv2}"))
api(platform("org.springframework.cloud:spring-cloud-dependencies:${versions.springCloud}"))
api(platform("io.strikt:strikt-bom:0.31.0"))
api(platform("org.spockframework:spock-bom:2.3-groovy-4.0"))
api(platform("org.spockframework:spock-bom:2.4-M1-groovy-4.0"))
api(platform("com.oracle.oci.sdk:oci-java-sdk-bom:3.21.0"))
api(platform("org.testcontainers:testcontainers-bom:1.19.8"))
api(platform("io.arrow-kt:arrow-stack:${versions.arrow}"))
Expand Down Expand Up @@ -110,7 +110,7 @@ dependencies {
api("com.nimbusds:nimbus-jose-jwt:9.37.2")
api("com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0")
api("com.nhaarman:mockito-kotlin:1.6.0")
api("com.ninja-squad:springmockk:2.0.3")
api("com.ninja-squad:springmockk:4.0.2") //Compatible with Spring boot 3.x and Java 17+ https://github.com/Ninja-Squad/springmockk?tab=readme-ov-file#versions-compatibility
api("com.squareup.retrofit2:converter-jackson:${versions.retrofit2}")
api("com.squareup.retrofit2:retrofit-mock:${versions.retrofit2}")
api("com.squareup.retrofit2:retrofit:${versions.retrofit2}")
Expand Down

0 comments on commit f402830

Please sign in to comment.