Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@miguel-figueiredo

Description

@miguel-figueiredo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions