javagroovywebclientspock

Test SpringBoot Webclient in Groovy throwsNullPointerException in put call


I am testing a Java method that uses Spring Boot WebClient to make a PUT request and returns void. The test is written in Groovy. This is the method I want to test:

public void putProcessing(final Auth auth, final TokenParser jwtParser, final String processing) {
    final String functional = AuthUtils.getFuncional(auth, jwtParser); // I have a mocked String here
    final CacheApiRequest cacheApiRequestBody = this.cacheApiRequestBodyMapper.map(processing, this.ttl); // I also have a mocked CacheApiRequest here
    this.webClient.put() // webclient is mocked here
            .uri(this.uriBase, // nullpointer exception here
                    uriBuilder -> uriBuilder.path("/").path(this.uriCacheLoading).path(this.valuePathKey)
                            .build(Utils.getFuncionalToogle(functional)))
            .header(TokenEnum.INFOSIG.getName(), auth.getInfoSig())
            .header(TokenEnum.AUTHENTICATION.getName(), auth.getAuthentication())
            .contentType(MediaType.APPLICATION_JSON).body(Mono.just(cacheApiRequestBody), CacheApiRequest.class)
            .retrieve().bodyToMono(Void.class).block();
}

And this is the class with the test method

@SpringBootTest
class CacheCarteiraRepositoryImplITSpec extends SpringBootSpec {

CacheCarteiraRepositoryImpl repository;
WebClient webClientMock = Mock(WebClient)
CacheApiRequestBodyMapper cacheMapper = Mock(CacheApiRequestBodyMapper)

def setup() {
    println "Initializing test setup"
    
    FixtureFactoryLoader.loadTemplates("com.foo.fixtures")
    repository = new CacheCarteiraRepositoryImpl(webClientMock, cacheMapper)

    repository.uriBase = "http://localhost:8080";
    repository.maxAttepts = 3L;
    repository.delay = 2L;
    repository.uriCacheLoading = "/api";
    repository.uriCacheData =  "/test";
    repository.ttl = 1L;
    repository.valuePathKey = "/abc";
    
    println "Setup completed"
}

@Unroll
def "must call putProcessing"() {
    println "Entering the test putProcessing"
    
    given: "a valid auth"
    Auth auth = Fixture.from(Auth.class).gimme("VALID")

    and: "a valid tokenParser"
    TokenParser jwtParser = Mock(TokenParser)

    and : "a mocked claims "
    Claims claims = Mock(Claims)
    claims.get("codigoFuncional", String.class) >> "1234-56"
    jwtParser.getBody("valid.jwt.token") >> claims

    and: "a mocked CacheApiRequest "
    CacheApiRequest cacheApiRequestMock = new CacheApiRequest("true", 1L)
    cacheMapper.map("true", 1L) >> cacheApiRequestMock

    WebClient.RequestBodyUriSpec bodySpec = Mock(WebClient.RequestBodyUriSpec)
    WebClient.RequestHeadersSpec headerSpec = Mock(WebClient.RequestHeadersSpec)
    WebClient.ResponseSpec responseSpec = Mock(WebClient.ResponseSpec)
    
    println "mocking webclient put method"

    webClientMock.put() >> bodySpec
    bodySpec.uri(_,_) >> bodySpec
    bodySpec.header(_,_) >> bodySpec
    bodySpec.contentType(_) >> bodySpec
    bodySpec.bodyValue(_) >> headerSpec
    headerSpec.retrieve() >> responseSpec
    responseSpec.bodyToMono(Void.class)>>  Mono.empty()

    when: "call putProcessing"
    println "Starting putProcessing"
    repository.putProcessing(auth, jwtParser, "true")

    then: "the webClient.put() is called"
    1 * webClientMock.put()

    println "test completed"
}

This is the error:

[INFO] Running com.foo.Test
Initializing test setup
Setup completed
Entering the test putProcessing
mocking webclient put method
Starting putProcessing
 <<< ERROR!
java.lang.NullPointerException: Cannot invoke "org.springframework.web.reactive.function.client.WebClient$RequestBodyUriSpec.uri(String, java.util.function.Function)" because the return value of "org.springframework.web.reactive.function.client.WebClient.put()" is null
        at com.foo.Test.putProcessing...

enter image description here


Solution

  • I did not try to run your code, because probably lots of classes are missing anyway. But I noticed the classical Spock user problem of trying to separate mocking and stubbing instead of doing them simultaneously as described in the Spock manual:

    You did:

    webClientMock.put() >> bodySpec
    // ...
    1 * webClientMock.put()
    

    You should do:

    1 * webClientMock.put() >> bodySpec