有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在尝试为我的应用程序实施一组测试。在本例中,我有一些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定义..)可能是加载的应用程序上下文不正确

我也试过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(); 
    }
}

记录在案。。。我的应用程序上下文。xml放在/src/main/webapp/WEB-INF/applicationContext中。xml 希望你能指引我正确的方向。多谢各位

Edit1:applicationContext。xml和上下文。添加了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。但是出现了一个新问题。我需要在我的应用程序中使用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 

这是我的背景。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>

共 (3) 个答案

  1. # 1 楼答案

    未正确加载应用程序上下文。@ContextConfiguration注释的locations属性应该是"classpath:main/webapp/WEB-INF/applicationContext.xml""classpath:**/applicationContext.xml""classpath*:applicationContext.xml"。当您运行测试时,应用程序未部署,因此WEB-INF将不在类路径中(除非您自己将其添加到测试类路径中)

    使用模拟对象时,需要为将要调用的方法提供模拟实现。getUsers()方法返回null,因为您尚未设置所需的结果。你可以在mockito网站上查看http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html上的示例

  2. # 2 楼答案

    问题是您请求了一个类路径资源,而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,但我猜它可能会工作)

  3. # 3 楼答案

    试一试

    @ContextConfiguration(locations = {"classpath:/applicationContext.xml"})
    

    更新 事实上,这也不起作用,因为。应用程序上下文。xml不在类路径上。它需要移动到WEB-INF/classes目录中