title: Unable to Mock behavior using Mockito for testing Struts2 action class method
body: <p>Well this is the setup that I have:    </p>
  
  <p>WelcomeUser.java</p>
  
  <pre><code>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&lt;UserPojo&gt; {
  
  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;
  }
  }
  </code></pre>
  
  <p>ApplicationDAOfactory.java</p>
  
  <pre><code>package org.user.www.dao;
  
  public class ApplicationDAOfactory {
  
  public  UserPojoDao getUserPojoDaoInstance(){
      return new UserPojoDaoImpl();
  }
  }
  </code></pre>
  
  <p>WelcomeUserTest.java</p>
  
  <pre><code>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&lt;WelcomeUser&gt;{
  
  @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&lt;String, Object&gt; paramMap =  new HashMap&lt;String, Object&gt;();
      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);
  }
  
  }
  </code></pre>
  
  <p>My doubt is that although I am trying to mock the behavior upon which my welcome method in the struts action class depends but it doesn't seems to be working. I have browsed through various sources but it has all gone in vain. As far as I have been able to comprehend this situation my best guess is that when I call the execute method upon the proxy instance in my test method the control does execute the welcome method as per the mapping in my struts.xml file but when execution arrives at the point where I am expecting my mocks to work it doesn't works and hits my database which I don't want. I do understand there is certainly a gap in my understanding but any help shall be appreciated. Cheers!!</p>