Skip to content

Commit 07015f5

Browse files
authored
Released 1.0.0-M4-rev1
* Some advancements towards AGP 3.0.0 * Expanded Unit Test suite to run tests for 2.x & 3.x variants of the AGP * Mark test source folders as such in IDEA * Use default JUnit Platform version * Performed some sweeping inside the plugin’s compile & runtime dependencies, and tasks * JUnit 4 is included by default, which allows AS builds w/o “class not found” * Deprecated junitVintage() dependency handler & issue a warning * Both TestEngines are included on the runtime classpath by default * Disable the default unit test task if JUnit 5 is used * Some clean-up * Moved around unit test cases for 2.x & 3.x * Bump to 1.0.0-M4-rev1
1 parent a168d73 commit 07015f5

File tree

14 files changed

+365
-155
lines changed

14 files changed

+365
-155
lines changed

.idea/runConfigurations/Run_Unit_Tests.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ buildscript {
1111
}
1212
dependencies {
1313
// 2. Add the plugin as a classpath dependency
14-
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0-M4"
14+
classpath "de.mannodermaus.gradle.plugins:android-junit5:1.0.0-M4-rev1"
1515
}
1616
}
1717
@@ -22,15 +22,14 @@ apply plugin: "de.mannodermaus.android-junit5"
2222
dependencies {
2323
// 4. Add the testCompile dependencies on JUnit Jupiter
2424
testCompile junitJupiter()
25-
26-
// 5. (Optional) Add the testCompile dependency on the JUnit Vintage Engine
27-
testCompile junitVintage()
2825
}
2926
```
3027

3128
## Usage
3229

33-
This plugin configures the `junitPlatform` task for each registered build variant of a project. Further instructions on how to write JUnit 5 tests can be found [in their User Guide][junit5ug].
30+
This plugin configures the `junitPlatform` task for each registered build variant of a project. Starting with version `1.0.0-M4-rev1`, the plugin automatically attaches both the Jupiter & Vintage Engines.
31+
32+
Further instructions on how to write JUnit 5 tests can be found [in their User Guide][junit5ug].
3433

3534
## Extras
3635

android-junit5/build.gradle

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
11
apply plugin: 'groovy'
2+
apply plugin: 'idea'
23
apply plugin: 'maven'
34
apply plugin: 'maven-publish'
45
apply plugin: 'com.jfrog.bintray'
56

67
sourceCompatibility = JavaVersion.VERSION_1_6
78
targetCompatibility = JavaVersion.VERSION_1_6
89

10+
configurations {
11+
testAgp2xCompile {
12+
extendsFrom configurations.testCompile
13+
}
14+
testAgp3xCompile {
15+
extendsFrom configurations.testCompile
16+
}
17+
}
18+
19+
sourceSets {
20+
testAgp2x {
21+
java.srcDir "src/testAgp2x/groovy"
22+
compileClasspath += sourceSets.test.output
23+
runtimeClasspath += sourceSets.test.output
24+
}
25+
testAgp3x {
26+
java.srcDir "src/testAgp3x/groovy"
27+
compileClasspath += sourceSets.test.output
28+
runtimeClasspath += sourceSets.test.output
29+
}
30+
}
31+
32+
idea {
33+
module {
34+
testSourceDirs += file("src/testAgp2x/groovy")
35+
testSourceDirs += file("src/testAgp3x/groovy")
36+
}
37+
}
38+
939
dependencies {
1040
compile gradleApi()
1141
compile localGroovy()
1242
compile "org.junit.platform:junit-platform-gradle-plugin:$JUNIT_PLATFORM_VERSION"
1343

14-
testCompile "com.android.tools.build:gradle:$ANDROID_PLUGIN_VERSION"
44+
testCompile "junit:junit:$JUNIT4_VERSION"
1545
testCompile("org.spockframework:spock-core:$SPOCK_VERSION") {
1646
transitive = false
1747
}
18-
testCompile "junit:junit:$JUNIT4_VERSION"
48+
49+
testAgp2xCompile "com.android.tools.build:gradle:2.3.2"
50+
testAgp3xCompile "com.android.tools.build:gradle:3.0.0-alpha1"
1951
}
2052

53+
// Run Unit Tests against Android Gradle Plugin version 2.x
54+
task testAgp2x(type: Test) {
55+
testClassesDirs = sourceSets.testAgp2x.output.classesDirs
56+
classpath = sourceSets.main.runtimeClasspath + sourceSets.testAgp2x.runtimeClasspath
57+
}
58+
59+
// Run Unit Tests against Android Gradle Plugin version 3.x
60+
task testAgp3x(type: Test) {
61+
testClassesDirs = sourceSets.testAgp3x.output.classesDirs
62+
classpath = sourceSets.main.runtimeClasspath + sourceSets.testAgp3x.runtimeClasspath
63+
}
64+
65+
// Combine all tests when executing the main JUnit task
66+
tasks.getByName("test").dependsOn(testAgp2x, testAgp3x)
67+
2168
version = VERSION_NAME
2269

2370
bintray {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package de.mannodermaus.gradle.anj5
2+
3+
class AndroidJUnit5Compat {
4+
5+
/**
6+
* Fetches the Java output directories for the given Variant scopes
7+
* across different versions of the Android Gradle plugin.
8+
* @param variantScope VariantScope to look up the Java outputs from
9+
* @return An Iterable container depicting the output directories
10+
*/
11+
static Iterable<File> getJavaOutputDirs(variantScope) {
12+
if (variantScope.hasProperty("javaOutputs")) {
13+
return variantScope.javaOutputs
14+
15+
} else if (variantScope.hasProperty("javaOuptuts")) {
16+
return variantScope.javaOuptuts
17+
18+
} else {
19+
return Collections.singletonList(variantScope.javaOutputDir)
20+
}
21+
}
22+
}

android-junit5/src/main/groovy/de/mannodermaus/gradle/anj5/AndroidJUnit5PlatformExtension.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ import org.junit.platform.gradle.plugin.JUnitPlatformExtension
99
*/
1010
class AndroidJUnit5PlatformExtension extends JUnitPlatformExtension {
1111

12+
private static final String PLATFORM_VERSION = "1.0.0-M4"
13+
1214
AndroidJUnit5PlatformExtension(Project project) {
1315
super(project)
16+
platformVersion = PLATFORM_VERSION
1417
}
1518

1619
/**

android-junit5/src/main/groovy/de/mannodermaus/gradle/anj5/AndroidJUnitPlatformPlugin.groovy

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ import org.junit.platform.gradle.plugin.*
1818
*/
1919
class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
2020

21+
private static final String LOG_TAG = "[android-junit5]"
22+
23+
private static final String VINTAGE_WARNING = "AGPBI: {\"kind\":\"warning\",\"text\":\"$LOG_TAG You don't need to depend on junitVintage() directly anymore!\",\"sources\":[{},{}]}"
24+
2125
private static final String EXTENSION_NAME = 'junitPlatform'
2226
private static final String TASK_NAME = 'junitPlatformTest'
2327

@@ -51,22 +55,34 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
5155
// by this plugin.
5256
def configuration = project.configurations.maybeCreate('junitPlatform')
5357
configuration.defaultDependencies { deps ->
54-
def version = junitExtension.platformVersion
55-
deps.add(project.dependencies.create("org.junit.platform:junit-platform-launcher:${version}"))
56-
deps.add(project.dependencies.create("org.junit.platform:junit-platform-console:${version}"))
58+
// By default, include both TestEngines
59+
// and the Launcher-related dependencies
60+
// on the runtime classpath
61+
def platformVersion = junitExtension.platformVersion
62+
deps.add(project.dependencies.create("org.junit.platform:junit-platform-launcher:${platformVersion}"))
63+
deps.add(project.dependencies.create("org.junit.platform:junit-platform-console:${platformVersion}"))
64+
65+
def jupiterVersion = junitExtension.jupiterVersion
66+
deps.add(project.dependencies.create("org.junit.jupiter:junit-jupiter-engine:${jupiterVersion}"))
67+
68+
def vintageVersion = junitExtension.vintageVersion
69+
deps.add(project.dependencies.create("org.junit.vintage:junit-vintage-engine:${vintageVersion}"))
5770
}
5871

5972
// Add a junitJupiter() dependency handler
6073
project.dependencies.ext.junitJupiter = {
6174
def jupiterVersion = junitExtension.jupiterVersion
62-
project.dependencies.create("org.junit.jupiter:junit-jupiter-api:${jupiterVersion}")
63-
project.dependencies.create("org.junit.jupiter:junit-jupiter-engine:${jupiterVersion}")
75+
76+
return [
77+
project.dependencies.create("junit:junit:4.12"),
78+
project.dependencies.create("org.junit.jupiter:junit-jupiter-api:${jupiterVersion}"),
79+
]
6480
}
6581

6682
// Add a junitVintage() dependency handler
6783
project.dependencies.ext.junitVintage = {
68-
def vintageVersion = junitExtension.vintageVersion
69-
project.dependencies.create("org.junit.vintage:junit-vintage-engine:${vintageVersion}")
84+
project.logger.warn(VINTAGE_WARNING)
85+
return []
7086
}
7187

7288
project.afterEvaluate {
@@ -86,12 +102,12 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
86102
// Obtain variant properties
87103
def variantData = variant.variantData
88104
def variantScope = variantData.scope
89-
def scopeJavaOutputs = variantScope.hasProperty("javaOutputs") ? variantScope.javaOutputs : variantScope.javaOuptuts
105+
def scopeJavaOutputs = AndroidJUnit5Compat.getJavaOutputDirs(variantScope)
90106

91107
// Obtain tested variant properties
92108
def testedVariantData = variant.testedVariant.variantData
93109
def testedVariantScope = testedVariantData.scope
94-
def testedScopeJavaOutputs = testedVariantScope.hasProperty("javaOutputs") ? testedVariantScope.javaOutputs : testedVariantScope.javaOuptuts
110+
def testedScopeJavaOutputs = AndroidJUnit5Compat.getJavaOutputDirs(testedVariantScope)
95111

96112
// Collect the root directories for unit tests from the variant's scopes
97113
def testRootDirs = []
@@ -111,19 +127,24 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
111127
classpath.add(scopeJavaOutputs)
112128
}
113129

114-
// 2) Add the testApk configuration
115-
def testApk = project.configurations.findByName("testApk")
116-
if (testApk != null) {
117-
classpath.add(testApk)
118-
}
130+
// 2) Add the runtime configurations
131+
// def testRuntime = project.configurations.findByName("testRuntimeOnly")
132+
// if (testRuntime == null) {
133+
// testRuntime = project.configurations.findByName("testApk")
134+
// }
135+
// if (testRuntime != null) {
136+
// classpath.add(testRuntime)
137+
// }
119138

120139
// 3) Add test resources
121140
classpath.add(variantData.javaResourcesForUnitTesting)
122141
classpath.add(testedVariantData.javaResourcesForUnitTesting)
123142

124143
// 4) Add filtered boot classpath
125144
def globalScope = variantScope.globalScope
126-
classpath.add(globalScope.androidBuilder.getBootClasspath(false).findAll { it.name != "android.jar" })
145+
classpath.add(globalScope.androidBuilder.getBootClasspath(false).findAll {
146+
it.name != "android.jar"
147+
})
127148

128149
// 5) Add mocked version of android.jar
129150
classpath.add(globalScope.mockableAndroidJarFile)
@@ -152,6 +173,11 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
152173
group: 'verification',
153174
description: 'Runs tests on the JUnit Platform.') { junitTask ->
154175

176+
// Disable the default Unit Test task, since we're running JUnit 5 anyway
177+
def defaultTestTask = project.tasks.findByName("test${nameSuffix}UnitTest")
178+
defaultTestTask.setEnabled(false)
179+
defaultTestTask.dependsOn += junitTask
180+
155181
junitTask.inputs.property('enableStandardTestTask', junitExtension.enableStandardTestTask)
156182
junitTask.inputs.property('selectors.uris', junitExtension.selectors.uris)
157183
junitTask.inputs.property('selectors.files', junitExtension.selectors.files)
@@ -188,25 +214,6 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
188214

189215
junitTask.main = ConsoleLauncher.class.getName()
190216
junitTask.args buildArgs(project, junitExtension, reportsDir, testRootDirs)
191-
192-
doFirst {
193-
project.logger.info("CLASS PATH ==")
194-
classpath.each {
195-
project.logger.info("$it")
196-
}
197-
project.logger.info("=============")
198-
project.logger.info("TASK ARGS ===")
199-
project.logger.info("${junitTask.args.join(" ")}")
200-
project.logger.info("=============")
201-
202-
// def rootDirs = []
203-
// project.sourceSets.each { sourceSet ->
204-
// rootDirs.add(sourceSet.output.classesDir)
205-
// rootDirs.add(sourceSet.output.resourcesDir)
206-
// rootDirs.addAll(sourceSet.output.dirs.files)
207-
// }
208-
// args.addAll(['--scan-class-path', rootDirs.join(File.pathSeparator)])
209-
}
210217
}
211218
}
212219

@@ -245,10 +252,10 @@ class AndroidJUnitPlatformPlugin extends JUnitPlatformPlugin {
245252
args.addAll(['-n', pattern])
246253
}
247254
filters.packages.include.each { includedPackage ->
248-
args.addAll(['--include-package',includedPackage])
255+
args.addAll(['--include-package', includedPackage])
249256
}
250257
filters.packages.exclude.each { excludedPackage ->
251-
args.addAll(['--exclude-package',excludedPackage])
258+
args.addAll(['--exclude-package', excludedPackage])
252259
}
253260
filters.tags.include.each { tag ->
254261
args.addAll(['-t', tag])

0 commit comments

Comments
 (0)