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 @@ -21,6 +21,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

This file was deleted.

@@ -1,5 +1,7 @@
package buildsrc.convention

import buildsrc.config.Deps

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

Expand Down Expand Up @@ -34,6 +36,11 @@ android {
defaultConfig {
minSdk = 26
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 @@ -42,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
11 changes: 10 additions & 1 deletion modules/mockk-agent-android-dispatcher/build.gradle.kts
Expand Up @@ -3,15 +3,24 @@ import buildsrc.config.asProvider
import com.android.build.gradle.internal.tasks.DexMergingTask

plugins {
buildsrc.convention.`android-application`
id("com.android.application")
Copy link
Contributor Author

@kubode kubode Aug 24, 2022

Choose a reason for hiding this comment

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

I removed android-application.gradle.kts since it is not used except for android-dispatcher module.
Instead, I moved the contents of android-application.gradle.kts to this build.gradle.kts.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's intentional that build config that is shareable is defined in buildSrc, even if it's only used in one place. It helps keep the build logic organised and consistent. It makes it easier to compare and contrast the Android Library config to the Android Application config, and easier to see if there's any special treatment that's needed in the subprojects. So I think this should be reverted.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IMHO, I think it is better to remove android-application.gradle.kts.
:mockk-agent-android-dispatcher module is a very specific Android Application module that only extracts the final DEX file, and the build configuration is completely independent and non-reusable.
If we were to add an Android sample application module in the future and try to reuse the configuration with :mockk-agent-android-dispatcher module, it would add an unnecessary dependency to :mockk-agent-android-dispatcher module and cause the same problems that encountered in this PR.

Copy link
Contributor

@aSemy aSemy Aug 24, 2022

Choose a reason for hiding this comment

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

Yeah, I think the dependency on :mockk-agent-android-dispatcher should be removed from the buildSrc script

edit: I read your comment too quickly and my response doesn't make sense! I'll have another look

}

@Suppress("UnstableApiUsage")
android {
compileSdk = 32

defaultConfig {
minSdk = 26
targetSdk = 32
applicationId = "com.android.dexmaker.mockito.inline.dispatcher"
versionCode = 1
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}

val androidClassesDexProvider by configurations.registering {
Expand Down
12 changes: 2 additions & 10 deletions modules/mockk-agent-android/build.gradle.kts
Expand Up @@ -12,6 +12,8 @@ description = "Android instrumented testing MockK inline mocking agent"
val mavenName: String by extra("MockK Android Agent")
val mavenDescription: String by extra("${project.description}")

val dispatcherJarResPath: Provider<Directory> = layout.buildDirectory.dir("generated/dispatcher-jar")

kubode marked this conversation as resolved.
Show resolved Hide resolved
@Suppress("UnstableApiUsage")
android {
externalNativeBuild {
Expand All @@ -29,8 +31,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,17 +51,9 @@ 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)
}

val dispatcherJarResPath: Provider<Directory> = layout.buildDirectory.dir("generated/dispatcher-jar")

val packageDispatcherJar by tasks.registering(Jar::class) {
group = LifecycleBasePlugin.BUILD_GROUP
from(androidClassesDex.asFileTree)
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,43 +11,20 @@ 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 {
implementation(projects.modules.mockk)
implementation(projects.modules.mockkAgentApi)
implementation(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"))

implementation(platform(Deps.Libs.kotlinCoroutinesBom))
implementation(Deps.Libs.kotlinCoroutinesCore)

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.