Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2a7b420eef76
Choose a base ref
...
head repository: openmrs/openmrs-core
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 97464e2a89c0
Choose a head ref
  • 6 commits
  • 8 files changed
  • 2 contributors

Commits on Jun 6, 2013

  1. Copy the full SHA
    6249afd View commit details

Commits on Jun 7, 2013

  1. TRUNK-3586 Fixed tests

    rkorytkowski committed Jun 7, 2013
    Copy the full SHA
    2525aaf View commit details

Commits on Jun 17, 2013

  1. Copy the full SHA
    2466cce View commit details
  2. Copy the full SHA
    7519ccc View commit details
  3. Copy the full SHA
    b29820b View commit details
  4. Merge pull request #334 from rkorytkowski/TRUNK-3586

    TRUNK-3586 Update unit testing environment to use mocks
    rkorytkowski committed Jun 17, 2013
    Copy the full SHA
    97464e2 View commit details
2 changes: 1 addition & 1 deletion api/src/main/java/org/openmrs/api/context/Context.java
Original file line number Diff line number Diff line change
@@ -249,7 +249,7 @@ public static UserContext getUserContext() {
*
* @return the current ServiceContext
*/
private static ServiceContext getServiceContext() {
static ServiceContext getServiceContext() {
if (serviceContext == null) {
log.error("serviceContext is null. Creating new ServiceContext()");
serviceContext = ServiceContext.getInstance();
26 changes: 8 additions & 18 deletions api/src/test/java/org/openmrs/EncounterTest.java
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.util.ArrayList;
import java.util.Arrays;
@@ -32,30 +31,26 @@

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.openmrs.api.EncounterService;
import org.openmrs.api.ProviderService;
import org.openmrs.api.context.Context;
import org.openmrs.test.BaseContextMockTest;
import org.openmrs.test.Verifies;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

/**
* This class tests the all of the {@link Encounter} non-trivial object methods.
*
* @see Encounter
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(Context.class)
public class EncounterTest {
public class EncounterTest extends BaseContextMockTest {

@Before
public void before() {
mockStatic(Context.class);
when(Context.getAuthenticatedUser()).thenReturn(new User());
}
@Mock
EncounterService encounterService;

@Mock
ProviderService providerService;

/**
* @see {@link Encounter#toString()}
@@ -1235,15 +1230,10 @@ public void setProvider_shouldSetExistingProviderForUnknownRole() throws Excepti
List<Provider> providers = new ArrayList<Provider>();
providers.add(provider);

EncounterService encounterService = mock(EncounterService.class);
when(encounterService.getEncounterRoleByUuid(EncounterRole.UNKNOWN_ENCOUNTER_ROLE_UUID)).thenReturn(unknownRole);

ProviderService providerService = mock(ProviderService.class);
when(providerService.getProvidersByPerson(person)).thenReturn(providers);

when(Context.getEncounterService()).thenReturn(encounterService);
when(Context.getProviderService()).thenReturn(providerService);

//when
encounter.setProvider(person);

4 changes: 2 additions & 2 deletions api/src/test/java/org/openmrs/OpenmrsTestsTest.java
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ public void shouldHaveTestAnnotationWhenStartingWithShould() throws Exception {
@Test
public void shouldHaveClassNameEndWithTestIfContainsMethodTestAnnotations() throws Exception {
// loop over all methods that _don't_ end in Test.class
for (Class<?> currentClass : getClasses("^.*(?<!Test)\\.class$")) {
for (Class<?> currentClass : getClasses("^.*(?<!Test|IT)\\.class$")) {

// skip over classes that are @Ignore'd
if (currentClass.getAnnotation(Ignore.class) == null) {
@@ -140,7 +140,7 @@ public void shouldHaveClassNameEndWithTestIfContainsMethodTestAnnotations() thro
* @return list of classes whose name ends with Test.class
*/
private List<Class> getTestClasses() {
return getClasses(".*Test\\.class$");
return getClasses(".*(Test|IT)\\.class$");
}

/**
220 changes: 220 additions & 0 deletions api/src/test/java/org/openmrs/api/context/ContextMockHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/**
* The contents of this file are subject to the OpenMRS Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://license.openmrs.org
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* Copyright (C) OpenMRS, LLC. All Rights Reserved.
*/
package org.openmrs.api.context;

import org.mockito.InjectMocks;
import org.openmrs.api.AdministrationService;
import org.openmrs.api.CohortService;
import org.openmrs.api.ConceptService;
import org.openmrs.api.DatatypeService;
import org.openmrs.api.EncounterService;
import org.openmrs.api.FormService;
import org.openmrs.api.LocationService;
import org.openmrs.api.ObsService;
import org.openmrs.api.OrderService;
import org.openmrs.api.PatientService;
import org.openmrs.api.ProviderService;
import org.openmrs.api.ReportService;
import org.openmrs.api.SerializationService;
import org.openmrs.api.UserService;
import org.openmrs.api.VisitService;
import org.openmrs.messagesource.MessageSourceService;

import java.util.HashMap;
import java.util.Map;

/**
* Helps to mock or spy on services. It can be used with {@link InjectMocks}. See
* {@link org.openmrs.module.ModuleUtilTest} for example. In general you should always try to refactor code first so
* that this class is not needed. In practice it is mostly enough to replace calls to
* Context.get...Service with fields, which are injected through a constructor.
*
* @deprecated Avoid using this by not calling Context.get...Service() in your code.
* @since 1.10
*/
@Deprecated
public class ContextMockHelper {

/**
* Mockito does not call setters if there are no fields so you must add both a setter and a
* field.
*/
AdministrationService administrationService;

CohortService cohortService;

ConceptService conceptService;

DatatypeService datatypeService;

EncounterService encounterService;

FormService formService;

LocationService locationService;

MessageSourceService messageSourceService;

ObsService obsService;

OrderService orderService;

PatientService patientService;

ProviderService providerService;

ReportService reportService;

SerializationService serializationService;

UserService userService;

VisitService visitService;

UserContext userContext;

Map<Class<?>, Object> realServices = new HashMap<Class<?>, Object>();

UserContext realUserContext;

boolean userContextMocked = false;

public ContextMockHelper() {
}

public void revertMocks() {
for (Map.Entry<Class<?>, Object> realService : realServices.entrySet()) {
Context.getServiceContext().setService(realService.getKey(), realService.getValue());
}
realServices.clear();

if (userContextMocked) {
Context.setUserContext(realUserContext);
realUserContext = null;
userContextMocked = false;
}
}

public void setService(Class<?> type, Object service) {
if (!realServices.containsKey(type)) {
Object realService = null;
try {
realService = Context.getService(type);
}
catch (Exception e) {
//let's not fail if context is not configured
}

realServices.put(type, realService);
}

Context.getServiceContext().setService(type, service);
}

public void setAdministrationService(AdministrationService administrationService) {
setService(AdministrationService.class, administrationService);
this.administrationService = administrationService;
}

public void setCohortService(CohortService cohortService) {
setService(CohortService.class, cohortService);
this.cohortService = cohortService;
}

public void setConceptService(ConceptService conceptService) {
setService(ConceptService.class, conceptService);
this.conceptService = conceptService;
}

public void setDatatypeService(DatatypeService datatypeService) {
setService(DatatypeService.class, datatypeService);
this.datatypeService = datatypeService;
}

public void setEncounterService(EncounterService encounterService) {
setService(EncounterService.class, encounterService);
this.encounterService = encounterService;
}

public void setFormService(FormService formService) {
setService(FormService.class, formService);
this.formService = formService;
}

public void setLocationService(LocationService locationService) {
setService(LocationService.class, locationService);
this.locationService = locationService;
}

public void setMessageSourceService(MessageSourceService messageSourceService) {
setService(MessageSourceService.class, messageSourceService);
this.messageSourceService = messageSourceService;
}

public void setObsService(ObsService obsService) {
setService(ObsService.class, obsService);
this.obsService = obsService;
}

public void setOrderService(OrderService orderService) {
setService(OrderService.class, orderService);
this.orderService = orderService;
}

public void setPatientService(PatientService patientService) {
setService(PatientService.class, patientService);
this.patientService = patientService;
}

public void setProviderService(ProviderService providerService) {
setService(ProviderService.class, providerService);
this.providerService = providerService;
}

public void setReportService(ReportService reportService) {
setService(ReportService.class, reportService);
this.reportService = reportService;
}

public void setSerializationService(SerializationService serializationService) {
setService(SerializationService.class, serializationService);
this.serializationService = serializationService;
}

public void setUserService(UserService userService) {
setService(UserService.class, userService);
this.userService = userService;
}

public void setVisitService(VisitService visitService) {
setService(VisitService.class, visitService);
this.visitService = visitService;
}

public void setUserContext(UserContext userContext) {
if (!userContextMocked) {
try {
realUserContext = Context.getUserContext();
}
catch (Exception e) {
//let's not fail if context is not configured
}
userContextMocked = true;
}

Context.setUserContext(userContext);
this.userContext = userContext;
}

}
Loading