我为spring mvc数据访问层编写了一个小测试用例。
测试类
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);
}
}
Dao class
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;
}
}
我将所需的对象作为模拟对象注入测试类中。
但是模拟对象的某些值是使用属性文件获得的,并且它是在appication-context xml文件中配置的。
我尝试在测试用例之前加载appication-context xml文件但是失败了。 我尝试如下。
@ContextConfiguration(locations = {"/springjbehave-servlet.xml"})
项目文件结构
我在这个场景中看到了类似的问题。但以下建议无法解决问题。
任何人都可以描述它为什么会发生以及如何解决这个问题它会非常有用。提前谢谢。
speeding-up-spring-integration-tests
How to access Spring context in jUnit tests annotated with @RunWith and @ContextConfiguration?