0

I am new to jmockit and StrictExpectations. Inside StrictExpectations I have recorded invocation and return value of static method of non-mocked class and the static method is mocked correctly but I don't know why it is happening. I think since the class is not mocked how is its invocation and return value is getting recorded in StrictExpectations. My code looks similar to below

@Test
public void test() {
   new StrictExpectations () {{
       DummyClass.someStaticMethod(anyInt);
result = 10;
   }};
   
   assertEquals(10, DummyClass.someStaticMetho(3));
}

My question is even though DummyClass is not defined as a mocked class(something like @Mocked DummyClass d) how we are able to record it's invocation and result.

Nobita
  • 1
  • 2
  • "magic". Is your question effectively: It's doing what you want it to do, but you don't know why? Odds are, you *did* tell it to mock DummyClass somewhere. Perhaps the test class has a "@Injectable" or "@Tested" on DummyClass? Both of those will also mock it. – Jeff Bennett Sep 12 '22 at 08:49
  • Yes, you understood the question correctly but test class don't have any such annotations on it – Nobita Sep 13 '22 at 09:19

1 Answers1

0

Per the documentation (https://jmockit.github.io/tutorial/Mocking.html#injectable): "[S]tatic methods and constructors are also excluded from being mocked. After all, a static method is not associated with any instance of the class, while a constructor is only associated with a newly created (and therefore different) instance."

Thus, you don't have to explicitly Mock a class in order to modify a static-method. It appears just by placing the static-method inside the Expectations-block (in the normal way), you have caused JMockit to override the default-implementation (which would be the obvious intent).

Jeff Bennett
  • 996
  • 7
  • 18