相关疑难解决方法(0)

Mockito如何只模拟超类方法的调用

我在一些测试中使用Mockito.

我有以下课程:

class BaseService {  
    public void save() {...}  
}

public Childservice extends BaseService {  
    public void save(){  
        //some code  
        super.save();
    }  
}   
Run Code Online (Sandbox Code Playgroud)

我想只模拟第二个调用(super.save)ChildService.第一个调用必须调用真正的方法.有没有办法做到这一点?

mockito

86
推荐指数
4
解决办法
10万
查看次数

如何验证父类的super.method()的调用?

我有三个非常简单的课程.其中一个扩展了父类.

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 …
Run Code Online (Sandbox Code Playgroud)

java unit-testing mockito powermock

12
推荐指数
1
解决办法
8509
查看次数

标签 统计

mockito ×2

java ×1

powermock ×1

unit-testing ×1