Skip to content

Incompatibility between RestAssured MockMvc and Spring Framework 7 breaks MOCKMVC base testsΒ #2379

@alexaugustobr

Description

@alexaugustobr

Describe the bug
When using headers on the requests and running generated contract tests in MOCKMVC mode, tests fail with a NoSuchMethodError originating from RestAssured's MockMvc module. The exception is:

java.lang.NoSuchMethodError: 'org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.header(java.lang.String, java.lang.Object[])'
    at io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.applyHeaders(MockMvcRequestSenderImpl.java:514)
    at io.restassured.module.mockmvc.internal.MockMvcRequestSenderImpl.sendRequest(MockMvcRequestSenderImpl.java:348)
    ...
    at com.example.demo.GreetingTest.validate_shouldReturnGreeting(GreetingTest.java:28)

This appears to be caused by RestAssured's MockMvc integration calling MockHttpServletRequestBuilder.header(String, Object[]), a method signature that is not present in Spring Framework 7's MockHttpServletRequestBuilder (used by Spring Boot 4). In short: RestAssured's MockMvc module is not compatible with Spring Framework 7, which prevents contract tests in MOCKMVC test mode from running.

Versions (important)

  • Spring Cloud: 2025.1.0
  • Spring Cloud Verifier: 5.0.0
  • Spring Boot: 4.0.0
  • Java 25
  • RestAssured 5.5.6 (transitive dependency via Spring Cloud Contract)

Sample
Repro repo (minimal demo):
https://github.com/alexaugustobr/spring-cloud-contract-demo-bug-rest-assured-mockmvc

Relevant snippets:

build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '4.0.0'
	id 'io.spring.dependency-management' version '1.1.7'
	id 'org.springframework.cloud.contract' version '5.0.0'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
description = 'Demo project for Spring Boot'

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(25)
	}
}

repositories {
	mavenCentral()
}

ext {
	set('springCloudVersion', "2025.1.0")
}

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-webmvc'
	testImplementation 'org.springframework.boot:spring-boot-starter-webmvc-test'
	testImplementation 'org.springframework.cloud:spring-cloud-starter-contract-verifier'
	testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

dependencyManagement {
	imports {
		mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
	}
}

contracts {

}

tasks.named('contractTest') {
	useJUnitPlatform()
}

tasks.named('test') {
	useJUnitPlatform()
}

contracts {
    testMode = 'MOCKMVC'
    baseClassForTests = 'com.example.demo.RestAssuredMockMVCBaseContractTest'
}

contracts/greeting/contract.groovy

package contracts.greeting

import org.springframework.cloud.contract.spec.Contract

Contract.make {
    description 'Should return greeting with given name'

    request {
        method GET()
        headers {
            accept(applicationJson())
        }
        url('/greeting') {
            queryParameters {
                parameter 'message': 'Alex'
            }
        }
    }

    response {
        status OK()
        headers {
            contentType(applicationJson())
        }
        body([ message: 'Hello, Alex!' ])
    }
}

RestAssuredMockMVCBaseContractTest.java

@WebMvcTest(controllers = GreetingController.class)
public abstract class RestAssuredMockMVCBaseContractTest {

    @Autowired
    private WebApplicationContext context;

    @BeforeEach
    void setUp() {
        RestAssuredMockMvc.mockMvc(
                MockMvcBuilders.webAppContextSetup(context)
                        .defaultResponseCharacterEncoding(StandardCharsets.UTF_8)
                        .build()
        );

        RestAssuredMockMvc.enableLoggingOfRequestAndResponseIfValidationFails();
    }
}

What I tried

Attempted to use CUSTOM test mode and write a custom test base, following the documentation, but I couldn't get it working. I had faced another bug related to @LocalServerPort not being injected correctly in Spring Boot 4 after adding Spring Cloud Verifier. I can share details if helpful.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions