带注释的 bean 在 junit 中如何可用?

how are annoted beans available in junit?

我从事的现有项目有一个 Spring bean,它是使用 Java:

创建的
@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 
    ....

我从未使用过 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 class。如果 BeansConfiguration class 由另一个 JavaConfig class 加载,则最后一个可能不正确。在这种情况下,您应该将 ContextConfiguration 指向父配置。