Skip to content
Merged
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
9 changes: 6 additions & 3 deletions android-junit5-tests/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ processTestResources {
}
}

test.testLogging {
events "passed", "skipped", "failed"
exceptionFormat = "full"
test {
failFast = true
testLogging {
events "passed", "skipped", "failed"
exceptionFormat = "full"
}
}

junitPlatform {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,82 @@ class FunctionalSpec extends Specification {
result.output.contains("KotlinAdderTest")
}

def "Returns default values successfully"() {
given:
androidPlugin()
junit5Plugin("""
unitTests {
returnDefaultValues = true
}
""")
test(language: FileLanguage.Java,
content: """
package de.mannodermaus.app;

import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.Test;
import android.content.Intent;

class AndroidTest {
@Test
void test() {
Intent intent = new Intent();
assertNull(intent.getAction());
}
}
""")
GradleRunner runner = runGradle()

when:
BuildResult result = runner
.withArguments("junitPlatformTestDebug")
.build()

then:
result.task(":junitPlatformTestDebug").outcome == TaskOutcome.SUCCESS
result.output.contains("1 tests successful")
result.output.contains("AndroidTest")
}

def "Includes Android resources successfully"() {
given:
androidPlugin()
junit5Plugin("""
unitTests {
includeAndroidResources = true
}
""")
test(language: FileLanguage.Java,
content: """
package de.mannodermaus.app;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import java.io.InputStream;

class AndroidTest {
@Test
void test() {
InputStream is = getClass().getResourceAsStream("/com/android/tools/test_config.properties");
assertNotNull(is);
}
}
""")
GradleRunner runner = runGradle()

when:
BuildResult result = runner
.withArguments("junitPlatformTestDebug")
.build()

then:
result.task(":junitPlatformTestDebug").outcome == TaskOutcome.SUCCESS
result.output.contains("1 tests successful")
result.output.contains("AndroidTest")
}

/*
* ===============================================================================================
* Helpers, Factories & Utilities
Expand Down Expand Up @@ -357,13 +433,14 @@ class FunctionalSpec extends Specification {
"""
}

protected final def junit5Plugin() {
protected final def junit5Plugin(String extraConfig = "") {
buildFile << """
apply plugin: "de.mannodermaus.android-junit5"

android.testOptions {
junitPlatform {
details "flat"
$extraConfig
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,56 @@ class PluginSpec : Spek({
}
}

context("unitTests.returnDefaultValues") {
val project by memoized { testProjectBuilder.build() }

listOf(true, false).forEach { state ->
on("set to $state") {
project.android.testOptions.junitPlatform.unitTests {
returnDefaultValues = state
}

project.evaluate()

it("configures the AGP setting correctly") {
assertThat(project.android.testOptions.unitTests.isReturnDefaultValues)
.isEqualTo(state)
}

it("generates a mockable android.jar with the correct suffix") {
val variant = ProjectConfig(project).unitTestVariants.first()
val file = variant.variantData.scope.globalScope.mockableAndroidJarFile
val assertion = assertThat(file.name)

if (state) {
assertion.contains("default-values")
} else {
assertion.doesNotContain("default-values")
}
}
}
}
}

context("unitTests.includeAndroidResources") {
val project by memoized { testProjectBuilder.build() }

listOf(true, false).forEach { state ->
on("set to $state") {
project.android.testOptions.junitPlatform.unitTests {
includeAndroidResources = state
}

project.evaluate()

it("configures the AGP setting correctly") {
assertThat(project.android.testOptions.unitTests.isIncludeAndroidResources)
.isEqualTo(state)
}
}
}
}

context("jacoco integration") {
beforeEachTest { testProjectBuilder.applyJacocoPlugin() }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* The version of JUnit Platform to use
*/
var platformVersion: String? = null

fun platformVersion(version: String?) {
this.platformVersion = version
}
Expand All @@ -51,6 +52,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* The version of JUnit Jupiter to use
*/
var jupiterVersion: String? = null

fun jupiterVersion(version: String?) {
this.jupiterVersion = version
}
Expand All @@ -59,6 +61,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* The version of JUnit Vintage to use
*/
var vintageVersion: String? = null

fun vintageVersion(version: String?) {
this.vintageVersion = version
}
Expand All @@ -67,6 +70,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* The directory for the test report files
*/
var reportsDir: File? = null

fun reportsDir(reportsDir: File?) {
// Work around for https://discuss.gradle.org/t/bug-in-project-file-on-windows/19917
if (reportsDir is File) {
Expand All @@ -82,6 +86,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* system property to this value
*/
var logManager: String? = null

fun logManager(logManager: String?) {
this.logManager = logManager
}
Expand All @@ -90,6 +95,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* Whether or not the standard Gradle {@code test} task should be enabled
*/
var enableStandardTestTask = false

fun enableStandardTestTask(state: Boolean) {
this.enableStandardTestTask = state
}
Expand All @@ -98,6 +104,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
* Select test execution plan details mode
*/
var details = Details.NONE

fun details(details: Details) {
this.details = details
}
Expand Down Expand Up @@ -157,7 +164,7 @@ open class AndroidJUnitPlatformExtension(private val project: Project) {
*
* @since 1.0.23
*/
val unitTests = UnitTestOptions()
val unitTests = UnitTestOptions(project)

/**
* Configures unit test options
Expand Down Expand Up @@ -462,7 +469,7 @@ open class IncludeExcludeContainer {
/**
* Options for controlling how JUnit 5 Unit Tests should be executed
*/
class UnitTestOptions {
class UnitTestOptions(private val project: Project) {

operator fun invoke(config: UnitTestOptions.() -> Unit) {
this.config()
Expand All @@ -482,10 +489,58 @@ class UnitTestOptions {
* - environment variables
*/
var applyDefaultTestOptions = true

fun applyDefaultTestOptions(state: Boolean) {
this.applyDefaultTestOptions = state
}

/**
* Whether unmocked methods from android.jar should throw exceptions or return default
* values (i.e. zero or null).
*
* Defaults to false, which will throw exceptions on unmocked method invocations
*
* @since 1.0.32
*/
var returnDefaultValues: Boolean
get() = project.android.testOptions.unitTests.isReturnDefaultValues
set(value) {
project.android.testOptions.unitTests.isReturnDefaultValues = value
}

/**
* Enables unit tests to use Android resources, assets, and manifests.
* <p>
* If you enable this setting, the Android Gradle Plugin performs resource, asset,
* and manifest merging before running your unit tests. Your tests can then inspect a file
* called {@code com/android/tools/test_config.properties} on the classpath, which is a Java
* properties file with the following keys:
*
* <ul>
* <li><code>android_sdk_home</code>: the absolute path to the Android SDK.
* <li><code>android_merged_resources</code>: the absolute path to the merged resources
* directory, which contains all the resources from this subproject and all its
* dependencies.
* <li><code>android_merged_assets</code>: the absolute path to the merged assets
* directory. For app subprojects, the merged assets directory contains assets from
* this subproject and its dependencies. For library subprojects, the merged assets
* directory contains only the assets from this subproject.
* <li><code>android_merged_manifest</code>: the absolute path to the merged manifest
* file. Only app subprojects merge manifests of its dependencies. So, library
* subprojects won't include manifest components from their dependencies.
* <li><code>android_custom_package</code>: the package name of the final R class. If you
* modify the application ID in your build scripts, this package name may not match
* the <code>package</code> attribute in the final app manifest.
* </ul>
*
* @since 1.0.32
*/
var includeAndroidResources: Boolean
get() = project.android.testOptions.unitTests.isIncludeAndroidResources
set(value) {
project.android.testOptions.unitTests.isIncludeAndroidResources = value
}

/**
* Applies the provided config closure to all JUnit 5 test tasks,
* and any task that'll be added in the future
Expand Down Expand Up @@ -535,6 +590,7 @@ class InstrumentationTestOptions {
* Whether or not to enable support for JUnit 5 instrumentation tests
*/
var enabled: Boolean = true

fun enabled(state: Boolean) {
this.enabled = state
}
Expand All @@ -543,6 +599,7 @@ class InstrumentationTestOptions {
* The version of the instrumentation companion library to use
*/
var version: String? = null

fun version(version: String?) {
this.version = version
}
Expand All @@ -562,6 +619,7 @@ class JacocoOptions {
* Whether or not to enable Jacoco task integration
*/
var taskGenerationEnabled = true

fun taskGenerationEnabled(state: Boolean) {
this.taskGenerationEnabled = state
}
Expand Down Expand Up @@ -620,13 +678,15 @@ class JacocoOptions {
* By default, this will exclude R.class & BuildConfig.class
*/
var excludedClasses = mutableListOf("**/R.class", "**/R$*.class", "**/BuildConfig.*")

fun excludedClasses(vararg classes: String) = excludedClasses.addAll(classes)

/**
* List of source name patterns that should be excluded from being processed by Jacoco.
* By default, this is an empty list
*/
var excludedSources = mutableListOf<String>()

fun excludedSources(vararg sources: String) = excludedSources.addAll(sources)

class Report {
Expand All @@ -639,6 +699,7 @@ class JacocoOptions {
* Whether or not this report should be generated
*/
var enabled: Boolean = true

fun enabled(state: Boolean) {
this.enabled = state
}
Expand All @@ -649,6 +710,7 @@ class JacocoOptions {
* each variant will be assigned a distinct folder if necessary
*/
var destination: File? = null

fun destination(file: File?) {
this.destination = file
}
Expand Down
Loading