[英]Unable to load beans from Application Context in junit test
我正在尝试为我的应用实施一组测试。 在这种情况下,我有一些mybatis映射器,其bean在我的applicationContext.xml中定义。 例如:
<bean id="usersMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.myapp.dao.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
我一直在寻找如何正确实施junit测试的时间,因为某些Internet帖子已被弃用或不是最新的。 这实际上是我的junit类:
@ContextConfiguration(locations = {"classpath:*applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class GroupTest {
@Autowired
ApplicationContext context;
@Test
public void testCreateGroup() throws SQLException {
UserMapper um = (UserMapper)context.getBean("usersMapper");
}
}
启动过程中没有错误。 当我尝试获取bean时,usersMapper返回一个异常(没有bean定义。)也许不是在正确加载applicationContext吗?
我也尝试了Mockito,但没有成功。 我读过它确实很棒,但是它能够像Spring一样加载上下文吗? 当我从UserMapper调用getUsers方法时,它返回null。 这是我的实现:
@ContextConfiguration(locations = {"classpath:*applicationContext.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
public class GroupTest {
@Mock
private UserMapper userMapper;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
}
@Test
public void testCreateGroup() throws SQLException {
userMapper.getUsers();
}
}
作为记录...我的applicationContext.xml放在/src/main/webapp/WEB-INF/applicationContext.xml中,希望您能以正确的方式指导我。 谢谢
Edit1:添加了applicationContext.xml和context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:comp/env/jdbc/adminDB"/>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/mybatis-config.xml" />
</bean>
<bean id="usersMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="es.unican.meteo.dao.UserMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
似乎Peter Hart解决方案加载了applicationContext.xml,但是出现了一个新问题。 我需要在我的应用程序中使用jndi Resource(引用包含在applicationContext.xml中)。 在测试环境中这不可能吗? 异常显示以下内容:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
这是我的context.xml
<Context>
<Resource name="jdbc/adminDB" auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdle="10" username="***" password="***"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:***"/>
</Context>
问题是您请求了类路径资源,并且WEB-INF/applicationContext.xml
可能不在类路径上(通常不会)。
您可以尝试将applicationContext.xml移动到类路径上的某个位置。 我猜您正在使用Maven,在这种情况下,通常是src/main/resources
或src/test/resources
(如果这是特定于特定测试的,而不是“真实的”应用程序上下文文件) )。 我还将按照@ohiocowboy的建议修复@ContextConfiguration
(假设您将其直接放置在该目录中,因为如果明确指定位置,通常情况下效果会更好)。
如果您的应用程序上下文绝对需要在WEB-INF/applicationContext.xml
,则可以尝试使用file:src/main/webapp/WEB-INF/applicationContext.xml
。 如果您使用mvn test
从项目的基本目录运行,则它应该可以工作,并且可能也可以从Eclipse测试运行程序中工作(对Idea或NetBeans一无所知,但我猜想它可能会工作)。
您的应用程序上下文未正确加载。 @ContextConfiguration
批注的locations属性应为"classpath:main/webapp/WEB-INF/applicationContext.xml"
, "classpath:**/applicationContext.xml"
或"classpath*:applicationContext.xml"
。 运行测试时,未部署应用程序,因此WEB-INF不会位于类路径中(除非您自己将其添加到测试类路径中)。
使用模拟对象时,您需要为将要调用的方法提供模拟实现。 由于尚未设置所需的结果,因此getUsers()
方法返回null。 您可以在http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html上查看模仿网站的示例。
尝试
@ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
更新实际上这也不起作用,因为。 applicationContext.xml不在类路径上。 需要将其移至WEB-INF / classes目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.