Nobita
Nobita

Reputation: 1

Jmockit StrictExpectations mocking unmocked static method

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.

Upvotes: 0

Views: 76

Answers (1)

Jeff Bennett
Jeff Bennett

Reputation: 1056

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).

Upvotes: 0

Related Questions