java - Spy private method on thread with mockito -
i have runnable call void private method during lifetime. want test, using powermockito, method "processstep" call 1 time each argument.
myrunnable class
public class myrunnable extends runnable { myrunnable(args){ ... } @override public final void run{ ... processstep(); ... } private void processtep(a){ ... addattributeresult(); ... } private void addattributeresult(){ ... } }
my test class testing myrunnable class
@powermockignore("org.apache.log4j.*") @runwith(powermockrunner.class) @preparefortest({ dbreader.class, myrunnable.class }) public class cyclemanagertest { @test public void mytest(){ myrunnable myrunnable = new myrunnable(arg[] {a,b}); thread t = new thread(myrunnable); t.start(); while (myrunnable.getnbrendcycle() < 1) { thread.sleep(10); } t.interrupt(); for(string s : arg){ powermockito.verifyprivate(myrunnable, times(1)).invoke("processstep", a); } } }
when there 1 argument test succed when there many arguments, test on error :
*org.mockito.exceptions.misusing.unfinishedverificationexception: missing method call verify(mock) here: -> @ fr.myrunnable.addattributeresult(myrunnable.java:254) example of correct verification: verify(mock).dosomething() also, error might show because verify either of: final/private/equals()/hashcode() methods. methods *cannot* stubbed/verified.*
i don't understand happend. think totally wrong somewhere.
the @preparefortest
annotation should refer class containing private method test, here myrunnable
. see last sample of https://code.google.com/p/powermock/wiki/mockitousage13.
Comments
Post a Comment