Mocks are simulated objects that mimic the behavior of real objects in controlled ways. OpenMRS 1.9 finally includes Mockito which is a library designated for creating mocks dynamically. In this document we will present how to use them in the context of OpenMRS. The presented code works in OpenMRS 1.9.9+.
...
It is also possible to use @Mock in tests running in the Spring context. The @Mock annotation is recognized in all test classes extending BaseContextSensitiveTest. You can use @Mock to mock out certain services and @Spy to spy on services.
Mocking Context outside BaseContextSensitiveTest
Jira Legacy | ||||||
---|---|---|---|---|---|---|
|
Troubleshooting
If you see an error like this:
...
It means that you called initMocks using static method import in your test. The fix is simply to use MociktoAnnotations.initMocks(this) instead of initMocks(this) or remove initMocks(this) and rely on BaseContextSensitiveTest calling it.