gpt4 book ai didi

java - 使用不同的基于java的spring上下文配置与cucumber

转载 作者:行者123 更新时间:2023-12-02 08:50:56 26 4
gpt4 key购买 nike

我正在尝试让 Cucumber 与 Spring 一起工作。在我们的代码中,我们已经使用基于 java 的 Spring 配置。我无法让它在以下场景中工作。有人可以帮忙吗?

今天,在我们的集成测试类中,我们对每个类使用@ContextConfiguration,并提供在该集成测试类中声明的配置类以加载bean。 Config 类使用@Configuration 注解。相同的 bean 可以在 2 个不同的集成测试类中使用的 2 个不同的类 Config 类中进行不同的实例化。

因此,当我使用 Cucumber 时,由于不同类的 Contextconfiguration 不同,它会查找 'Cucumber.xml' 。在 xml 文件中,我使用组件扫描通过提供这些类使用的包名称(两个类具有相同的包名称)来扫描 cucumber 步骤定义类。由于所有 bean 都在相同的上下文中加载,因此当 Cucumber 发现这些不同的配置类中定义的相同 bean 时,它无法加载 bean。

如何克服以不同方式创建相同 bean 并在不同类中使用它们的问题?
请注意,我并不是在寻找一种会因现有编码实践而造成大量流失的解决方案,因此拥有 per-test-xml 文件对我来说不是一个选择。

这是我们的代码的样子:

类名称和地址提供程序集成测试步骤:-

@ContextConfiguration(locations="classpath:cucumber.xml")
public class NameAndAddressProviderIntegrationTestSteps {
@Configuration
@Import({
xyz.class,
abc.class,
NameAndAddressProvider.class
})
@ImportResource({
"file:configuration/spring-configuration/abc.xml",
"file:configuration/spring-configuration/xyz.xml"
})
public static class Config {

@Bean
AccountHolderDataMap dataMap() {
AccountHolderDataMap data = new AccountHolderDataMap();
data.put(ID,
new AccountHolderData(customerID));
data.get(customerID).setCustomerplaceID(testCustomerplaceID);
return data;
}

}
@Inject
private NameAndAddressProvider provider;

@When("^I call nameandAddress provider with a 'customerId'$")
public void i_call_nameandAddress_provider_with_a_customerId() throws DependencyException {
System.out.println("Entering when method");
names = provider.getNames(customerID);
System.out.println(provider.toString());
}
......
}

类AddressProviderIntegrationTestSteps:-

 @ContextConfiguration(locations="classpath:cucumber.xml")
public class AddressProviderIntegrationTestSteps {
@Configuration
@Import({
abc.class,
xyz.class,
AddressesProvider.class
})
@ImportResource({
"file:configuration/spring-configuration/test-environment.xml",
"file:configuration/spring-configuration/test-logging-config.xml"
})
public static class Config {
@Bean
@DependsOn("Environment")
AccountHolderDataMap data() {
AccountHolderDataMap data = new AccountHolderDataMap();
data.put(testCustomerID,
new AccountHolderData(testCustomerID, testCustomerplaceID,businessType));
return data;
}
}

private static final String testCustomerID = "1234";
private static final String testMarketplaceID = "abc";

@Inject
private AddressesProvider provider;

@When("^I call AddressesProvider provider with a 'CustomerID'$")
public void i_call_AddressesProvider_provider_with_a_CustomerID() throws Throwable {
List<Address> addresses = provider.getAddresses(testCustomerID);
Log.info(addresses.get(0).toString());
assertTrue(addresses.size()==1);
}

}

这是我得到的嵌套异常:-“嵌套异常是 org.springframework.beans.factory.NoUniqueBeanDefinitionException:没有定义类型 [....AccountHolderDataMap] 的合格 bean:预期单个匹配 bean,但发现 2:dataMap,data”

感谢您的帮助!

最佳答案

我管理了多个 bean 定义源。您可以从一开始就使用它(或者互联网上的其他人,因为您的问题已经很老了)我正在使用 spring4,请参阅我的其他 cucumer 帖子的 pom

在stepdefs中使用config.class

@ContextConfiguration(classes = { CucumberConfiguration.class })
public class StepdefsTest123 {

@Autowired bean; // from cucumberBeanContext.xml


@When("^A$")
public void a() throws Throwable {
System.out.println(bean.getFoo());
}

}

在配置类中添加额外的bean定义

@Configuration
@ComponentScan(basePackages = "package.here.cucumber")
@ImportResource("classpath:cucumberBeanContext.xml")
public class CucumberConfiguration {

// nothing to do here

}

关于java - 使用不同的基于java的spring上下文配置与cucumber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com