jeudi 21 mai 2015

InvocationTargetException in Easy mock of JUnit

I am facing an issue while running JUnit using EasyMock. Please find below the source files - UserTest.java (JUnit file) and UserFinder.java (Application file to be unit tested).

UserTest.java

Class UserTest
{
  ...
  @Test
  public void testFindUserFunction() throws Exception {


    DAO daoMock = EasyMock.createMock(DAO.class);       
    User user = new User("john", "stephen", "city street", "bangalore");
    EasyMock.expect(daoMock.userExists(user).andReturn(true);
    EasyMock.replay(daoMock);       
    String userFirstName = user.getFirstName();
    User resultUser = UserFinder.findUser(userFirstName, daoMock);      
    PowerMock.verify(daoMock);      
    Assert.assertEquals(user, resultUser);
  }
}

UserFinder.java

Class UserFinder {
...
...
public User findUser(String userFirstName, DAO dao)
{
return dao.findUser(userFirstName);
}
...
...
}

In JUnit, I have created a mock object daoMock and added below expect behavior.

EasyMock.expect(daoMock.userExists(user).andReturn(true);

In the below line of UserFinder.java, I need to pass daoMock as the class is already existing and designed in that way. When the below line is executed through JUnit, I am getting java.lang.reflect.InvocationTargetException.

User resultUser = UserFinder.findUser(userFirstName, daoMock);

I am new to EasyMock and I thought setting expect adds to the behavior of the daoMock object but it is not. Please help me about how to call dao.findUser line as I cannot mock this line since it is in application file. How can I mock and run the JUnit for this case in EasyMock. Thanks.

Aucun commentaire:

Enregistrer un commentaire