我正在测试一个Java方法,该方法使用Spring Boot WebClient发出PUT请求并返回void。该测试是用 Groovy 编写的。这是我想测试的方法:
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] 运行 com.foo.Test 初始化测试设置 设置完成 进入测试put处理 模拟 webclient put 方法 开始 put 处理 <<< 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...
我没有尝试运行你的代码,因为无论如何可能有很多类丢失了。但我注意到经典的 Spock 用户问题是尝试将模拟和存根分开,而不是像 Spock 手册中描述的那样同时进行:
你做到了:
webClientMock.put() >> bodySpec
// ...
1 * webClientMock.put()
你应该这样做:
1 * webClientMock.put() >> bodySpec