Search code examples
jmockit

In JMockit, how to specify times for a series of calls?


I have something like:

new Expectations() {{
    mock0.f();
    mock1.f();

    mock0.f();
    mock1.f();

    mock0.f();
    mock1.f();
}};

Is there a way I can use the 'times =' specification? Something like:

new Expectations() {{
    {
        mock0.f();
        mock1.f();
    }
    times = 3;
}};

I /could/ have a loop in my Expectations, but I really hate cyclomatic complexities greater than one in my unit tests.

Note that I want to keep using strict expectations.


Solution

  • You can use the Expectations constructor which takes a numberOfIterations argument:

    new Expectations(3) {{
        {
            mock0.f();
            mock1.f();
        }
    }};
    

    NonStrictExpectations and Verifications also support it.