Powermockito:java.lang.IllegalArgumentException:参数类型不匹配

时间:2017-02-17 13:39:04

标签: java mockito junit4 powermock

我对Mocking没有太多经验,我最近开始在我的Junit测试用例中使用它。但是,我很难理解执行情况。

我在尝试此代码时收到IllegalArgumentException

PowerMockito.doNothing().when(spyObject, "lockUser", String.class, User.class);

但是当我提供lockUser在执行时会收到的值时,一切都按预期工作。

工作代码

PowerMockito.doNothing().when(spyObject, "lockUser", "my_text", userMock);

我对此行为感到困惑。我期待相同的行为。 有人可以解释为什么会这样吗?

此外,当我有以下代码时

PowerMockito.doNothing().when(spyObject, "lockUser", anyString(), anyObject());

不再模拟该方法,并调用实际方法。

有趣的是,我有另一个同名的方法" lockUser"它需要不同数量的参数。在我的其他测试方法中,我只使用了Matchers(anyObject(),anyString()等)并且按预期工作。

PowerMockito.doNothing().when(spyObject, "lockUser", anyObject(), anyString(), anyString(), anyString());

所有的lockUser方法都是priavate。

我正与Mockito 1.9.5一起使用PowerMock 1.5.6

非常感谢任何帮助

编辑 附加代码,以明确

Class Core {
public Worker getWorker(String workerId) {
  // Get worker from Map<String, Worker> fID_WRK with workerId as key 
  // Get user from worker (I have mocked this part, so my mock user is     
  //   returned)
  If(user.isTooOld()) {
   lockUserAndNotify(reason, user);
   throw new UserLockedException("Too old");
   }

private void lockUserAndNotify(String reason, User user) {
  lockUserAndNotify(reason, user.fname, user.lname); // locks user and notifies
}

public getUser(String login, String password) {
  // find user in database
  if(user password is too old) {
    lockUserAndNotify(dbConnection, fname, lname, userId);
  }
}

private lockUserAndNotify(Connection dbConn, String fName, String lName, String
                 userId) {
  //method call to lock the user
  //method call to notify the admin
}


}

我的测试课

Class CoreTest {
  @Test (expected = UserLockedException.class)
  public void getUser_ThrowsException() throws                  
             Exception{

    Core core = new Core();
    Core coreSpy = PowerMockito.spy(core);

    when(userMock.isPwdUpdateTimeExpired()).thenReturn(true);
    PowerMockito.doNothing().when(coreSpy, "lockUserAndNotify", 
     anyObject(), anyString(), anyString(), anyString(), anyString());

    admin4.UserManager.getUser("l.user1","password");

  }

@Test (expected = UserLockedException.class)
      public void getWorker_ThrowsException() throws                  
                 Exception{

        Core core = new Core();
        Core coreSpy = PowerMockito.spy(core);

        Map workerMap = Whitebox.getInternalState(coreSpy, "fID_WRK");
        Map workerMapSpy = PowerMockito.spy(workerMap);

        when(workerMapSpy.getWorker("12345")).thenReturn(workerMock);
        when(workerMock.getUser()).thenReturn(userMock);
        when(userMock.isTooOld()).thenReturn(true);
        PowerMockito.doNothing().when(coreSpy, "lockUserAndNotify", 
         anyString(), anyObject());

        admin4.UserManager.getWorker("123445");

      }
}

因此测试getUser_ThrowsException按预期工作,但getWorker_ThrowsException不能。

1 个答案:

答案 0 :(得分:2)

要回答关于IllegalArgumentException: argument type mismatch的问题部分,您会得到此信息,因为您在使用

时错误地使用了API
PowerMockito.doNothing().when(spyObject, "lockUser", String.class, User.class);

请参阅PowerMocktioStubber.when的文档,此处转载的相关部分 -

public static <T> org.mockito.stubbing.OngoingStubbing<T> when(Class<?> klass,
                                                               Object... arguments)
                                                        throws Exception

Expect calls to private static methods without having to specify the method name. The method will be looked up using the parameter types if possible

Throws:
    Exception - If something unexpected goes wrong.
See Also:
    Mockito#when(Object)} 

正如您已经观察到的那样,您可以使用实际参数的值,也可以使用Matchers之类的anyString

这里有一些示例代码来演示这个 -

public class Core {
    public String getWorker(String workerId) {
        if (workerId.isEmpty()) {
            lockUser("Reason", workerId);
        }
        return workerId;
    }

    private void lockUser(String reason, String user) {
    }
}

和相应的测试 -

@RunWith(PowerMockRunner.class)
@PrepareForTest(Core.class)
public class CoreTest {

    @Test
    // this is incorrect usage and throws an IllegalArgumentException
    public void test1() throws Exception {
        Core spy = PowerMockito.spy(new Core());
        PowerMockito.doNothing().when(spy, "lockUser", String.class, String.class);
        spy.getWorker("");
    }

    @Test
    public void test2() throws Exception {
        Core spy = PowerMockito.spy(new Core());
        PowerMockito.doNothing().when(spy, "lockUser", Mockito.anyString(), Mockito.anyString());
        spy.getWorker("");
        PowerMockito.verifyPrivate(spy).invoke("lockUser", Mockito.anyString(), Mockito.anyString());
    }

    @Test
    public void test3() throws Exception {
        Core spy = PowerMockito.spy(new Core());
        PowerMockito.doNothing().when(spy, "lockUser", "abc", "Reason");
        spy.getWorker("abc");
        PowerMockito.verifyPrivate(spy, Mockito.times(0)).invoke("lockUser", Mockito.anyString(), Mockito.anyString());
    }
}

如果没有可编辑的代码或getWorker_ThrowsException的例外情况,则无法回答为何没有按预期工作的情况。添加所需信息后,我可以再次查看。

相关问题