Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test coverage #906

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ jobs:
run: java -version
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Download robolectric dependencies
run: ./download-robolectric-deps.sh
- name: Run unit tests with Gradle
run: ./gradlew :opensrp-core:clean :opensrp-core:jacocoTestReport --stacktrace
- name: Generate Javadoc with Gradle
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ opensrp-path/fabric.properties
opensrp-path/reports/
opensrp-core/reports/
opensrp-core/jacoco.exec
robolectric-deps/
18 changes: 18 additions & 0 deletions download-robolectric-deps.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh
mkdir robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/10-robolectric-5803371/android-all-10-robolectric-5803371.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/8.1.0-robolectric-4611349/android-all-8.1.0-robolectric-4611349.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all-instrumented/8.1.0-robolectric-4611349-i4/android-all-instrumented-8.1.0-robolectric-4611349-i4.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/8.0.0_r4-robolectric-r1/android-all-8.0.0_r4-robolectric-r1.jar -P robolectric-deps

wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/6.0.1_r3-robolectric-r1/android-all-6.0.1_r3-robolectric-r1.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all-instrumented/5.1.1_r9-robolectric-r2-i4/android-all-instrumented-5.1.1_r9-robolectric-r2-i4.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/7.0.0_r1-robolectric-r1/android-all-7.0.0_r1-robolectric-r1.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/7.1.0_r7-robolectric-r1/android-all-7.1.0_r7-robolectric-r1.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/9-robolectric-4913185-2/android-all-9-robolectric-4913185-2.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/11-robolectric-6757853/android-all-11-robolectric-6757853.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all-instrumented/12-robolectric-7732740-i4/android-all-instrumented-12-robolectric-7732740-i4.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all-instrumented/12.1-robolectric-8229987-i4/android-all-instrumented-12.1-robolectric-8229987-i4.jar -P robolectric-deps
wget -nc https://repo1.maven.org/maven2/org/robolectric/android-all/5.1.1_r9-robolectric-r2/android-all-5.1.1_r9-robolectric-r2.jar -P robolectric-deps

cp robolectric-deps.properties opensrp-core/src/test/resources
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ POM_SETTING_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
POM_SETTING_LICENCE_DIST=repo
POM_SETTING_DEVELOPER_ID=opensrp
POM_SETTING_DEVELOPER_NAME=OpenSRP Onadev

android.useAndroidX=true
android.enableJetifier=true
org.gradle.jvmargs=-Xmx2048m
Expand Down
28 changes: 24 additions & 4 deletions opensrp-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ apply plugin: 'com.android.library'
apply plugin: 'jacoco'
apply plugin: 'com.github.kt3k.coveralls'
apply plugin: 'maven-publish'
apply plugin: 'kotlin-android'

jacoco {
toolVersion = "0.8.8"
Expand Down Expand Up @@ -87,12 +88,14 @@ android {
targetSdkVersion androidTargetSdkVersion
versionCode Integer.parseInt(project.VERSION_CODE)
versionName project.VERSION_NAME
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
buildConfigField "long", "BUILD_TIMESTAMP", System.currentTimeMillis() + "L"
buildConfigField "int", "DB_ENCRYPTION_VERSION", '1'
buildConfigField("int", "VERSION_CODE", "${defaultConfig.versionCode}")
buildConfigField("String", "VERSION_NAME", "\"${defaultConfig.versionName}\"")
}
/*

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
Expand All @@ -102,8 +105,12 @@ android {
jniLibs.srcDirs 'src/main/native'
resources.srcDir 'resources'
}
test {
java.srcDir 'tests'
}
androidTest.setRoot('src/androidTest')
}
*/

aaptOptions {
noCompress 'apk'
Expand All @@ -120,6 +127,7 @@ android {
includeAndroidResources = true
}
}

}

import org.gradle.api.tasks.testing.logging.TestExceptionFormat
Expand All @@ -130,13 +138,23 @@ tasks.withType(Test) {
jacoco.excludes = ['jdk.internal.*']
testLogging {
// Enables easier debugging of tests in Github Actions CI
events TestLogEvent.FAILED
events TestLogEvent.FAILED, TestLogEvent.SKIPPED, TestLogEvent.PASSED
exceptionFormat TestExceptionFormat.FULL
showExceptions true
showCauses true
showStackTraces true
}
maxHeapSize = "4g"

test {
beforeTest { testDescriptor ->
println "${testDescriptor.className} > ${testDescriptor.name} STARTED"
}
afterTest { testDescriptor ->
println "${testDescriptor.className} > ${testDescriptor.name} ENDED"

}
}
}

