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 an issue that Android Instrumentation Test fails #895

Merged
merged 12 commits into from Aug 29, 2022
Merged
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/buildsrc/config/Deps.kt
Expand Up @@ -24,6 +24,8 @@ object Deps {
const val dexmaker = "2.28.1"
const val androidxEspresso = "3.4.0"
const val androidxTestRules = "1.4.0"
const val androidxTestRunner = "1.4.0"
const val androidxTestExtJunit = "1.1.3"
}

object Libs {
Expand Down
@@ -1,15 +1,10 @@
package buildsrc.convention

import buildsrc.config.Deps
import org.gradle.jvm.tasks.Jar

plugins {
id("com.android.application")

kotlin("android")
kotlin("kapt")
kotlin("plugin.allopen")

id("org.jetbrains.dokka")

id("buildsrc.convention.base")
Expand All @@ -18,10 +13,6 @@ plugins {
android {
compileSdkVersion = "android-32"

kotlinOptions {
jvmTarget = Deps.Versions.jvmTarget.toString()
}

lint {
abortOnError = false
disable += "InvalidPackage"
Expand Down
Expand Up @@ -36,6 +36,11 @@ android {
defaultConfig {
minSdk = 21
targetSdk = 32
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

testOptions {
execution = "ANDROIDX_TEST_ORCHESTRATOR"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Using Test Orchestrator makes Instrumentation Test stable. Documentation

}

compileOptions {
Expand All @@ -44,6 +49,20 @@ android {
}
}

dependencies {
testImplementation("junit:junit:${Deps.Versions.junit4}")
androidTestImplementation("androidx.test.espresso:espresso-core:${Deps.Versions.androidxEspresso}") {
exclude("com.android.support:support-annotations")
}

androidTestImplementation("androidx.test:rules:${Deps.Versions.androidxTestRules}")
androidTestImplementation("androidx.test:runner:${Deps.Versions.androidxTestRunner}")
androidTestImplementation("androidx.test.ext:junit-ktx:${Deps.Versions.androidxTestExtJunit}")

androidTestImplementation(kotlin("test"))
androidTestImplementation(kotlin("test-junit"))
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝I thought it would be better to make the test configs common, so I moved them here.

val javadocJar by tasks.registering(Jar::class) {
from(tasks.dokkaJavadoc)
archiveClassifier.set("javadoc")
Expand Down
8 changes: 0 additions & 8 deletions modules/mockk-agent-android/build.gradle.kts
Expand Up @@ -29,8 +29,6 @@ android {
}

defaultConfig {
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["notAnnotation"] = "io.mockk.test.SkipInstrumentedAndroidTest"
ndk {
abiFilters += setOf("armeabi-v7a", "x86", "x86_64", "arm64-v8a")
}
Expand All @@ -51,12 +49,6 @@ dependencies {
implementation("com.linkedin.dexmaker:dexmaker:${buildsrc.config.Deps.Versions.dexmaker}")
implementation("org.objenesis:objenesis:${buildsrc.config.Deps.Versions.objenesis}")

androidTestImplementation("androidx.test.espresso:espresso-core:${buildsrc.config.Deps.Versions.androidxEspresso}") {
exclude("com.android.support:support-annotations")
}

androidTestImplementation(kotlin("test"))

androidClassesDex(projects.modules.mockkAgentAndroidDispatcher)
}

Expand Down
Expand Up @@ -5,6 +5,7 @@
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.lang.reflect.Method;
import java.util.ArrayList;
Expand All @@ -14,7 +15,10 @@

import static org.junit.Assert.*;

import androidx.test.ext.junit.runners.AndroidJUnit4;

@SuppressWarnings("Duplicates")
@RunWith(AndroidJUnit4.class)
public class AndroidMockKProxyMakerTest {

static boolean[] executed = new boolean[10];
Expand Down
23 changes: 0 additions & 23 deletions modules/mockk-android/build.gradle.kts
Expand Up @@ -11,40 +11,17 @@ description = "Mocking library for Kotlin (Android instrumented test)"
val mavenName: String by extra("MockK Android")
val mavenDescription: String by extra("${project.description}")

@Suppress("UnstableApiUsage")
android {
packagingOptions {
resources {
excludes += "META-INF/LICENSE.md"
excludes += "META-INF/LICENSE-notice.md"
}
}

defaultConfig {
testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments["notAnnotation"] = "io.mockk.test.SkipInstrumentedAndroidTest"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝This class was not included in the test APK and an error log was displayed, so I remove this notAnnotation config.

}

sourceSets {
getByName("androidTest").assets.srcDirs("$projectDir/common/src/test/kotlin")
}
}

dependencies {
api(projects.modules.mockk)
api(projects.modules.mockkAgentApi)
api(projects.modules.mockkAgentAndroid)

testImplementation("junit:junit:${Deps.Versions.junit4}")
androidTestImplementation("androidx.test.espresso:espresso-core:${Deps.Versions.androidxEspresso}") {
exclude(group = "com.android.support", module = "support-annotations")
}
androidTestImplementation(kotlin("reflect"))

androidTestImplementation("androidx.test:rules:${Deps.Versions.androidxTestRules}")

androidTestImplementation(kotlin("test"))
androidTestImplementation(kotlin("test-junit"))
androidTestImplementation(Deps.Libs.junitJupiter)
androidTestImplementation(Deps.Libs.junitVintageEngine)
}
4 changes: 4 additions & 0 deletions modules/mockk-android/src/androidTest/java/TestKeep.java
@@ -1,5 +1,9 @@
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(AndroidJUnit4.class)
public class TestKeep {
@Test
public void neededToRunAutomatedTask() {
Expand Down
@@ -1,9 +1,12 @@
package io.mockk

import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.runner.RunWith
import java.util.concurrent.Callable
import kotlin.test.Test
import kotlin.test.assertEquals

@RunWith(AndroidJUnit4::class)
class MethodDescriptionTest {

/**
Expand All @@ -15,4 +18,4 @@ class MethodDescriptionTest {
every { mock.call() } returns "test"
assertEquals("test", mock.call())
}
}
}
@@ -1,7 +1,9 @@
package io.mockk.ait

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.mockk
import io.mockk.verify
import org.junit.runner.RunWith
import kotlin.test.Test

abstract class MyAbstractClass
Expand All @@ -14,6 +16,7 @@ interface IMockableInterface {
fun doSomethingWithString(s: String)
}

@RunWith(AndroidJUnit4::class)
class MockAbstractArgTest {

@Test
Expand Down Expand Up @@ -45,4 +48,4 @@ class MockAbstractArgTest {
// works
verify { myMock.doSomethingWithInterface(any()) }
}
}
}
@@ -1,10 +1,13 @@
package io.mockk.ait

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.every
import io.mockk.mockk
import org.junit.runner.RunWith
import kotlin.test.Test
import kotlin.test.assertEquals

@RunWith(AndroidJUnit4::class)
class PrePTest {
open class MockCls {
open fun sum(a: Int, b: Int) = a + b
Expand All @@ -18,4 +21,4 @@ class PrePTest {

assertEquals(4, mock.sum(1, 2))
}
}
}
@@ -1,18 +1,14 @@
package io.mockk.proxy.android

import android.widget.FrameLayout
import androidx.test.rule.ActivityTestRule
import io.mockk.debug.TestActivity
import androidx.test.ext.junit.runners.AndroidJUnit4
import io.mockk.mockk
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class AndroidMockKAgentFactoryTest {

@Rule
@JvmField
val rule = ActivityTestRule(TestActivity::class.java)
Comment on lines -12 to -14
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Removed because it's unnecessary.


/**
* This tests that the hidden api logic in [AndroidMockKAgentFactory] works. Otherwise, we would fail when mocking
* the FrameLayout.
Expand Down
9 changes: 0 additions & 9 deletions modules/mockk-android/src/debug/AndroidManifest.xml

This file was deleted.

This file was deleted.