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

Mockito's deep-stub mocking doesn't do well when lambda arguments are involved #3253

Open
NadChel opened this issue Jan 28, 2024 · 0 comments

Comments

@NadChel
Copy link

NadChel commented Jan 28, 2024

Look at this test. It freezes and doesn't complete in a reasonable amount of time (if at all)

package com.example.dynamicgateway.service.applicationDocClient;

import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.junit.jupiter.api.Test;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WebClientTest {
    @Test
    void test() {
        OpenAPIV3Parser parserMock = mock(OpenAPIV3Parser.class);
        SwaggerParseResult parseResultMock = mock(SwaggerParseResult.class);
        WebClient webClientMock = mock(WebClient.class, RETURNS_DEEP_STUBS);
        when(webClientMock
                .get()
                .uri("https://example.com/docs")
                .retrieve()
                .bodyToMono(String.class)
                .map(parserMock::readContents)
        ).thenReturn(Mono.just(parseResultMock));

        Mono<SwaggerParseResult> stringMono = webClientMock
                .get()
                .uri("https://example.com/docs")
                .retrieve()
                .bodyToMono(String.class)
                .map(parserMock::readContents);

        StepVerifier.create(stringMono)
                .expectNext(parseResultMock)
                .expectComplete()
                .verify();
    }
}

I figured it has to do with the lambda (it doesn't matter if it's a method reference or not). If I run this test, it passes:

    @Test
    void test() {
        String responseString = "response-string";
        WebClient webClientMock = mock(WebClient.class, RETURNS_DEEP_STUBS);
        when(webClientMock
                .get()
                .uri("https://example.com/docs")
                .retrieve()
                .bodyToMono(String.class)
        ).thenReturn(Mono.just(responseString));

        Mono<String> stringMono = webClientMock
                .get()
                .uri("https://example.com/docs")
                .retrieve()
                .bodyToMono(String.class);

        StepVerifier.create(stringMono)
                .expectNext(responseString)
                .expectComplete()
                .verify();
    }

It doesn't matter if the lambda uses a mock or not. This freezes too:

    @Test
    void test() {
        WebClient webClientMock = mock(WebClient.class, RETURNS_DEEP_STUBS);
        String uri = "https://example.com";
        Integer expectedInt = 42;
        when(webClientMock
                .get()
                .uri(uri)
                .retrieve()
                .bodyToMono(String.class)
                .map(Integer::valueOf)
        ).thenReturn(Mono.just(42));

        Mono<Integer> integerMono = webClientMock
                .get()
                .uri(uri)
                .retrieve()
                .bodyToMono(String.class)
                .map(Integer::valueOf);

        StepVerifier.create(integerMono)
                .expectNext(expectedInt)
                .expectComplete()
                .verify();
    }

I believe it's a serious limitation: you may need to mock method chains involving a WebClient every now and then – chains that may accept an occasional lambda

Java 17, Spring Boot 3.2.1, spring-boot-starter-test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant