Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of times() in JMockIt?

Tags:

jmockit

I dont think minInvocation or maxInvocation is equivalent to times() in Mockito. Is there?

Please see this questions: Major difference between: Mockito and JMockIt

which has not been answered yet by anyone.

Edit

I found the answer myself: Adding it here for others who need this answered:

The solution is to use DynamicPartialMocking and pass the object to the constructor of the Expectations or NonStrictExpectations and not call any function on that object.

Then in the Verifications section, call any function on the object for which you want to measure the number of invocations and set times = the value you want

new NonStrictExpectations(Foo.class, Bar.class, zooObj)
{
    {
        // don't call zooObj.method1() here
        // Otherwise it will get stubbed out
    }
};


new Verifications()
{
    {
        zooObj.method1(); times = N;
    }
};
like image 513
user855 Avatar asked Oct 09 '11 02:10

user855


1 Answers

I found the answer myself: Adding it here for others who need this answered:

The solution is to use DynamicPartialMocking and pass the object to the constructor of the Expectations or NonStrictExpectations and not call any function on that object.

Then in the Verifications section, call any function on the object for which you want to measure the number of invocations and set times = the value you want

new NonStrictExpectations(Foo.class, Bar.class, zooObj)
{
    {
        // don't call zooObj.method1() here
        // Otherwise it will get stubbed out
    }
};


new Verifications()
{
    {
        zooObj.method1(); times = N;
    }
};
like image 198
user855 Avatar answered Nov 12 '22 19:11

user855