Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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

...

Code Block
languagejava
firstline1125
linenumberstrue
/**
 * @see Encounter#setProvider(Person)
 * @verifies set existing provider for unknown role
 */
@Test
public void setProvider_shouldSetExistingProviderForUnknownRole() throws Exception {
	//given
	Encounter encounter = new Encounter();
	EncounterRole unknownRole = new EncounterRole();
	Person person = new Person();
	Provider provider = new Provider();
	provider.setPerson(person);
	List<Provider> providers = new ArrayList<Provider>();
	providers.add(provider);

	when(encounterService.getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID)).thenReturn(unknownRole);

	when(providerService.getProvidersByPerson(person)).thenReturn(providers);

	//when
	encounter.setProvider(personunknownRole, provider);

	//then
	assertEquals(1, encounter.getProvidersByRoles().size());
	assertEquals(1, encounter.getProvidersByRole(unknownRole).size());
	assertEquals(provider, encounter.getProvidersByRole(unknownRole).iterator().next());
}

...

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.