【发布时间】:2014-05-12 15:37:53
【问题描述】:
我正在尝试为我的应用实施一组测试。在这种情况下,我有一些 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 测试的几个小时,因为一些互联网帖子已被弃用或不是最新的。这实际上是我的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 资源(引用包含在 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>
【问题讨论】:
-
如果您想在测试期间使用 jndi 资源,请在类路径中包含 jndi.properties 文件,并在此文件中包含上下文信息。例如,
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=jnp://localhost:1099。属性的值将根据数据源所在的应用程序服务器而变化。您也可以将它们设置为系统属性。或者,您可以使用直接连接到数据库而不是数据源。 -
但是我有一个问题。如果数据源定义的属性 jndiName 仍然存在...如何将您建议的文件与数据源链接?我认为您提出了类似于 jdbc.properties 的内容,您可以在 applicationContext 的数据源中读取这些属性。还有一个问题......我如何区分测试环境和部署之间的 applicationContext ?也许我需要两个?
-
当你指定一个带有 jndi 名称的数据源时,spring 内部会尝试创建一个 Initial Context 来查找 jndi 资源。要创建初始上下文,上述属性是必需的,因为它们告诉应用程序 jndi 资源在哪里注册。这些属性可以指定为系统属性(例如 -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory),也可以放在类路径中的 jndi.properties 文件中。
-
至于applicationContext,你可以有一个单独的文件进行测试。您必须通过在类路径中的应用程序(src)之前指定测试上下文文件夹来确保之前读取了测试上下文文件。在 Eclipse 中,您可以使用“项目属性>Java 构建路径”中的“订单和导出”选项卡来执行此操作。
标签: java spring spring-mvc junit mockito