Spring 如何在使用模拟对象的junit测试类之前加载应用程序上下文xml文件

Spring 如何在使用模拟对象的junit测试类之前加载应用程序上下文xml文件,spring,unit-testing,spring-mvc,junit,mockito,Spring,Unit Testing,Spring Mvc,Junit,Mockito,我已经为SpringMVC数据访问层编写了一个小测试用例 测试类 package com.test.jbehave.steps; import java.util.List; import org.jbehave.core.annotations.BeforeScenario; import org.jbehave.core.annotations.Given; import org.junit.Test; import org.junit.runner.RunWith; import org

我已经为SpringMVC数据访问层编写了一个小测试用例

测试类

package com.test.jbehave.steps;

import java.util.List;
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.core.annotations.Given;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.test.dao.home.HomeDao;
import com.test.dao.home.impl.HomeDaoImpl;
import com.test.mapping.useraccount.UserAccount;
import com.test.util.common.Common;
import com.test.util.varlist.CommonVarList;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/springjbehave-servlet.xml"})
public class UserAccountListSteps{

    @Mock
    JdbcTemplate jdbcTemplate;

    @Mock
    CommonVarList commonVarList;

    @Mock
    Common common;

    @InjectMocks
    HomeDao homeDao =new HomeDaoImpl();

    @BeforeScenario
    public void initMocks(){
        MockitoAnnotations.initMocks(this);
    }

    @Given("customer userid is $userid")
    @Test
    public void checkUserAccountList() throws Exception{
        List<UserAccount> userAccountList=homeDao.getUserAccountList("1");
        System.out.println("userAccountList  :"+userAccountList);
    }
}
package com.test.dao.home.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.test.dao.home.HomeDao;
import com.test.mapping.useraccount.UserAccount;
import com.test.util.common.Common;
import com.test.util.varlist.CommonVarList;

@Repository
@Scope("prototype")
public class HomeDaoImpl implements HomeDao{
    private final Log logger = LogFactory.getLog(getClass());

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    CommonVarList commonVarList;

    @Autowired
    Common common;

    private final String sqlUserAccountList ="SELECT USERID,ACCOUNTNO,ACCOUNTTYPE,BANK,STATUS,ACC_HOLDER_NAME,BRANCH FROM USERACCOUNT WHERE USERID=? AND STATUS=?";

    @Override
    public List<UserAccount> getUserAccountList(String userId) throws Exception {
        List<UserAccount> userAccountList=new ArrayList<UserAccount>();
        try{
            List<Map<String, Object>> resultSet=jdbcTemplate.queryForList(sqlUserAccountList,new Object[] {userId,commonVarList.STATUS_DEFAULT_ACTIVE});
            if(!resultSet.isEmpty()){
                for(Map<String,Object> record : resultSet){
                    UserAccount userAccount=new UserAccount();

                    userAccount.setUserId(common.replaceNullAndEmpty(record.get("USERID")));
                    userAccount.setAccountNumber(common.replaceNullAndEmpty(record.get("ACCOUNTNO")));
                    userAccount.setAccountType(common.replaceNullAndEmpty(record.get("ACCOUNTTYPE")));
                    userAccount.setBank(common.replaceNullAndEmpty(record.get("BANK")));
                    userAccount.setStatus(common.replaceNullAndEmpty(record.get("STATUS")));
                    userAccount.setAccountHolderName(common.replaceNullAndEmpty(record.get("ACC_HOLDER_NAME")));
                    userAccount.setBranch(common.replaceNullAndEmpty(record.get("BRANCH")));

                    userAccountList.add(userAccount);
                    System.out.println(userAccount.toString());
                }
            }
        }catch(Exception e){
            logger.error("Exception  :  ", e);
            throw e;
        }
        return userAccountList;
    }
}

项目文件结构

我见过类似的问题。但以下建议并不能解决这个问题

任何人都可以描述它发生的原因以及如何解决这个问题,这将是非常有帮助的。提前谢谢


为测试创建配置,并在配置文件中模拟bean。看见或者不要使用Spring,只需自己创建一个实例并注入mock,这将使它成为一个纯单元测试(您不必使用Spring进行测试,尤其是如果您想要一个简单的单元测试的话)@Denium我已经看完了上面提供的链接。但它仍然让我感到困惑。如果您能提供一个小示例来配置测试并在配置文件中模拟bean,这将非常有帮助。这些都在链接的答案中…我将再次浏览链接并重试。非常感谢@Denium。
@ContextConfiguration(locations = {"/springjbehave-servlet.xml"})