Does anyone know if there is a way that I can programmatically create a bean context?
I want to be able to do something like:
ConfigurableApplicationContext c = new ConfigurableApplicationContext(); BeanDefinition bd = new BeanDefinition(); bd.setId("id"); bd.setClassName("classname"); bd.setProperty("propertyName", propertyValue"); ...etc...
or itβs even better to embed a ready-made bean in the application context:
c.addBean("beanId", beanObject);
Or if I use annotations:
c.setAnnotationAware(true); c.setAnnotationScanBasePackage("packagename");
or
c.addAnnotatedSpringClass("classnamethatisannotated");
The rationale for this is that I want to override bean definitions for testing purposes. In my test, I create this new application context, configured with the code in the test (and not in xml), and then do it the test context of the application have the SUT application context as the parent.
I have not found any code in spring libraries that can do this. Has anyone built something like this? Is it possible to build something like this? I know that the previous approach is feasible, I am not 100% sure that the latter will work without any conditions.
spring unit-testing
Michael wiles
source share