0

我有一个在 Spring Boot 2.3.8 中运行良好的应用程序,但@RestClientTests 在 2.4.2 中失败,因为无法实例化测试对象,因为没有 bean @ConfigurationProperties(由@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: {}
4

1 回答 1

2

事实证明,@RestClientTest按照宣传的方式工作并抑制 bean @TestConfiguration

使用此注解将禁用完全自动配置,而是仅应用与其余客户端测试相关的配置(即 Jackson 或 GS​​ON 自动配置和@JsonComponentbean,但不是常规 @Componentbean)。

使用@Import帮助。

@RestClientTest(DynamicSenderIdReplacementClient.class)
@AutoConfigureWebClient(registerRestTemplate = true)
@Import(DynamicSenderIdReplacementClientWebTest.testConfiguration.class)
class DynamicSenderIdReplacementClientWebTest {
于 2021-01-22T10:54:14.370 回答