Mockito How to mock only the call of a method of t

2019-01-01 13:45发布

I'm using Mockito in some tests.

I have the following classes:

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

public Childservice extends BaseService {  
    public void save(){  
        //some code  
        super.save();
    }  
}   

I want to mock only the second call (super.save) of ChildService. The first call must call the real method. Is there a way to do that?

标签: mockito
7条回答
明月照影归
2楼-- · 2019-01-01 14:12

create a package protected (assumes test class in same package) method in the sub class that calls the super class method and then call that method in your overridden sub class method. you can then set expectations on this method in your test through the use of the spy pattern. not pretty but certainly better than having to deal with all the expectation setting for the super method in your test

查看更多
登录 后发表回答