Тестирование веб-клиента SpringBoot в Groovy выдает исключение NullPointerException при вызове putJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Тестирование веб-клиента SpringBoot в Groovy выдает исключение NullPointerException при вызове put

Сообщение Anonymous »

Я тестирую метод 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] 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]
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»