Skip to content

Commit

Permalink
Increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-vd committed May 17, 2023
1 parent 08182b6 commit 7297bc5
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
import androidx.fragment.app.FragmentActivity;
import android.view.View;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.opensrp.api.constants.Gender;
import org.robolectric.Robolectric;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.growthmonitoring.BaseUnitTest;
import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
import org.smartregister.growthmonitoring.R;
import org.smartregister.growthmonitoring.domain.HeightZScore;
import org.smartregister.growthmonitoring.repository.HeightZScoreRepository;
import org.smartregister.view.customcontrols.CustomFontTextView;

import java.util.ArrayList;

/**
* Created by ndegwamartin on 2020-04-15.
*/
Expand All @@ -34,7 +41,16 @@ public void setUp() {

@Test
public void testThatHeightForAgeViewIsCreated() throws InterruptedException {
ArrayList<HeightZScore> heightZScores = new ArrayList<>();
heightZScores.add(getHeightZScore(56));
heightZScores.add(getHeightZScore(44));
//
HeightZScoreRepository heightZScoreRepository = Mockito.spy(GrowthMonitoringLibrary.getInstance().heightZScoreRepository());
Mockito.doReturn(heightZScores).when(heightZScoreRepository).findByGender(Mockito.any());
ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "heightZScoreRepository", heightZScoreRepository);

HeightMonitoringFragment fragment = Mockito.spy(HeightMonitoringFragment.createInstance("2018-09-12", Gender.FEMALE, getHeights()));
Mockito.doReturn(true).when(fragment).isVisible();
activity.getSupportFragmentManager().beginTransaction().add(fragment, "Height-for-Age-Boys").commitNow();
View view = fragment.getView();
Assert.assertNotNull(view);
Expand All @@ -46,4 +62,16 @@ public void testThatHeightForAgeViewIsCreated() throws InterruptedException {
Assert.assertEquals(constraintLayout.getContext().getString(R.string.height), ((CustomFontTextView)constraintLayout.findViewById(R.id.metric_label)).getText());
Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView)constraintLayout.findViewById(R.id.column_three_metric)).getText());
}

private HeightZScore getHeightZScore(int age) {
HeightZScore heightZScore = new HeightZScore();
heightZScore.setGender(Gender.MALE);
heightZScore.setMonth(age);
return heightZScore;
}

@After
public void destroy(){
ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "heightZScoreRepository", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.opensrp.api.constants.Gender;
import org.robolectric.Robolectric;
Expand All @@ -31,7 +32,8 @@ public void setUp() {

@Test
public void testThatWeightForHeightViewIsCreated() throws InterruptedException {
WeightForHeightMonitoringFragment fragment = WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights());
WeightForHeightMonitoringFragment fragment = Mockito.spy(WeightForHeightMonitoringFragment.createInstance(Gender.MALE, "2018-09-12", getWeights(), getHeights()));
Mockito.doReturn(true).when(fragment).isVisible();
activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Height-Boys").commitNow();
View view = fragment.getView();
Assert.assertNotNull(view);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@

import android.view.View;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.opensrp.api.constants.Gender;
import org.robolectric.Robolectric;
import org.robolectric.util.ReflectionHelpers;
import org.smartregister.growthmonitoring.BaseUnitTest;
import org.smartregister.growthmonitoring.GrowthMonitoringLibrary;
import org.smartregister.growthmonitoring.R;
import org.smartregister.growthmonitoring.domain.WeightZScore;
import org.smartregister.growthmonitoring.repository.WeightZScoreRepository;
import org.smartregister.view.customcontrols.CustomFontTextView;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

/**
* Created by ndegwamartin on 2020-04-15.
*/
Expand All @@ -35,17 +43,38 @@ public void setUp() {

@Test
public void testThatWeightForAgeViewIsCreated() throws InterruptedException {
ArrayList<WeightZScore> weightZScores = new ArrayList<>();
weightZScores.add(getWeightZScore(56));
weightZScores.add(getWeightZScore(44));
//
WeightZScoreRepository weightZScoreRepository = Mockito.spy(GrowthMonitoringLibrary.getInstance().weightZScoreRepository());
Mockito.doReturn(weightZScores).when(weightZScoreRepository).findByGender(Mockito.any());
ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "weightZScoreRepository", weightZScoreRepository);

WeightMonitoringFragment fragment = Mockito.spy(WeightMonitoringFragment.createInstance("2018-09-12", Gender.MALE, getWeights()));
Mockito.doReturn(true).when(fragment).isVisible();
activity.getSupportFragmentManager().beginTransaction().add(fragment, "Weight-for-Age-Boys").commitNow();
View view = fragment.getView();
Assert.assertNotNull(view);
Assert.assertTrue(view instanceof ConstraintLayout);
ConstraintLayout constraintLayout = (ConstraintLayout) view;
Thread.sleep(3000);
TimeUnit.SECONDS.toMillis(20);
Assert.assertEquals(3, constraintLayout.getChildCount());
Assert.assertEquals(constraintLayout.getContext().getString(R.string.age), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_one_metric)).getText());
Assert.assertEquals(constraintLayout.getContext().getString(R.string.weight), ((CustomFontTextView) constraintLayout.findViewById(R.id.metric_label)).getText());
Assert.assertEquals(constraintLayout.getContext().getString(R.string.z_score), ((CustomFontTextView) constraintLayout.findViewById(R.id.column_three_metric)).getText());
}

private WeightZScore getWeightZScore(int age) {
WeightZScore weightZScore = new WeightZScore();
weightZScore.setGender(Gender.MALE);
weightZScore.setMonth(age);
return weightZScore;
}

@After
public void destroy(){
ReflectionHelpers.setField(GrowthMonitoringLibrary.getInstance(), "weightZScoreRepository", null);
}

}

0 comments on commit 7297bc5

Please sign in to comment.