Код: Выделить всё
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();
}
Код: Выделить всё
@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"
}
Код: Выделить всё
[INFO] Running com.foo.Test
Initializing test setup
Setup completed
Entering the test putProcessing
mocking webclient put method
Starting putProcessing
Подробнее здесь: [url]https://stackoverflow.com/questions/79070695/test-springboot-webclient-in-groovy-throwsnullpointerexception-in-put-call[/url]
Мобильная версия