How to use AnnotationConfigWebApplicationContext (Spring framework) to load a configuration class during unit test

I am using Spring framework 3.0.5 to build a web application. I use the @Configuration annotation to configure my domain object, and some domain objects have a session scope. And when I write unit test using jUnit 4.8.2, the AnnotationConfigWebApplicationContext variable cannot get the beans that are defined in the configuration class.

I get the following exceptions:

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'country' is defined 

Is there anyone who can give me advice on this issue? Thank you very much!

Here is my configuration class, unit test class, DAO class and domain object class. (I use annotation to configure the application instead of xml)

Configuration class:

 @Configuration public class RegionDomainObj { /** * Define City Domain Object bean */ @Bean @Scope("session") public CityImp city() { return new CityImp(); } /** * Define City IP Domain Object bean */ @Bean @Scope("session") public CityIPImp cityIP() { return new CityIPImp(); } /** * Define Country Domain Object bean */ @Bean @Scope("session") public CountryImp country() { return new CountryImp(); } /** * Define Country IP Domain Object bean */ @Bean @Scope("session") public CountryIPImp countryIP() { return new CountryIPImp(); } /** * Define Locale Domain Object bean */ @Bean @Scope("session") public LocaleImp locale() { return new LocaleImp(); } /** * Define Region Domain Object bean */ @Bean @Scope("session") public RegionImp region() { return new RegionImp(); } /** * Define Top Level Domain Domain Object bean */ @Bean @Scope("session") public TopLevelDomainImp topLevelDomain() { return new TopLevelDomainImp(); } } 

Test class:

 @RunWith(SpringJUnit4ClassRunner.class) public class TestENV { protected AnnotationConfigWebApplicationContext annotationConfigWebApplicationContext; @Before public void setUp() { annotationConfigWebApplicationContext = new AnnotationConfigWebApplicationContext(); annotationConfigWebApplicationContext.refresh(); } } 

Unit Test class:

 public class CountryDAOTest extends TestENV { private ICountryDAO countryDAO; private ICountry country; @Before public void setUpLocalTestENV() { this.country = (ICountry) this.annotationConfigWebApplicationContext.getBean("country"); this.countryDAO = (ICountryDAO) this.annotationConfigWebApplicationContext.getBean("crazyamsCountryDAO"); } /* Test save country */ @Test public void testSaveCountry() { this.country.setCode("AU"); this.country.setName("Australia"); this.countryDAO.save(this.country); /* ignore the assert functions */ } } 

Update

Maybe I am making this problem too complicated, you know, when we use AnnotationConfigApplicationContext , we can use the following code to register bean definitions.

 AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); annotationConfigApplicationContext.register(TestProcessUnitConfig.class, OtherClazz.class); 

And in my project I use Spring MVC with annotation support, and instead of AnnotationConfigApplicationContext use AnnotationConfigWebApplicationContext .

Although they are very similar, AnnotationConfigWebApplicationContext does not provide a "register" function.

And I just want to know if there is another way to register the bean definition in AnnotationConfigWebApplicationContext or not.

+4
source share
3 answers

I think you need to use your @Configuration class inside the context constructor:

 AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(RegionDomainObj.class); 

or with register:

 AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(); annotationConfigApplicationContext.register(RegionDomainObj.class); 

As I know, AnnotationConfigWebApplicationContext (with "Web") for JSF stuff.

+2
source

I don't know if this was the reason, but there is one thing that bothers me:

Exporting country as an implementation class:

  /** * Define Country Domain Object bean */ @Bean @Scope("session") public CountryImp country(){ return new CountryImp(); } 

But refer to it as an interface:

 this.country = (ICountry) this.annotationConfigWebApplicationContext.getBean("country"); 

A more obvious way would be to export it as an interface:

  /** * Define Country Domain Object bean */ @Bean @Scope("session") public ICountry country(){ return new CountryImp(); } 

Try to see if this has changed. Even if it is not, it improves your code.

0
source

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named

This error appears when something is wrong with your configuration classes. In your case, you need to determine exactly where you defined the beans. So, add the following codes after @Configuration to your test class:

 @WebAppConfiguration @ContextConfiguration(classes={/*..your configuration classes*/}) @PropertySource(/*.. your possible properties files..*/); 
0
source

All Articles