Spring Mock Mvc: Requests with Cookies cause "java.lang.IllegalArgumentException: argument type mismatch"

Author: miguel-figueiredoCreated Oct 13, 2025Updated Jul 10, 2026

Hi,

I'm encountering intermittent test failures when adding a cookie, with the following error: java.lang.IllegalArgumentException: argument type mismatch

The issue is related to how org.springframework.util.ReflectionUtils retrieves the cookie method from MockHttpServletRequestBuilder. Sometimes it gets MockHttpServletRequestBuilder.cookie, and other times it gets AbstractMockHttpServletRequestBuilder.cookie. Although both methods accept varargs, calling isVarArgs() returns true for the first and false for the second. This inconsistency causes the test to fail when the non-varargs method is used.

Example:

Stream.of(MockHttpServletRequestBuilder.class.getMethods())
    .filter(m -> m.getName().equals("cookie"))
    .forEach(m -> System.out.println(m + ": " + m.isVarArgs()));

Results: public org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.cookie(jakarta.servlet.http.Cookie[]): true public org.springframework.test.web.servlet.request.AbstractMockHttpServletRequestBuilder org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder.cookie(jakarta.servlet.http.Cookie[]): false

Test case:

class MyControllerTest {
    @BeforeEach
    void setUp() {
        standaloneSetup(new MyController());
    }

    @Test
    void get() {
        given()
            .cookie("MyCookie", "MyCookieValue")
        .when()
            .get("/")
        .then()
            .statusCode(200);
    }
}

spring-mock-mvc: 5.5.6 Spring Boot version: 3.5.6

Source: rest-assured/rest-assured