Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic Android instrumented and unit tests (#2360)
As part of #2341 Co-authored-by: Tim van der Lippe <tvanderlippe@google.com>
- Loading branch information
1 parent
123beb8
commit d4790f7
Showing
37 changed files
with
769 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
org.gradle.daemon=true | ||
org.gradle.parallel=true | ||
org.gradle.caching=true | ||
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8 | ||
# AndroidX package structure to make it clearer which packages are bundled with the | ||
# Android operating system, and which are packaged with your app"s APK | ||
# https://developer.android.com/topic/libraries/support-library/androidx-rn | ||
android.useAndroidX=true | ||
# Kotlin code style for this project: "official" or "obsolete": | ||
kotlin.code.style=official |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionSha256Sum=e58cdff0cee6d9b422dcd08ebeb3177bc44eaa09bd9a2e838ff74c408fe1cbcd | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-bin.zip | ||
distributionSha256Sum=3239b5ed86c3838a37d983ac100573f64c1f3fd8e1eb6c89fa5f9529b5ec091d | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/captures | ||
.externalNativeBuild | ||
.cxx | ||
local.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Android Testing Project | ||
## Overview | ||
Unlike the other subprojects, this project is designed to run against the Android toolchain rather than standard Kotlin/Java. This means it should be opened in Android Studio, rather than IntelliJ. | ||
|
||
This project was introduced to provide a basic level of assurance for Mockito's correct operation on the Android platform. | ||
|
||
This project features the following: | ||
* the minimum app project prerequisites - it is not intended to be run as an actual app | ||
* basic JUnit tests | ||
* basic instrumented tests | ||
|
||
While JUnit tests run on the local machine, instrumented tests in the Android context run on a device, which can be an emulator. Due to the Android runtime design, the implementation options for instrumented tests are constrained compared to normal JUnit tests; for example, it is not possible to mock final classes. | ||
|
||
## Library Version Definitions / ByteBuddy | ||
|
||
Libraries and their versions are defined in `ext.library-versions.gradle`. It is here that you can specify which version of Mockito to test. | ||
|
||
ByteBuddy is an optional declaration. If you specify '0' for the ByteBuddy version, e.g. | ||
|
||
`bytebuddy_version = '0'` | ||
|
||
then ByteBuddy will be as declared by Mockito's own dependencies. | ||
|
||
However you are able to specify a _newer_ version if you would like to test an update, and you can do this by specifying a real version, e.g.: | ||
|
||
`bytebuddy_version = '1.11.7'` | ||
|
||
This will cause ByteBuddy to be directly included in this project using the specified version. | ||
|
||
This override is subject to Gradle's dependency resolution process where it reconciles different versions - Mockito's library inclusion vs. the project one. This usually means the newest version is accepted; therefore if you specify a version of ByteBuddy that's _older_ than Mockito's one, it's unlikely to apply it. You would need to use Gradle's `resolutionStrategy` definition to force this. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Top-level build file where you can add configuration options common to all sub-projects/modules. | ||
|
||
plugins { | ||
id 'com.android.application' | ||
id 'kotlin-android' | ||
} | ||
|
||
android { | ||
compileSdkVersion 30 | ||
buildToolsVersion "30.0.3" | ||
|
||
defaultConfig { | ||
applicationId "org.mockitousage.androidtest" | ||
minSdkVersion 21 | ||
targetSdkVersion 30 | ||
versionCode 1 | ||
versionName "1.0" | ||
|
||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
} | ||
|
||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
compileOptions { | ||
sourceCompatibility JavaVersion.VERSION_1_8 | ||
targetCompatibility JavaVersion.VERSION_1_8 | ||
} | ||
kotlinOptions { | ||
jvmTarget = '1.8' | ||
} | ||
} | ||
|
||
apply from: "$rootDir/gradle/dependencies.gradle" | ||
|
||
dependencies { | ||
implementation libraries.kotlin.stdlib | ||
implementation libraries.android.ktx | ||
implementation libraries.android.compat | ||
implementation libraries.android.material | ||
|
||
testImplementation project(":inline") | ||
testImplementation libraries.junit4 | ||
testImplementation libraries.junitJupiterApi | ||
testImplementation libraries.junitJupiterEngine | ||
|
||
androidTestImplementation libraries.android.junit | ||
androidTestImplementation libraries.android.espresso | ||
androidTestImplementation project(":android") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
78 changes: 78 additions & 0 deletions
78
...s/androidTest/src/androidTest/java/org/mockitousage/androidtest/BasicInstrumentedTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.mockitousage.androidtest | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import org.junit.After | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mock | ||
import org.mockito.MockitoAnnotations | ||
import org.mockito.Mockito.mock | ||
import org.mockito.Mockito.verify | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class BasicInstrumentedTests { | ||
|
||
private var closeable: AutoCloseable? = null | ||
|
||
@Mock private lateinit var mockedViaAnnotationBasicOpenClass: BasicOpenClass | ||
@Mock private lateinit var mockedViaAnnotationBasicInterface: BasicInterface | ||
|
||
@Before | ||
fun setup() { | ||
closeable = MockitoAnnotations.openMocks(this) | ||
} | ||
|
||
@After | ||
@Throws(Exception::class) | ||
fun releaseMocks() { | ||
closeable?.close() | ||
} | ||
|
||
//Open class | ||
|
||
@Test | ||
fun mockAndUseBasicClassUsingAnnotatedMock() { | ||
val basicClass = BasicOpenClassReceiver(mockedViaAnnotationBasicOpenClass) | ||
basicClass.callDependencyMethod() | ||
throw Exception("java") | ||
} | ||
|
||
@Test | ||
fun mockAndUseBasicClassUsingLocalMock() { | ||
val basicOpenClass = mock(BasicOpenClass::class.java) | ||
val basicReceiver = BasicOpenClassReceiver(basicOpenClass) | ||
basicReceiver.callDependencyMethod() | ||
} | ||
|
||
@Test | ||
fun mockAndUseBasicClassWithVerify() { | ||
val basicClass = BasicOpenClassReceiver(mockedViaAnnotationBasicOpenClass) | ||
basicClass.callDependencyMethod() | ||
verify(mockedViaAnnotationBasicOpenClass).emptyMethod() | ||
} | ||
|
||
//Interface | ||
|
||
@Test | ||
fun mockAndUseBasicInterfaceUsingAnnotatedMock() { | ||
val receiver = BasicInterfaceReceiver(mockedViaAnnotationBasicInterface) | ||
receiver.callInterfaceMethod() | ||
verify(mockedViaAnnotationBasicInterface).interfaceMethod() | ||
} | ||
|
||
@Test | ||
fun mockAndUseBasicInterfaceUsingLocalMock() { | ||
val basicInterface = mock(BasicInterface::class.java) | ||
val receiver = BasicInterfaceReceiver(basicInterface) | ||
receiver.callInterfaceMethod() | ||
} | ||
|
||
@Test | ||
fun mockAndUseBasicInterfaceAndVerify() { | ||
val basicInterface = mock(BasicInterface::class.java) | ||
val receiver = BasicInterfaceReceiver(basicInterface) | ||
receiver.callInterfaceMethod() | ||
verify(basicInterface).interfaceMethod() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="org.mockitousage.androidtest"> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/Theme.MockitoAndroidTest" /> | ||
|
||
</manifest> |
Oops, something went wrong.