我工作的现有项目有一个用Java创建的Spring bean:
@Configuration
public class BeansConfiguration
{
@Autowired
private Environment environment;
@Bean(name = "edielbean")
public EdielBean edielBean()
{
return new EdielBean();
}
}
现在我想从单元测试中访问该bean。我的问题类似于How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?,除了我的bean不是XML而是Java。
我尝试添加此单元测试(可以忽略驼峰部分):
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration(loader=AnnotationConfigContextLoader.class)
public class EdielBeanTest extends CamelTestSupport implements ApplicationContextAware {
private ApplicationContext context;
@Override
public String isMockEndpoints() {
// camel
return "*";
}
@Test
public void testSupplierSwitch() throws Exception
{
getMockEndpoint("mock:market-out").expectedMessageCount(1); // camel
System.out.println("beans: "+context.getBeanDefinitionCount());
for (String name: context.getBeanDefinitionNames()) {
System.out.println(name);
}
EdielBean edielBean = (EdielBean)context.getBean("edielbean");
edielBean.startSupplierSwitch(createCustomer(), createOrder(), "54", "43");
assertMockEndpointsSatisfied(); // camel
}
private Customer createCustomer()
{
Customer customer = new Customer();
// .... part omitted
return customer;
}
private Order createOrder() {
Order order = new Order();
// .... part omitted
return order;
}
@Override
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = context;
}
}
控制台输出显示只有琐碎的bean:
...
beans: 6
org.springframework.context.annotation.internalConfigurationAnnotationProcessor
org.springframework.context.annotation.internalAutowiredAnnotationProcessorSep
org.springframework.context.annotation.internalRequiredAnnotationProcessor
org.springframework.context.annotation.internalCommonAnnotationProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
...
测试输出:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'edielbean' is defined
at
....
at com.essent.belgium.ediel.services.EdielBeanTest.testSupplierSwitch(EdielBeanTest.java:42)
at
....
答案 0 :(得分:1)
我从未与Camel合作过,所以我不确定这是否适合你的情况,但这就是我将单位测试改为工作的方式:
<强> BeansConfiguration 强>
@Configuration
public class BeansConfiguration
{
@Autowired
private Environment environment;
@Bean(name = "edielbean")
public EdielBean edielBean()
{
return new EdielBean();
}
}
<强> EdielBeanTest 强>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BeansConfiguration.class)
public class EdielBeanTest extends CamelTestSupport {
@Autowired
private ApplicationContext context;
@Override
public String isMockEndpoints() {
// camel
return "*";
}
@Test
public void testSupplierSwitch() throws Exception
{
getMockEndpoint("mock:market-out").expectedMessageCount(1); // camel
System.out.println("beans: " + context.getBeanDefinitionCount());
for (String name : context.getBeanDefinitionNames()) {
System.out.println(name);
}
EdielBean edielBean = (EdielBean) context.getBean("edielbean");
edielBean.startSupplierSwitch(createCustomer(), createOrder(), "54", "43");
assertMockEndpointsSatisfied(); // camel
}
private Customer createCustomer()
{
Customer customer = new Customer();
// .... part omitted
return customer;
}
private Order createOrder() {
Order order = new Order();
// .... part omitted
return order;
}
}
我删除了ApplicationContextAware
的实现,自动连接了ApplicationContext,并将@ContextConfiguration
更改为指向您的BeansConfiguration
类。如果BeansConfiguration类由另一个JavaConfig类加载,则最后一个可能不正确。在这种情况下,您应该将ContextConfiguration指向父配置。