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