0

In my unit test for DAO method, I am trying to test the update user method. But, I have to create an user and then update it. So my mock DAO is expecting the addUser call, but then when I am, calling the update method it is complaining

java.lang.AssertionError: Unexpected method call UserAdminDAO.updateUser(null):

here is the code snipper

expect(userAdminDAO.addNewUser(u1)).andReturn(u1);
    replay(userAdminDAO);

    User u2 = (User)userService.addNewUser(ar);
    Assert.assertEquals(u.getUserName(), u2.getUserName());

    u2.setUserName("new modified");
userAdminDAO.updateUser(u2);   //error is on this line 
    expectLastCall().once().andAnswer(new IAnswer<User>() {
        public User answer() {
            return null;
        }
    });
    replay(userAdminDAO);
    userService.updateUser(u2);
mi3
  • 581
  • 1
  • 7
  • 13

1 Answers1

1

You've replayed the mock before adding the second expectation:

expect(userAdminDAO.addNewUser(u1)).andReturn(u1);
replay(userAdminDAO); // you shouldn't do this
...
userAdminDAO.updateUser(u2);
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255