Powermockito:java.lang.IllegalArgumentException:参数类型不匹配
我没有太多的模拟经验,我最近开始在Junit测试用例中使用它。然而,我在理解执行过程中遇到了困难 当我尝试此代码时,我得到了IllegalArgumentExceptionPowermockito:java.lang.IllegalArgumentException:参数类型不匹配,java,mockito,junit4,powermock,Java,Mockito,Junit4,Powermock,我没有太多的模拟经验,我最近开始在Junit测试用例中使用它。然而,我在理解执行过程中遇到了困难 当我尝试此代码时,我得到了IllegalArgumentException PowerMockito.doNothing().when(spyObject, "lockUser", String.class, User.class); 但是,当我提供lockUser在执行时将收到的值时,一切都按预期工作 工作代码 PowerMockito.doNothing().when(spyObject, "
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”,它接受不同数量的参数。在我的另一个测试方法中,我只使用了匹配器(anyObject()、anyString()等),并且按照预期工作
PowerMockito.doNothing().when(spyObject, "lockUser", anyObject(), anyString(), anyString(), anyString());
所有lockUser方法都是私有的
我正在使用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
}
}
因此,测试getUser_ThrowsException按预期工作,但getWorker_ThrowsException不工作 要回答您的问题中关于
IllegalArgumentException:argument type mismatch
的部分,您会得到这个问题,因为您在使用
PowerMockito.doNothing().when(spyObject, "lockUser", String.class, User.class);
参见此处复制的相关章节的文档-
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)}
以及相应的测试-
@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\u ThrowsException
获得的异常,就无法回答为什么不能按预期工作。一旦您添加了所需的信息,我可以再次查看。您介意发布您要测试的代码吗?您的代码在这里无法编译。使复制和帮助您变得更加困难。阅读
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());
}
}