Java 如何在单元测试期间使用AnnotationConfigWebApplicationContext(Spring框架)加载配置类

Java 如何在单元测试期间使用AnnotationConfigWebApplicationContext(Spring框架)加载配置类,java,spring,configuration,junit,Java,Spring,Configuration,Junit,我使用SpringFramework3.0.5构建一个web应用程序。我使用@Configuration annotation来配置我的域对象,其中一些域对象具有会话范围。当我使用JUnit4.8.2编写单元测试时,AnnotationConfigWebApplicationContext变量无法获取配置类中定义的bean 我总是会遇到以下例外情况: org.springframework.beans.factory.NoSuchBean定义异常:未定义名为“country”的bean 有没有

我使用SpringFramework3.0.5构建一个web应用程序。我使用@Configuration annotation来配置我的域对象,其中一些域对象具有会话范围。当我使用JUnit4.8.2编写单元测试时,
AnnotationConfigWebApplicationContext
变量无法获取配置类中定义的bean

我总是会遇到以下例外情况:

org.springframework.beans.factory.NoSuchBean定义异常:未定义名为“country”的bean
有没有人能就这个问题给我一些建议?非常感谢

这是我的配置类、单元测试类、DAO类和域对象类。(我使用注释来配置应用程序,而不是xml)

配置类:

@配置
公共类区域DOMAINOBJ
{
/**
*定义城市域对象bean
*/
@豆子
@范围(“会议”)
公共城市
{
返回新的citymp();
}
/**
*定义城市IP域对象bean
*/
@豆子
@范围(“会议”)
公共城市皮条客城市
{
返回新的citypimp();
}
/**
*定义国家域对象bean
*/
@豆子
@范围(“会议”)
公共国家IMP国家()
{
返回新CountryImp();
}
/**
*定义国家IP域对象bean
*/
@豆子
@范围(“会议”)
公共国家IP国家IP()
{
返回新的countryIPMP();
}
/**
*定义区域设置域对象bean
*/
@豆子
@范围(“会议”)
公共LocaleImp locale()
{
返回新的LocaleImp();
}
/**
*定义区域域对象bean
*/
@豆子
@范围(“会议”)
公共区域管理区域()
{
返回新RegionImp();
}
/**
*定义顶级域对象bean
*/
@豆子
@范围(“会议”)
公共TopLevelDomainImp topLevelDomain()
{
返回新的TopLevelDomainImp();
}
}
测试超类:

@RunWith(SpringJUnit4ClassRunner.class)
公共类测试
{
受保护的注释ConfigWebApplicationContext注释ConfigWebApplicationContext;
@以前
公共作废设置()
{
annotationConfigWebApplicationContext=新的annotationConfigWebApplicationContext();
annotationConfigWebApplicationContext.refresh();
}
}
单元测试类:

公共类CountryDAOTest扩展了TestENV
{
私家i村道countryDAO;
私人国家;
@以前
public void setUpLocalTestENV()
{
this.country=(i国家)this.annotationConfigWebApplicationContext.getBean(“国家”);
this.countryDAO=(iconrydao)this.annotationConfigWebApplicationContext.getBean(“crazyamsCountryDAO”);
}
/*测试拯救国家*/
@试验
public void testSaveCountry()
{
此.country.setCode(“AU”);
this.country.setName(“澳大利亚”);
这个。国家道。拯救(这个。国家);
/*忽略断言函数*/
}
}
更新

也许我把这个问题弄得太复杂了,你知道,当我们使用
AnnotationConfigApplicationContext
时,我们可以使用下面的代码来注册bean定义

AnnotationConfigApplicationContext AnnotationConfigApplicationContext=新的AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(TestProcessUnitConfig.class,OtherClazz.class);
在我的项目中,我使用带注释支持的Spring MVC,并使用
AnnotationConfigWebApplicationContext
而不是
AnnotationConfigApplicationContext

尽管它们非常相似,
注释ConfigWebApplicationContext
不提供“注册”功能


我只想知道是否有其他方法可以将bean定义注册到
注释ConfigWebApplicationContext
中。

我不知道这是否是原因,但有一件事困扰着我:

国家/地区
导出为实现类:

 /**
  * Define Country Domain Object bean
  */
 @Bean @Scope("session") public CountryImp country(){
     return new CountryImp();
 }
但将其作为接口引用:

this.country = (ICountry) this.annotationConfigWebApplicationContext.getBean("country");
更明显的方法是将其导出为接口:

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

试着看看这是否会有所不同。即使没有,它也会改进您的代码。

我认为您需要在上下文构造函数中使用@Configuration类:

AnnotationConfigApplicationContext AnnotationConfigApplicationContext=新的AnnotationConfigApplicationContext(RegionDomainObj.class);
或注册:

AnnotationConfigApplicationContext AnnotationConfigApplicationContext=新的AnnotationConfigApplicationContext();
annotationConfigApplicationContext.register(RegionDomainObj.class);
据我所知,
AnnotationConfigWebApplicationContext
(带“Web”)是用于JSF的

org.springframework.beans.factory.nosuchbeansdefinitionexception:没有名为

当配置类出现问题时,会出现此错误。在您的情况下,您必须直接定义bean的定义位置。因此,在测试类中的
@Configuration
之后添加下一个代码行:

@WebAppConfiguration
@ContextConfiguration(类={/*…您的配置类*/})
@PropertySource(/*…您可能的属性文件..*/);

使用CTRL+K设置代码格式。现在无法读取。忽略不重要的部分。