我在 Spring Boot 应用程序中使用带有 @ContextConfiguration 的 RestTemplateBuilder 时遇到了问题(我尝试添加 @SpringBootTest、@RunWith(SpringRunner.class) 注释但没有任何运气)。
任何帮助表示赞赏。这是背景:
我对我的班级进行了如下注释:
@ContextConfiguration(classes = {
JsonNodeList.class,
JsonNodeUtils.class,
MyService.class,
RestClient.class,
RestTemplateBuilder.class}, loader = SpringBootContextLoader.class)
public class StepsDefinition {
RestClient 类将 RestTemplateBuilder 自动装配为:
@Autowired
public RestClient(final RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = configureRestTemplate(restTemplateBuilder);
}
MyService 类自动装配 RestClient。当我尝试使用@ContextConfiguration
with加载应用程序SpringBootContextLoader
时,出现以下错误:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'restTemplateBuilder': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.web.client.RestTemplateBuilder]: No default constructor found; nested exception is java.lang.NoSuchMethodException: org.springframework.boot.web.client.RestTemplateBuilder.<init>()