def filesToCopy = copySpec {
Expand Down Expand Up @@ -270,6 +288,8 @@ dependencies {
// Add the dependency for the Performance Monitoring library
testImplementation 'com.google.firebase:firebase-perf'

testImplementation(project(":opensrp-core"))

// PowerMock
def powerMockVersion = '2.0.9'
testImplementation "org.powermock:powermock-core:$powerMockVersion"
Expand Down Expand Up @@ -320,5 +340,5 @@ coveralls {
sourceDirs = ["$project.projectDir/src/main/java"]
}

apply from: '../maven.gradle'
apply plugin: 'kotlin-android'

apply from: '../maven.gradle'
Empty file modified opensrp-core/gradlew
100644 → 100755
Empty file.
12 changes: 12 additions & 0 deletions opensrp-core/robolectric-deps.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
org.robolectric\:android-all-instrumented\:5.1.1_r9-robolectric-r2-i4=../../../../../../robolectric-deps/android-all-instrumented-5.1.1_r9-robolectric-r2-i4.jar
org.robolectric\:android-all\:6.0.1_r3-robolectric-r1=../../../../../../robolectric-deps/android-all-6.0.1_r3-robolectric-r1.jar
org.robolectric\:android-all\:7.0.0_r1-robolectric-r1=../../../../../../robolectric-deps/android-all-7.0.0_r1-robolectric-r1.jar
org.robolectric\:android-all\:7.1.0_r7-robolectric-r1=../../../../../../robolectric-deps/android-all-7.1.0_r7-robolectric-r1.jar
org.robolectric\:android-all\:8.0.0_r4-robolectric-r1=../../../../../../robolectric-deps/android-all-8.0.0_r4-robolectric-r1.jar
org.robolectric\:android-all\:8.1.0-robolectric-4611349=../../../../../../robolectric-deps/android-all-8.1.0-robolectric-4611349.jar
org.robolectric\:android-all-instrumented\:8.1.0-robolectric-4611349-i4=../../../../../../robolectric-deps/android-all-instrumented-8.1.0-robolectric-4611349-i4.jar
org.robolectric\:android-all\:9-robolectric-4913185-2=../../../../../../robolectric-deps/android-all-9-robolectric-4913185-2.jar
org.robolectric\:android-all\:10-robolectric-5803371=../../../../../../robolectric-deps/android-all-10-robolectric-5803371.jar
org.robolectric\:android-all\:11-robolectric-6757853=../../../../../../robolectric-deps/android-all-11-robolectric-6757853.jar
org.robolectric\:android-all-instrumented\:12-robolectric-7732740-i4=../../../../../../robolectric-deps/android-all-instrumented-12-robolectric-7732740-i4.jar
org.robolectric\:android-all-instrumented\:12.1-robolectric-8229987-i4=../../../../../../robolectric-deps/android-all-instrumented-12.1-robolectric-8229987-i4.jar
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public void testGenerateDefaultLocationHierarchyWithRegisteredANMNotSetReturnsNu
LocationHelper spyLocationHelper = Mockito.spy(locationHelper);
AllSharedPreferences spiedAllSharedPreferences = Mockito.spy((AllSharedPreferences) ReflectionHelpers.getField(spyLocationHelper, "allSharedPreferences"));
ReflectionHelpers.setField(spyLocationHelper, "allSharedPreferences", spiedAllSharedPreferences);
Mockito.doReturn("demo").when(spiedAllSharedPreferences).fetchRegisteredANM();

ANMLocationController anmLocationController = Mockito.spy(CoreLibrary.getInstance().context().anmLocationController());
ReflectionHelpers.setField(CoreLibrary.getInstance().context(), "anmLocationController", anmLocationController);
Expand All @@ -242,7 +243,7 @@ public void testGenerateDefaultLocationHierarchyWithRegisteredANMNotSetReturnsNu
List<String> allowedLevels = Arrays.asList("District", "Village");
List<String> result = spyLocationHelper.generateDefaultLocationHierarchy(allowedLevels);

Mockito.verify(spiedAllSharedPreferences).fetchDefaultLocalityId(Mockito.eq(""));
Mockito.verify(spiedAllSharedPreferences).fetchDefaultLocalityId(Mockito.eq("demo"));
Mockito.verify(spyLocationHelper).getDefaultLocationHierarchy(Mockito.isNull(), Mockito.any(), Mockito.anyList(), Mockito.eq(allowedLevels), Mockito.eq(false));
assertNull(result);
}
Expand Down
Loading