What is the difference between a normal mock and a strict mock in EasyMock?
A) A strict mock enforces the order of method calls
B) A strict mock allows any order of method calls
C) A strict mock is automatically verified at the end of the test
D) A strict mock does not allow any method calls unless specified
Answer:
A) A strict mock enforces the order of method calls
Explanation:
The key difference between a normal mock and a strict mock in EasyMock is that a strict mock enforces the order of method calls. With a strict mock, if the methods are called in a different order than expected, the test will fail.
For example:
MyService strictMockService = EasyMock.createStrictMock(MyService.class);
EasyMock.expect(strictMockService.methodOne()).andReturn(value1);
EasyMock.expect(strictMockService.methodTwo()).andReturn(value2);
EasyMock.replay(strictMockService);
// If methodTwo() is called before methodOne(), the test will fail
In this example, the strict mock strictMockService
will enforce that methodOne()
is called before methodTwo()
.