Skip to content

Commit

Permalink
Merge pull request #276 from opensrp/fix-failing-tests
Browse files Browse the repository at this point in the history
Fix failing tests
  • Loading branch information
roywanyaga authored Feb 17, 2022
2 parents 2942ac6 + 0b0b8b7 commit 626b6bd
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 20 deletions.
3 changes: 3 additions & 0 deletions opensrp-child/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ android {
includeAndroidResources = true
returnDefaultValues = true
}
unitTests.all {
jvmArgs "-Xmx4g"
}
}

compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ public void setUp() {
@After
public void tearDown() {
ReflectionHelpers.setStaticField(ChildLibrary.class, "instance", null);

try {
childFormActivity.finish();
} catch (Exception e){
e.printStackTrace();
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import org.apache.commons.lang3.tuple.Triple;
import org.joda.time.DateTime;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -76,6 +77,15 @@ public void setUp() {
boosterImmunizationsLayout = new LinearLayout(appCompatActivity);
}

@After
public void tearDown(){
try {
appCompatActivity.finish();
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testNewInstance() {
Bundle bundle = new Bundle();
Expand Down Expand Up @@ -206,7 +216,6 @@ public void testThatLayoutIsNotChangedAfterUndoingVaccine(){
ChildUnderFiveFragment fragment = initFragment();
fragment.onVaccineUpdated(new ExtraVaccineUpdateEvent(entityId, vaccine, vaccineDate, true));
assertEquals(((ViewGroup) boosterImmunizationsLayout).getChildCount(), 0);

}

private ChildUnderFiveFragment initFragment() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import androidx.appcompat.app.AppCompatActivity;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
Expand Down Expand Up @@ -50,6 +51,15 @@ public void setUp() {
extraVaccineFragment = ExtraVaccineEditDialogFragment.newInstance();
}

@After
public void tearDown(){
try {
activity.finish();
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
@Ignore("TODO: Resolve out of memory error")
public void testThatViewsAreInitializedCorrectly() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -63,17 +64,28 @@ public class NoMatchDialogFragmentTest extends BaseUnitTest {

private Context context;

private AppCompatActivity activity;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);

fragment = new NoMatchDialogFragment(baseRegisterActivity, "123");
context = RuntimeEnvironment.application;

AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get();
activity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get();
activity.getSupportFragmentManager().beginTransaction().add(fragment, "Tasks").commit();
}

@After
public void tearDown(){
try {
activity.finish();
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testInstantiation() {
assertNotNull(Whitebox.getInternalState(fragment,"uniqueId"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import androidx.appcompat.app.AppCompatActivity;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -32,11 +33,28 @@ public class StatusEditDialogFragmentTest extends BaseUnitTest {

private StatusEditDialogFragment fragment;

private AppCompatActivity appCompatActivity;

private TestAppCompactActivity testAppCompactActivity;


@Before
public void setUp() {
appCompatActivity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get();
testAppCompactActivity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get();
MockitoAnnotations.initMocks(this);
}

@After
public void tearDown(){
try {
appCompatActivity.finish();
testAppCompactActivity.finish();
} catch (Exception e){
e.printStackTrace();
}
}

@Test
public void testInstantiation() {
Map<String, String> details = new HashMap<>();
Expand All @@ -60,8 +78,7 @@ public void testOnAttachWithCorrectActivity() {

assertNull(Whitebox.getInternalState(fragment, "listener"));

TestAppCompactActivity activity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get();
fragment.show(activity.getFragmentManager(), "DIALOG_TAG");
fragment.show(testAppCompactActivity.getFragmentManager(), "DIALOG_TAG");

assertNotNull(Whitebox.getInternalState(fragment, "listener"));
}
Expand All @@ -77,8 +94,7 @@ public void testOnAttachWithIncorrectActivityThrowsException() {

assertNull(Whitebox.getInternalState(fragment, "listener"));

AppCompatActivity activity = Robolectric.buildActivity(AppCompatActivity.class).create().start().get();
fragment.show(activity.getFragmentManager(), "DIALOG_TAG");
fragment.show(appCompatActivity.getFragmentManager(), "DIALOG_TAG");
}

@Test
Expand All @@ -92,8 +108,7 @@ public void testOnCreateViewWhenLostToFollowUpIsSet() {

fragment = StatusEditDialogFragment.newInstance(detailsMock);

TestAppCompactActivity activity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get();
fragment.show(activity.getFragmentManager(), "DIALOG_TAG");
fragment.show(testAppCompactActivity.getFragmentManager(), "DIALOG_TAG");

Mockito.verify(detailsMock).containsKey(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP);
Mockito.verify(detailsMock, Mockito.atLeastOnce()).get(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP);
Expand All @@ -110,10 +125,10 @@ public void testOnCreateViewWhenInactiveAndLostToFollowUpAreNotSet() {

fragment = StatusEditDialogFragment.newInstance(detailsMock);

TestAppCompactActivity activity = Robolectric.buildActivity(TestAppCompactActivity.class).create().start().get();
fragment.show(activity.getFragmentManager(), "DIALOG_TAG");
fragment.show(testAppCompactActivity.getFragmentManager(), "DIALOG_TAG");

Mockito.verify(detailsMock, Mockito.never()).get(Constants.CHILD_STATUS.INACTIVE);
Mockito.verify(detailsMock, Mockito.never()).get(Constants.CHILD_STATUS.LOST_TO_FOLLOW_UP);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ public class VaccineCalculatorTest extends BaseUnitTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mockImmunizationLibrary(immunizationLibrary, context, vaccineRepository, alertService);
Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD)
;
Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD);
}

@Test
Expand Down Expand Up @@ -199,7 +198,6 @@ private void mockImmunicationLibraryToUseHardcodedVaccines() {
ImmunizationLibrary immunizationLibrary = Mockito.mock(ImmunizationLibrary.class);

ReflectionHelpers.setStaticField(ImmunizationLibrary.class, "instance", immunizationLibrary);
Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD)
;
Mockito.doReturn(VaccineRepo.Vaccine.values()).when(immunizationLibrary).getVaccines(IMConstants.VACCINE_TYPE.CHILD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -15,6 +16,7 @@
import org.mockito.MockitoAnnotations;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
Expand All @@ -29,11 +31,14 @@ public class BCGNotificationDialogTest {
@Mock
private DialogInterface.OnClickListener negativeDialogListener;

private final Activity activity = Robolectric.setupActivity(AppCompatActivity.class);
private Activity activity;
private ActivityController<AppCompatActivity> controller;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
controller = Robolectric.buildActivity(AppCompatActivity.class).create().resume();
activity = controller.get();
bcgNotificationDialog = new BCGNotificationDialog(activity, positiveDialogListener, negativeDialogListener);
}

Expand All @@ -50,4 +55,15 @@ public void testThatAlertDialogIsShown() {
Assert.assertEquals(negativeButton.getText().toString(), "DISMISS");
Assert.assertEquals(String.valueOf(negativeButton.getTextSize()), String.valueOf(20f));
}

@After
public void tearDown() {
try {
bcgNotificationDialog.getAlertDialog().dismiss();
controller.pause().stop().destroy();
activity.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class LookUpTextWatcherTest extends BaseUnitTest {

private JsonFormFragment formFragment;

private ChildFormActivityShadow childFormActivityShadow;

@Mock
private ChildLibrary childLibrary;

Expand All @@ -63,7 +65,7 @@ public void setUp() throws JSONException {
ReflectionHelpers.setStaticField(ChildLibrary.class, "instance", childLibrary);

formFragment = Mockito.spy(ChildFormFragment.class);
ChildFormActivityShadow childFormActivityShadow = Robolectric.buildActivity(ChildFormActivityShadow.class).get();
childFormActivityShadow = Robolectric.buildActivity(ChildFormActivityShadow.class).get();
String formJson = "{\"count\":\"1\",\"encounter_type\":\"Birth Registration\",\"step1\":{\"title\":\"{{child_enrollment.step1.title}}\"," +
"\"fields\":[{\"key\":\"first_name\",\"openmrs_entity_parent\":\"\",\"openmrs_entity\":\"person\",\"openmrs_entity_id\":\"first_name\"," +
"\"entity_id\":\"mother\",\"look_up\":\"true\",\"type\":\"edit_text\",\"hint\":\"First name\",\"edit_type\":\"name\"}," +
Expand Down Expand Up @@ -135,6 +137,11 @@ public void testAfterTextChangedShouldNotLookUpAfterIfAfterLookUpIsTrue() {
@After
public void tearDown() {
ReflectionHelpers.setStaticField(ChildLibrary.class, "instance", null);
try {
childFormActivityShadow.finish();
} catch (Exception e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import androidx.appcompat.app.AppCompatActivity;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -23,14 +24,15 @@
public class AdvanceSearchDatePickerDialogTest {

private AdvanceSearchDatePickerDialog datePickerDialog;
private AppCompatActivity startActivity;

@Before
public void setUp() {
AppCompatActivity startActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get();
startActivity = Robolectric.buildActivity(AppCompatActivity.class).create().get();
ChildLibrary childLibrary = Mockito.mock(ChildLibrary.class);
Context context = Mockito.mock(Context.class);
AllSharedPreferences allSharedPreferences = Mockito.mock(AllSharedPreferences.class);
AppProperties appProperties = Mockito.spy(AppProperties.class);
AppProperties appProperties = Mockito.mock(AppProperties.class);
Mockito.doReturn(context).when(childLibrary).context();
Mockito.doReturn(allSharedPreferences).when(context).allSharedPreferences();
Mockito.doReturn(appProperties).when(childLibrary).getProperties();
Expand All @@ -44,4 +46,9 @@ public void testGetDatePickerDialog() {
datePickerDialog.showDialog();
Assert.assertNotNull(datePickerDialog.getDatePickerDialog());
}

@After
public void tearDown() {
startActivity.finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

public class ChildEditTextFactoryTest {

Expand All @@ -23,7 +22,7 @@ public class ChildEditTextFactoryTest {
@Mock
private JsonFormFragment formFragment;

@Spy
@Mock
private JsonFormActivity formActivity;

private MaterialEditText materialEditText;
Expand Down

0 comments on commit 626b6bd

Please sign in to comment.