英文:
Can webClient be mocked if it's initialised in PostConstruct method?
问题
我有一个包含私有WebClient的类,该WebClient是通过PostConstruct初始化的。
我想要模拟它,以避免调用真正的端点,但我不知道如何为这个对象创建模拟。因为它不是通过字段或构造函数初始化的。
有人能指导如何在不使用反射的情况下进行模拟吗?
谢谢。
英文:
I have a class with private WebClient which is initialised via PostConstruct.
I would like to mock it to not call real endpoints, but I have't got any idea how I can create mock for this object. As it't not initialised via a field or a constructor.
Can anyone advise how to it can be mocked without using reflection?
Thank you.
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {
private final ServiceB serviceB;
private WebClient webClient;
@PostConstruct
public void init() {
this.webClient = WebClient.builder()
.baseUrl(baseUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.clientConnector(new ReactorClientHttpConnector(new HttpClient()))
.build();
}
@Override
public String request(String body) {
return this.requestString(body)
.bodyToMono(String.class)
.block();
}
public WebClient.ResponseSpec requestString(String body) {
return webClient
.post()
.uri(uriBuilder -> uriBuilder
.path("/test/{id}/")
.build(1))
.bodyValue(body)
.retrieve();
}
}
答案1
得分: 1
你需要摒弃PostContruct的初始化,只需创建一个WebClient bean,并让Spring注入到你的类中:
@Configuration
public class SomeConfig {
@Bean
public WebClient webClient() {
return WebClient.builder()
.baseUrl(baseUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.clientConnector(new ReactorClientHttpConnector(new HttpClient()))
.build();
}
}
因此,在重构后,你的类将如下所示,现在你可以轻松地进行模拟:
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {
private final ServiceB serviceB;
private final WebClient webClient;
@Override
public String request(String body) {
return this.requestString(body)
.bodyToMono(String.class)
.block();
}
public WebClient.ResponseSpec requestString(String body) {
return webClient
.post()
.uri(uriBuilder -> uriBuilder
.path("/test/{id}/")
.build(1))
.bodyValue(body)
.retrieve();
}
}
英文:
You need to get rid of the PostContruct initialization and
just create a WebClient bean and let spring to inject it in your class:
@Configuration
public class SomeConfig {
@Bean
public WebClient webClient() {
WebClient.builder()
.baseUrl(baseUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.clientConnector(new ReactorClientHttpConnector(new HttpClient()))
.build();
}
}
So, your class would look like this after refactoring, now you can mock them easily:
@Service
@RequiredArgsConstructor
public class TestServiceImpl implements TestService {
private final ServiceB serviceB;
private final WebClient webClient;
@Override
public String request(String body) {
return this.requestString(body)
.bodyToMono(String.class)
.block();
}
public WebClient.ResponseSpec requestString(String body) {
return webClient
.post()
.uri(uriBuilder -> uriBuilder
.path("/test/{id}/")
.build(1))
.bodyValue(body)
.retrieve();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论