使用Mockito测试Struts2动作类方法时无法模拟行为

时间:2014-11-23 04:14:47

标签: unit-testing junit struts2 mockito

这就是我的设置:

WelcomeUser.java

package org.user.www.action;
import org.user.www.dao.ApplicationDAOfactory;
import org.user.www.dao.UserPojoDao;
import org.user.www.pojo.UserPojo;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class WelcomeUser extends ActionSupport implements ModelDriven<UserPojo> {

private static final long serialVersionUID = 1L;
private UserPojo userPojo = new UserPojo();
private ApplicationDAOfactory appFactory = new ApplicationDAOfactory();
private UserPojoDao  appDAOObj /*= ApplicationDAOfactory.getUserPojoDaoInstance()*/;

public WelcomeUser(){

}

public WelcomeUser(UserPojoDao appDAOObj, ApplicationDAOfactory appFactory){
    this.appDAOObj  = appDAOObj;
    this.appFactory = appFactory;
}

public String execute(){
    return SUCCESS;
}
@Override
public UserPojo getModel() {
    // TODO Auto-generated method stub
    return userPojo;
}

public String welcome(){
    appDAOObj = appFactory.getUserPojoDaoInstance();
    appDAOObj.persistUser(userPojo);
    return SUCCESS;
}
}

ApplicationDAOfactory.java

package org.user.www.dao;

public class ApplicationDAOfactory {

public  UserPojoDao getUserPojoDaoInstance(){
    return new UserPojoDaoImpl();
}
}

WelcomeUserTest.java

package org.user.www.junit;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.StrutsJUnit4TestCase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.user.www.action.WelcomeUser;
import org.user.www.dao.ApplicationDAOfactory;
import org.user.www.dao.UserPojoDao;
import org.user.www.dao.UserPojoDaoImpl;
import org.user.www.pojo.UserPojo;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionProxy;

@RunWith(MockitoJUnitRunner.class)
public class WelcomeUserTest extends StrutsJUnit4TestCase<WelcomeUser>{

@Mock private  UserPojoDao appDAOObj;
@Mock private ApplicationDAOfactory appFactory;
@Mock private  UserPojo userPojo;

@Before
public void setUpBeforeClass() throws Exception {
    appDAOObj = mock(UserPojoDaoImpl.class);
    appFactory = mock(ApplicationDAOfactory.class);
    //MockitoAnnotations.initMocks(WelcomeUser.class);
    new WelcomeUser(appDAOObj, appFactory);

}

@Test
public void testWelcome() throws Exception{
    when(appFactory.getUserPojoDaoInstance()).thenReturn(appDAOObj);
    when(appDAOObj.persistUser(userPojo)).thenReturn(userPojo);
    Map<String, Object> paramMap =  new HashMap<String, Object>();
    paramMap.put("name" ,"user");
    paramMap.put("email" ,"user@user.com");
    ActionProxy proxy = getActionProxy("/submitUser");
    ActionContext actionContext= proxy.getInvocation().getInvocationContext();
    actionContext.setParameters(paramMap); 
    String result= proxy.execute();
    assertEquals("success", result);
}

}

我怀疑的是,尽管我试图模仿struts动作类中我的欢迎方法依赖的行为,但它似乎并没有起作用。我浏览了各种各样的消息来源,但一切都是徒劳的。至于我能够理解这种情况,我最好的猜测是,当我在我的测试方法中调用代理实例上的execute方法时,控件会根据我的struts.xml文件中的映射执行welcome方法但是在执行时到达我希望我的模拟工作的地方,它不起作用,并打到我不想要的数据库。我理解我的理解肯定存在差距,但任何帮助都应该受到赞赏。干杯!!

1 个答案:

答案 0 :(得分:0)

我有类似的问题,但我的情况有点不同,因为我通过spring框架使用setter Injector,我的动作类已经有Dao的setter方法。

我必须从代​​理和Inject嘲笑Dao对象到我的行动。 我在执行代理之前做了以下事情:

WelcomeUser actionObj = (WelcomeUser) proxy.getAction();
actionObj.setDaoObj(appDAOObj);

你必须找到一种方法将模拟对象注入actionObj。

更新:

检查以下内容是否适用

WelcomeUser actionObj = (WelcomeUser) proxy.getAction();
ReflectionTestUtils.setField(actionObj, "appDAOObj", appDAOObj);
ReflectionTestUtils.setField(actionObj, "appFactory", appFactory);
String result= proxy.execute();
assertEquals("success", result);

确保在类路径中有spring-test-x.x.x.RELEASE.jar和spring-core-x.x.x.RELEASE.jar(包括任何其他依赖项)。