title: how are annoted beans available in junit?
body: <p>An existing project that I work on, has a Spring bean that's created with Java:</p>
  
  <pre><code>@Configuration
  public class BeansConfiguration
  {
    @Autowired
    private Environment environment;
  
    @Bean(name = "edielbean")
    public EdielBean edielBean()
    {
      return new EdielBean();
    }
  }
  </code></pre>
  
  <p>Now I want to access that bean from my unit test. My question is similar to <a href="https://stackoverflow.com/questions/2425015/how-to-access-spring-context-in-junit-tests-annotated-with-runwith-and-context">How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?</a> except that my bean is not in XML but in Java.</p>
  
  <p>I tried by adding this unit test (the camel parts can be ignored):</p>
  
  <pre><code>@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;
  }
  
  }
  </code></pre>
  
  <p>The console output shows there are only trivial beans:</p>
  
  <pre><code>...
  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
  ...
  </code></pre>
  
  <p>test output:</p>
  
  <pre><code>org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'edielbean' is defined
      at
      ....
      at com.essent.belgium.ediel.services.EdielBeanTest.testSupplierSwitch(EdielBeanTest.java:42)
      at 
      ....
  </code></pre>