How to mock 0 or 1 invocation with jmockit

539 views Asked by At
@Test
public void test() {
    new NonStrictExpectations() {
            {
                    aService.method1(anyString); result=abc
            }
        };
}

I am using Parameterized runner with jmockit. Now method1 of aService may or may not be invoked depending on test data. but jmockit throws MissingInvocation Exception.

1

There are 1 answers

0
Anil Punia On

In this case as per my new understanding I shouldn't be using Expectation block instead should be using faking and use faked instance in test.

Service aService = new MockUp<Service>() {
            @Mock
            String method(String str){
                return "abc";
            }
        }.getMockInstance();

Now aService can be used in test.