Skip to content

Commit

Permalink
test: Add integration tests (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
warnyul authored Mar 14, 2024
1 parent d231a14 commit 6274387
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 31 deletions.
12 changes: 12 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ allprojects {
}

dependencies {
kover(project(':integration-tests:agp-groovy-dsl'))
kover(project(':integration-tests:agp-kotlin-dsl'))
kover(project(':robolectric-extension'))
}
13 changes: 6 additions & 7 deletions buildSrc/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id 'java-library'
id 'groovy'
id('groovy')
}

repositories {
Expand All @@ -10,10 +9,10 @@ repositories {
}

dependencies {
compileOnly gradleApi()
compileOnly localGroovy()
implementation libs.guava
implementation libs.androidBuildTools
compileOnly(gradleApi())
compileOnly(localGroovy())
implementation(libs.guava)
implementation(libs.androidGradle)
}

java {
Expand All @@ -24,7 +23,7 @@ java {

task downloadAarDepsPlugin {
final from = "https://raw.githubusercontent.com/robolectric/robolectric/robolectric-${libs.versions.robolectric.get()}/buildSrc/src/main/groovy/org/robolectric/gradle/AarDepsPlugin.java"
final groovySourceSet = new File(sourceSets.findByName('main').allSource.sourceDirectories.find { it.name == 'groovy' }.path)
final groovySourceSet = sourceSets.findByName('main').allSource.sourceDirectories.find { it.name == 'groovy' } as File
final to = new File(groovySourceSet, '/org/robolectric/gradle/AarDepsPlugin.java')

inputs.property("from", from)
Expand Down
17 changes: 6 additions & 11 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[versions]
androidBuildTools = "8.3.0"
androidJUnit5 = "1.10.0.0"
androidBuildTools = "34.0.0"
androidCompileSdk = "34"
androidGradle = "8.3.0"
androidMinimumSdk = "14"
androidxTestExtJunit = "1.1.5"
detekt = "1.23.5"
guava = "33.1.0-jre"
Expand All @@ -15,7 +17,7 @@ robolectricAndroidAll = "14-robolectric-10818077"
sources = "sources"

[libraries]
androidBuildTools = { module = "com.android.tools.build:gradle", version.ref = "androidBuildTools" }
androidGradle = { module = "com.android.tools.build:gradle", version.ref = "androidGradle" }
androidxTestExtJunit = { module = "androidx.test.ext:junit", version.ref = "androidxTestExtJunit" }
detektFormatting = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt" }
detektRulesLibraries = { module = "io.gitlab.arturbosch.detekt:detekt-rules-libraries", version.ref = "detekt" }
Expand All @@ -32,15 +34,8 @@ robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectr
robolectricAndroidAll = { module = "org.robolectric:android-all", version.ref = "robolectricAndroidAll" }

[plugins]
androidJUnit5 = { id = "de.mannodermaus.android-junit5", version.ref = "androidJUnit5" }
androidLibrary = { id = "com.android.library", version.ref = "androidBuildTools" }
androidLibrary = { id = "com.android.library", version.ref = "androidGradle" }
detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlinxKover = { id = "org.jetbrains.kotlinx.kover", version.ref = "kover" }

[bundles]
junit5 = [
"junit5JupiterApi",
"junit5PlatformLauncher",
]
45 changes: 45 additions & 0 deletions integration-tests/agp-groovy-dsl/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
id('com.android.library')
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.kotlinxKover)
alias(libs.plugins.detekt)
}

android {
namespace = 'tech.apter.junit.jupiter.robolectric.integration.tests.agp.groovy.dsl'
compileSdk = libs.versions.androidCompileSdk.get().toInteger()
buildToolsVersion = libs.versions.androidBuildTools.get()

defaultConfig {
minSdk = libs.versions.androidMinimumSdk.get().toInteger()
}

testOptions {
unitTests {
includeAndroidResources = true
all {
useJUnitPlatform()
jvmArgs('-Djunit.platform.launcher.interceptors.enabled=true')
}
}
}
}

detekt {
version = libs.versions.detekt.get()
autoCorrect = true
config.setFrom(rootProject.layout.projectDirectory.file('config/detekt/detekt.yml').asFile)
}

kotlin {
jvmToolchain(libs.versions.jvmToolchain.get().toInteger())
}

dependencies {
detektPlugins(libs.detektFormatting)
detektPlugins(libs.detektRulesLibraries)
testImplementation(project(':robolectric-extension'))
testImplementation(libs.robolectric)
testImplementation(libs.junit5JupiterApi)
testRuntimeOnly(libs.junit5JupiterEngine)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tech.apter.junit.jupiter.robolectric.integration.tests.agp.groovy.dsl

import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.robolectric.RuntimeEnvironment
import tech.apter.junit.jupiter.robolectric.RobolectricExtension

@ExtendWith(RobolectricExtension::class)
class AndroidGradlePluginGroovyDslTest {

@Test
fun `Given an android project with kotlin-dsl build script when using Robolectric with JUnit5 the android app should be available`() {
assertNotNull(RuntimeEnvironment.getApplication())
}
}
45 changes: 45 additions & 0 deletions integration-tests/agp-kotlin-dsl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
plugins {
id("com.android.library")
alias(libs.plugins.kotlinAndroid)
alias(libs.plugins.kotlinxKover)
alias(libs.plugins.detekt)
}

android {
namespace = "tech.apter.junit.jupiter.robolectric.integration.tests.agp.kotlin.dsl"
compileSdk = libs.versions.androidCompileSdk.get().toInt()
buildToolsVersion = libs.versions.androidBuildTools.get()

defaultConfig {
minSdk = libs.versions.androidMinimumSdk.get().toInt()
}

testOptions {
unitTests {
isIncludeAndroidResources = true
all { test ->
test.useJUnitPlatform()
test.jvmArgs(listOf("-Djunit.platform.launcher.interceptors.enabled=true"))
}
}
}
}

detekt {
version = libs.versions.detekt.get()
autoCorrect = true
config.setFrom(rootProject.layout.projectDirectory.file("config/detekt/detekt.yml").asFile)
}

kotlin {
jvmToolchain(libs.versions.jvmToolchain.get().toInt())
}

dependencies {
detektPlugins(libs.detektFormatting)
detektPlugins(libs.detektRulesLibraries)
testImplementation(project(":robolectric-extension"))
testImplementation(libs.robolectric)
testImplementation(libs.junit5JupiterApi)
testRuntimeOnly(libs.junit5JupiterEngine)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tech.apter.junit.jupiter.robolectric.integration.tests.agp.kotlin.dsl

import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.robolectric.RuntimeEnvironment
import tech.apter.junit.jupiter.robolectric.RobolectricExtension

@ExtendWith(RobolectricExtension::class)
class AndroidGradlePluginKotlinDslTest {

@Test
fun `Given an android project with kotlin-dsl build script when using Robolectric with JUnit5 the android app should be available`() {
assertNotNull(RuntimeEnvironment.getApplication())
}
}
61 changes: 50 additions & 11 deletions robolectric-extension/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kotlin {

test {
useJUnitPlatform()
jvmArgs '-Djunit.platform.launcher.interceptors.enabled=true'
jvmArgs('-Djunit.platform.launcher.interceptors.enabled=true')
}

detekt {
Expand All @@ -30,18 +30,57 @@ detekt {
dependencies {
detektPlugins(libs.detektFormatting)
detektPlugins(libs.detektRulesLibraries)
implementation libs.robolectric
implementation libs.junit4
implementation platform(libs.junit5Bom)
implementation libs.bundles.junit5
api(libs.robolectric)
implementation(libs.junit4)
implementation(platform(libs.junit5Bom))
api(libs.junit5JupiterApi)
implementation(libs.junit5PlatformLauncher)
implementation(libs.guavaConstraint) {
because 'CVE-2023-2976 7.1 Transitive Files or Directories Accessible to External Parties vulnerability ' +
'with High severity found'
}
testImplementation libs.robolectricAndroidAll
testImplementation libs.junit4
testImplementation libs.androidxTestExtJunit
testImplementation libs.kotlinTestJUnit5
testImplementation libs.junit5JupiterParams
testRuntimeOnly libs.junit5JupiterEngine
testImplementation(libs.robolectricAndroidAll)
testImplementation(libs.junit4)
testImplementation(libs.androidxTestExtJunit)
testImplementation(libs.kotlinTestJUnit5)
testImplementation(libs.junit5JupiterParams)
testRuntimeOnly(libs.junit5JupiterEngine)
}

private void bashExecute(String command) {
final process = command.execute()
final error = process.errorReader().readLines().join("\n")
if (!error.isBlank()) {
logger.error("error during execution of:\ncommand:$command\ndetails:\n$error")
}
}

tasks.register('generateAndroidR') {
final androidHome = System.getenv("ANDROID_HOME")
final aapt = "$androidHome/build-tools/${libs.versions.androidBuildTools.get()}/aapt"
final testSourceSet = sourceSets.named('test').get()
final testJavaSourceDir = testSourceSet.allSource.sourceDirectories.find { it.name == 'java' } as File
final testResourcesDir = testSourceSet.allSource.sourceDirectories.find { it.name == 'resources' } as File
final androidResDir = new File(testResourcesDir, 'res')
final androidAssetsDir = new File(testResourcesDir, 'assets')
final outputResourcesFile = new File(testResourcesDir, 'resources.ap_')
final androidJarFile = "$androidHome/platforms/android-${libs.versions.androidCompileSdk.get()}/android.jar"
final androidManifestFile = new File(testResourcesDir.parentFile, 'AndroidManifest.xml')

inputs.dir(androidResDir)
inputs.dir(androidAssetsDir)
inputs.file(androidManifestFile)

outputs.file(outputResourcesFile)
outputs.dir(testJavaSourceDir)
doLast {
final aaptCommand = "$aapt p -v -f -m --auto-add-overlay -I $androidJarFile " +
"-S $androidResDir " +
"-M $androidManifestFile.path " +
"-A $androidAssetsDir.path " +
"-F $outputResourcesFile.path " +
"-J $testJavaSourceDir.path " +
"--no-version-vectors"
bashExecute(aaptCommand)
}
}
4 changes: 3 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,7 @@ dependencyResolutionManagement {
}

rootProject.name = 'junit5-robolectric-extension'
include("robolectric-extension")
include('integration-tests:agp-groovy-dsl')
include('integration-tests:agp-kotlin-dsl')
include('robolectric-extension')

0 comments on commit 6274387

Please sign in to comment.