首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >@ConfigurationProperties在Spring 2.4.2中无法测试

@ConfigurationProperties在Spring 2.4.2中无法测试
EN

Stack Overflow用户
提问于 2020-12-18 12:59:03
回答 1查看 797关注 0票数 0

我有一个在SpringBoot2.3.8中运行良好的应用程序,但是@RestClientTest的2.4.2失败了,因为不能实例化测试对象,因为没有@ConfigurationProperties的bean (由@TestConfiguration创建)。

如何更改代码,使其与2.4.x一起工作?

守则是:

代码语言:javascript
复制
@Configuration
@ConfigurationProperties(prefix = "tyntec.routetest.dsidr")
@Data
@Validated
public class DynamicSenderIdReplacementClientConfiguration {

  @NotBlank
  private String baseUrl;
  @NotBlank
  private String dsidrPath;
}

@Component
@RequiredArgsConstructor
public class DynamicSenderIdReplacementClient {

  private final DynamicSenderIdReplacementClientConfiguration configuration;
}

@ExtendWith(SpringExtension.class)
@RestClientTest(DynamicSenderIdReplacementClient.class)
@AutoConfigureWebClient(registerRestTemplate = true)
class DynamicSenderIdReplacementClientWebTest {

  @Autowired
  private DynamicSenderIdReplacementClient cut;

  @TestConfiguration
  static class testConfiguration {

    @Bean
    @Primary
    public DynamicSenderIdReplacementClientConfiguration provideConfig() {
      return new DynamicSenderIdReplacementClientConfiguration() {
        {
          setBaseUrl(BASE_URL);
          setDsidrPath(DSIDR_PATH);
        }
      };
    }
  }

这在2.3.8中有效,但在2.4.2中失败

代码语言:javascript
复制
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.tyntec.routetesting.client.itest.clients.DynamicSenderIdReplacementClientConfiguration' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
EN

Stack Overflow用户

回答已采纳

发布于 2021-01-22 10:54:14

事实证明,@RestClientTest按照广告的方式工作,并抑制@TestConfiguration中的bean

使用此注释的

将禁用完全的自动配置,而只应用与rest客户端测试相关的配置(即Jackson或GSON自动配置和@JsonComponent bean,而不是常规的@Component bean)。

使用@Import会有所帮助。

代码语言:javascript
复制
@RestClientTest(DynamicSenderIdReplacementClient.class)
@AutoConfigureWebClient(registerRestTemplate = true)
@Import(DynamicSenderIdReplacementClientWebTest.testConfiguration.class)
class DynamicSenderIdReplacementClientWebTest {
票数 2
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65357489

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档