@ConfigurationProperties 在 Spring Boot 2.4.2 的测试中不起作用

@ConfigurationProperties don't work in test in Spring Boot 2.4.2

我有一个应用程序在 Spring Boot 2.3.8 中运行良好,但 @RestClientTests 在 2.4.2 中失败,因为测试对象无法实例化,因为没有 [= =14=](由@TestConfiguration创建)。

我要如何更改我的代码才能与 2 一起使用。4.x?

代码是:

@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 中失败

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: {}

事实证明,@RestClientTest 像宣传的那样工作并抑制了 @TestConfiguration 中的 bean:

Using this annotation will disable full auto-configuration and instead apply only configuration relevant to rest client tests (i.e. Jackson or GSON auto-configuration and @JsonComponent beans, but not regular @Component beans).

使用 @Import 有帮助。

@RestClientTest(DynamicSenderIdReplacementClient.class)
@AutoConfigureWebClient(registerRestTemplate = true)
@Import(DynamicSenderIdReplacementClientWebTest.testConfiguration.class)
class DynamicSenderIdReplacementClientWebTest {