我有一堂课如下:
public class A {
public A(String test) {
bla bla bla
}
public String check() {
bla bla bla
}
}
A(String test)
构造函数中的逻辑check()
是我试图模拟的东西。我想要任何调用,例如:new A($$$any string$$$).check()
返回一个虚拟字符串"test"
。
我试过:
A a = mock(A.class);
when(a.check()).thenReturn("test");
String test = a.check(); // to this point, everything works. test shows as "tests"
whenNew(A.class).withArguments(Matchers.anyString()).thenReturn(rk);
// also tried:
//whenNew(A.class).withParameterTypes(String.class).withArguments(Matchers.anyString()).thenReturn(rk);
new A("random string").check(); // this doesn't work
但这似乎不起作用。new A($$$any string$$$).check()
仍在通过构造函数逻辑而不是获取A
.