-1
@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.

Anil Punia
  • 37
  • 3
  • First, you should be aware that `NonStrictExpectations` has been removed from the most recent versions of JMockit. You should clarify what version you are using. Secondly, you should provide the stack trace of the `MissingInvocationException` -- it may not be saying what you think it's saying. Third, you should always strive to provide an example we can actual replicate and see the problem in action. There's nothing wrong *per se* with the above fragment, but it's not exactly something I can debug... – dcsohl Dec 15 '16 at 17:03
  • You may need to use `maxTimes = 1`. Take a look at http://jmockit.org/tutorial/Mocking.html#constraints – Alfergon Dec 16 '16 at 09:05
  • @dcsohl I am using jmockit 1.8, It doesn't matter whether I use strict or non Strict Expectations in my scenario. If I mock method in Expectations or NonStrictExpectations block, jmockit expects atleast 1 invocation of that method, If there mocked method is not invoked then it throw MissingInvocationException. I worked around this problem by using new MockUp. – Anil Punia Dec 16 '16 at 10:15
  • With `Expectations` or `StrictExpectations` I would absolutely expect the behaviour you are describing. It shouldn't happen with `NonStrictExpectations` though. Unfortunately, you never provided an actual example, though I am glad you have managed to work around it. – dcsohl Dec 16 '16 at 15:53

1 Answers1

-1

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.

Anil Punia
  • 37
  • 3