java - 无法在 junit 测试中从应用程序上下文加载 bean

标签 java spring spring-mvc junit mockito

我正在尝试为我的应用程序实现一组测试。在本例中,我有一些 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 中 希望你能指导我正确的方法。谢谢

编辑1:添加了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>

最佳答案

问题是您请求了类路径资源,而 WEB-INF/applicationContext.xml 可能不在类路径上(通常不会)。

您可以尝试将 applicationContext.xml 移动到类路径上的某个位置。我猜测您正在使用 Maven,在这种情况下,这通常是 src/main/resourcessrc/test/resources (如果这是特定的)到特定的测试,而不是“真实的”应用程序上下文文件)。我还会按照 @ohiocowboy 的建议修复您的 @ContextConfiguration (假设您将其直接放在该目录中,因为如果您明确说明位置,通常情况会更好)。

如果您的应用程序上下文绝对需要位于 WEB-INF/applicationContext.xml 中,您可以尝试使用 file:src/main/webapp/WEB-INF/applicationContext.xml。如果您使用 mvn test 从项目的基本目录运行,它应该可以工作,并且它可能也可以从 Eclipse 测试运行程序运行(不知道 Idea 或 NetBeans,但我的猜测是它可能有效)。

关于java - 无法在 junit 测试中从应用程序上下文加载 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613264/

相关文章:

java - Spring MVC : How to bind to nested object properties in the @ModelAttribute

java - org.springframework.beans.NotReadablePropertyException :

java - applicationContext.xml 中的数据源 bean 定义

java - 证明以下代码不是线程安全的

java - Apache Camel 中按时间顺序排列两个来源的消息

java - android - 显示变量更改

spring - JSON Web Token (JWT) 和基于 Spring 的 SockJS/STOMP Web Socket

java - Spring Data JPA 用户帖子

Java 的表现就像我有指针一样。为什么?

spring - Thymeleaf 缓存设置为 false 不起作用