Aar*_*ron 12 java unit-testing mockito powermock
我有三个非常简单的课程.其中一个扩展了父类.
public class Parent{
protected String print() {
// some code
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个儿童班.
public class Child extends Parent {
/**
* Shouldn't invoke protected Parent.print() of parent class.
*/
@Override
protected String print() {
// some additional behavior
return super.print();
}
}
Run Code Online (Sandbox Code Playgroud)
和测试班.
public class ChildTest {
@Test
public void should_mock_invocation_of_protected_method_of_parent_class() throws Exception {
// Given
Child child = PowerMockito.mock(Child.class);
Method method = PowerMockito.method(Parent.class, "print");
PowerMockito.when(child, method).withNoArguments().thenReturn("abc");
// When
String retrieved = child.print();
// Than
Mockito.verify(child, times(1)).print(); // verification of child method
Assert.assertEquals(retrieved, "abc");
}
}
Run Code Online (Sandbox Code Playgroud)
我需要验证super.print()调用.我该怎么做?
Era*_*ran -4
如果你调用super.print()Child类的print()方法,当然print()会调用Parent类的实现。如何验证它是否确实发生取决于父类实现实际执行的操作。
PS你代码中的注释Shouldn't invoke protected Parent.print() of parent class是Parent.print() method shouldn't be invoked.错误的。
| 归档时间: |
|
| 查看次数: |
8509 次 |
| 最近记录: |