注释bean如何在junit中可用



我所从事的一个现有项目,有一个用Java创建的Spring bean:

@Configuration
public class BeansConfiguration
{
  @Autowired
  private Environment environment;
  @Bean(name = "edielbean")
  public EdielBean edielBean()
  {
    return new EdielBean();
  }
}

现在我想从单元测试中访问该bean。我的问题类似于如何在jUnit测试中访问带有@RunWith和@ContextConfiguration注释的Spring上下文?除了我的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 
    ....

我从来没有使用过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指向父配置。

最新更新