From c1659f2bc056f378e6a589cd0a66b3495f7ac86f Mon Sep 17 00:00:00 2001 From: Madhur Gupta <30932899+madhurgupta10@users.noreply.github.com> Date: Sun, 6 Dec 2020 00:19:25 +0530 Subject: [PATCH] Added Tests for Profile Activity (#4062) --- app/build.gradle | 12 ++++- .../profile/ProfileActivityStartYourself.kt | 34 +++++++++++++ .../commons/profile/ProfileActivityTest.kt | 48 +++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityStartYourself.kt create mode 100644 app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityTest.kt diff --git a/app/build.gradle b/app/build.gradle index ae858db4e..b6114ca51 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -81,15 +81,23 @@ dependencies { testImplementation "org.powermock:powermock-api-mockito2:2.0.0-beta.5" // Unit testing - testImplementation 'junit:junit:4.13' + testImplementation 'junit:junit:4.13.1' testImplementation 'org.robolectric:robolectric:4.3' - testImplementation 'androidx.test:core:1.2.0' + testImplementation 'androidx.test:core:1.3.0' testImplementation "com.squareup.okhttp3:mockwebserver:$OKHTTP_VERSION" testImplementation "org.powermock:powermock-module-junit4:2.0.0-beta.5" testImplementation "org.powermock:powermock-api-mockito2:2.0.0-beta.5" testImplementation 'org.mockito:mockito-core:2.23.0' testImplementation "com.jraska.livedata:testing-ktx:1.1.2" testImplementation "androidx.arch.core:core-testing:2.1.0" + testImplementation "org.junit.jupiter:junit-jupiter-api:5.3.1" + testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.3.1" + + // Mockito and PowerMock + androidTestCompile ('org.powermock:powermock-mockito-release-full:1.6.0') { + exclude module: 'hamcrest-core' + exclude module: 'objenesis' + } // Android testing androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$KOTLIN_VERSION" diff --git a/app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityStartYourself.kt b/app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityStartYourself.kt new file mode 100644 index 000000000..d6fac2078 --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityStartYourself.kt @@ -0,0 +1,34 @@ +package fr.free.nrw.commons.profile + +import android.content.Context +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.powermock.api.mockito.PowerMockito +import org.powermock.core.classloader.annotations.PrepareForTest +import org.powermock.modules.junit4.PowerMockRunner + +@RunWith(PowerMockRunner::class) +@PrepareForTest( + ProfileActivity::class +) +class ProfileActivityStartYourself { + + @Mock + private lateinit var context: Context + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + } + + @Test + @Throws(Exception::class) + fun testStartYourself() { + PowerMockito.mockStatic(ProfileActivity::class.java) + ProfileActivity.startYourself(context) + + } +} \ No newline at end of file diff --git a/app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityTest.kt b/app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityTest.kt new file mode 100644 index 000000000..20724906e --- /dev/null +++ b/app/src/test/kotlin/fr/free/nrw/commons/profile/ProfileActivityTest.kt @@ -0,0 +1,48 @@ +package fr.free.nrw.commons.profile + +import android.content.Context +import fr.free.nrw.commons.TestCommonsApplication +import org.junit.Assert +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mock +import org.mockito.MockitoAnnotations +import org.powermock.api.mockito.PowerMockito.mockStatic +import org.robolectric.Robolectric +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment +import org.robolectric.annotation.Config + + +@RunWith(RobolectricTestRunner::class) +@Config(sdk = [21], application = TestCommonsApplication::class) +class ProfileActivityTest { + + private lateinit var activity: ProfileActivity + + @Mock + private lateinit var context: Context + + @Before + fun setUp() { + MockitoAnnotations.initMocks(this) + + activity = Robolectric.buildActivity(ProfileActivity::class.java).create().get() + + context = RuntimeEnvironment.application.applicationContext + } + + @Test + @Throws(Exception::class) + fun checkActivityNotNull() { + Assert.assertNotNull(activity) + } + + @Test + @Throws(Exception::class) + fun testOnDestroy() { + activity.onDestroy() + } + +} \ No newline at end of file