从另一个 class 方法内部调用时无法正确模拟方法调用
Unable to properly mock method call when called from inside another class method
MyClass firstClass = PowerMockito.spy(new MyClass());
AnotherClass secondClass;
secondClass = PowerMockito.mock(AnotherClass.class);
PowerMockito.when(secondClass.anotherFunction(Mockito.any()).thenReturn(1);
int myInt = firstClass.myFunction();
if (myInt == 1) {
System.out.println("true");
}
myFunction
调用 anotherFunction
和 returns anotherFunction
.
的结果
但它并没有像我期望的那样返回 1
并打印 "true
",相反它仍在执行其真正的功能。
我在这里错过了什么?
An instance of AnotherClass is created inside myFunction then the instance is used to call secondClass.anotherFunction from inside myFunction.
没错。这意味着使用的是真实实例,而不是模拟实例。被测方法与依赖关系紧密耦合,因为它自己创建了一个真实的实例
public class MyClass {
public int myFunction() {
AnotherClass secondClass = new AnotherClass();
int result = secondClass.anotherFunction(someValue);
//...
return result;
}
}
How can I use the mocked instance instead?
您要么重构以通过构造函数或方法参数注入第二个 class,这是一个干净的代码设计,要么您使用 powermock 模拟第二个 class 的初始化,这在我看来是糟糕的设计。
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class) //<-- you must prepare the class creating the new instance
public class MyClassTest {
@Test
public void test() {
//Arrange
int expected = 1;
//Mock second class
AnotherClass secondClass;
secondClass = PowerMockito.mock(AnotherClass.class);
PowerMockito.when(secondClass.anotherFunction(Mockito.any()).thenReturn(expected);
//mocking initialization of second class withing first class
PowerMockito.whenNew(AnotherClass.class).withNoArguments().thenReturn(secondClass);
MyClass firstClass = new MyClass();
//Act
int actual = firstClass.myFunction();
//Assert
assertEquals(expected, actual);
}
}
MyClass firstClass = PowerMockito.spy(new MyClass());
AnotherClass secondClass;
secondClass = PowerMockito.mock(AnotherClass.class);
PowerMockito.when(secondClass.anotherFunction(Mockito.any()).thenReturn(1);
int myInt = firstClass.myFunction();
if (myInt == 1) {
System.out.println("true");
}
myFunction
调用 anotherFunction
和 returns anotherFunction
.
但它并没有像我期望的那样返回 1
并打印 "true
",相反它仍在执行其真正的功能。
我在这里错过了什么?
An instance of AnotherClass is created inside myFunction then the instance is used to call secondClass.anotherFunction from inside myFunction.
没错。这意味着使用的是真实实例,而不是模拟实例。被测方法与依赖关系紧密耦合,因为它自己创建了一个真实的实例
public class MyClass {
public int myFunction() {
AnotherClass secondClass = new AnotherClass();
int result = secondClass.anotherFunction(someValue);
//...
return result;
}
}
How can I use the mocked instance instead?
您要么重构以通过构造函数或方法参数注入第二个 class,这是一个干净的代码设计,要么您使用 powermock 模拟第二个 class 的初始化,这在我看来是糟糕的设计。
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyClass.class) //<-- you must prepare the class creating the new instance
public class MyClassTest {
@Test
public void test() {
//Arrange
int expected = 1;
//Mock second class
AnotherClass secondClass;
secondClass = PowerMockito.mock(AnotherClass.class);
PowerMockito.when(secondClass.anotherFunction(Mockito.any()).thenReturn(expected);
//mocking initialization of second class withing first class
PowerMockito.whenNew(AnotherClass.class).withNoArguments().thenReturn(secondClass);
MyClass firstClass = new MyClass();
//Act
int actual = firstClass.myFunction();
//Assert
assertEquals(expected, actual);
}
}