我有一个显然会破坏集成测试的源类。下面提供了代码。
@Configuration
@Import( EmailageConfiguration.class )
public class EmailageServiceConfiguration {
private static final String EMAILAGE_ACCOUNT_ID_CONFIG_KEY = "emailage.key";
private static final String EMAILAGE_API_KEY_CONFIG_KEY = "emailage.secret";
private final EmailageConfigHolder holder;
public EmailageServiceConfiguration( final EmailageConfigHolder holder ) {
this.holder = holder;
}
@Bean
public EmailageConfigHolder emailageConfigHolder( Environment env ) {
// final EmailageConfigHolder holder = new EmailageConfigHolder();
holder.setApiKey( env.getRequiredProperty( EMAILAGE_API_KEY_CONFIG_KEY ) );
holder.setAccountId( env.getRequiredProperty( EMAILAGE_ACCOUNT_ID_CONFIG_KEY ) );
return holder;
}
}
提供了IT测试,
public class EmailageServiceIT {
private static final String EMAILAGE_RESPONSE_FILE = "emailage-response.json";
private final static String EMAILAGE_ENDPOINT = "/emailage";
private EmailageService service;
@Rule
public WireMockRule wireMockRule = new WireMockRule( wireMockConfig().dynamicPort() );
@Before
@SuppressWarnings( "resource" )
public void setup() {
int port = wireMockRule.port();
System.setProperty( "emailage.uri", "http://localhost:" + port + EMAILAGE_ENDPOINT );
System.setProperty( "emailage.key", "123" );
System.setProperty( "emailage.secret", "123" );
System.setProperty( "emailage.connection_timeout", "10000" );
System.setProperty( "emailage.read_timeout", "10000" );
System.setProperty( "kafka.bootstrap_servers", "localhost" );
System.setProperty( "kafka.topic", "ella-test-topic" );
ApplicationContext context = new AnnotationConfigApplicationContext( EmailageServiceConfiguration.class );
service = context.getBean( EmailageService.class );
}
// .........................
}
运行maven命令时,出现错误堆栈
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'emailageServiceConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailageServiceConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?
at com.ratepay.ella.service.EmailageServiceIT.setup(EmailageServiceIT.java:64)
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'emailageServiceConfiguration': Requested bean is currently in creation: Is there an unresolvable circular reference?
at com.ratepay.ella.service.EmailageServiceIT.setup(EmailageServiceIT.java:64)
因此,IT测试基本上在LOC中失败了,
ApplicationContext context = new AnnotationConfigApplicationContext( EmailageServiceConfiguration.class );
我还为问题添加了EmailageConfigHolder
类
@Getter
@Setter
public class EmailageConfigHolder {
private String apiKey;
private String accountId;
}
我该如何纠正?
答案 0 :(得分:1)
也许我对此有误,但是
EmailageServiceConfiguration
负责Bean EmailageConfigHolder
的创建(不是@Bean注释表示的内容)?
那么spring应该如何将其传递给应该创建它的对象的构造函数?那就是您的循环依赖关系最可能的来源。
要解决此问题,请为您的EmailageServiceConfiguration创建一个独立的生产者。