Skip to content

Commit 13050d9

Browse files
author
Marcel Schnelle
authored
Finish dependency revisions for new instrumentation libs (#160)
* Brush up androidTest dependencies for core & runner libs * Add some instrumentation tests to the sample project
1 parent 4f1095c commit 13050d9

File tree

7 files changed

+118
-8
lines changed

7 files changed

+118
-8
lines changed

buildSrc/src/main/kotlin/Artifacts.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ object Artifacts {
7272
val Runner = Deployed(
7373
platform = Android(minSdk = 14),
7474
groupId = groupId,
75-
artifactId = "android-instrumentation-test-runner",
75+
artifactId = "android-test-runner",
7676
currentVersion = currentVersion,
7777
latestStableVersion = latestStableVersion,
7878
license = license,

buildSrc/src/main/kotlin/Versions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ object Versions {
1515

1616
const val aapt2: String = "3.2.1-4818971"
1717

18-
const val com_android_tools_build_gradle: String = "3.4.0-rc02"
18+
const val com_android_tools_build_gradle: String = "3.4.0-rc03"
1919
const val com_android_tools_build_gradle_32x: String = "3.2.1"
2020
const val com_android_tools_build_gradle_33x: String = "3.3.2"
21-
const val com_android_tools_build_gradle_34x: String = "3.4.0-rc02"
21+
const val com_android_tools_build_gradle_34x: String = "3.4.0-rc03"
2222
const val com_android_tools_build_gradle_35x: String = "3.5.0-alpha07"
2323

2424
const val lint_gradle: String = "26.2.1"

instrumentation/core/build.gradle.kts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,15 @@ tasks.withType<Test> {
8181
}
8282

8383
dependencies {
84-
implementation(Libs.junit_jupiter_api)
8584
implementation(Libs.kotlin_stdlib)
86-
implementation(Libs.androidx_test_core)
85+
implementation(Libs.junit_jupiter_api)
86+
api(Libs.androidx_test_core)
8787

8888
// This is required by the "instrumentation-runner" companion library,
8989
// since it can't provide any JUnit 5 runtime libraries itself
9090
// due to fear of prematurely incrementing the minSdkVersion requirement.
9191
runtimeOnly(Libs.junit_platform_runner)
92+
runtimeOnly(Libs.junit_jupiter_engine)
9293

9394
androidTestImplementation(Libs.junit_jupiter_api)
9495
androidTestImplementation(Libs.espresso_core)

instrumentation/sample/build.gradle.kts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import de.mannodermaus.gradle.plugins.junit5.junitPlatform
22
import org.gradle.api.tasks.testing.logging.TestLogEvent
3+
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
34

45
buildscript {
56
repositories {
@@ -65,6 +66,12 @@ android {
6566
}
6667
}
6768

69+
tasks.withType<KotlinCompile> {
70+
kotlinOptions {
71+
jvmTarget = JavaVersion.VERSION_1_8.toString()
72+
}
73+
}
74+
6875
tasks.withType<Test> {
6976
testLogging.events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
7077
}
@@ -81,8 +88,8 @@ dependencies {
8188

8289
// Android Instrumentation Tests wth JUnit 5
8390
androidTestImplementation(Libs.junit_jupiter_api)
84-
androidTestRuntimeOnly(Libs.junit_jupiter_engine)
85-
androidTestRuntimeOnly(Libs.junit_platform_runner)
86-
androidTestImplementation(project(":api"))
91+
androidTestImplementation(Libs.junit_jupiter_params)
92+
androidTestImplementation(Libs.espresso_core)
93+
androidTestImplementation(project(":core"))
8794
androidTestRuntimeOnly(project(":runner"))
8895
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package de.mannodermaus.sample
2+
3+
import androidx.test.core.app.ActivityScenario
4+
import androidx.test.espresso.Espresso.onView
5+
import androidx.test.espresso.action.ViewActions.click
6+
import androidx.test.espresso.assertion.ViewAssertions.matches
7+
import androidx.test.espresso.matcher.ViewMatchers.withId
8+
import androidx.test.espresso.matcher.ViewMatchers.withText
9+
import de.mannodermaus.junit5.ActivityScenarioExtension
10+
import de.mannodermaus.junit5.sample.ActivityOne
11+
import de.mannodermaus.junit5.sample.R
12+
import org.junit.jupiter.api.Assertions.assertEquals
13+
import org.junit.jupiter.api.RepeatedTest
14+
import org.junit.jupiter.api.RepetitionInfo
15+
import org.junit.jupiter.api.Test
16+
import org.junit.jupiter.api.extension.RegisterExtension
17+
import org.junit.jupiter.params.ParameterizedTest
18+
import org.junit.jupiter.params.provider.ValueSource
19+
20+
class ActivityOneTest {
21+
22+
@JvmField
23+
@RegisterExtension
24+
val scenarioExtension = ActivityScenarioExtension.launch<ActivityOne>()
25+
26+
@Test
27+
fun testExample(scenario: ActivityScenario<ActivityOne>) {
28+
onView(withId(R.id.textView)).check(matches(withText("0")))
29+
}
30+
31+
@ValueSource(strings = ["value1", "value2"])
32+
@ParameterizedTest
33+
fun parameterizedTestExample(value: String, scenario: ActivityScenario<ActivityOne>) {
34+
scenario.onActivity {
35+
assertEquals(0, it.getClickCount())
36+
it.setButtonLabel(value)
37+
}
38+
39+
onView(withId(R.id.button)).check(matches(withText(value)))
40+
onView(withId(R.id.button)).perform(click())
41+
42+
scenario.onActivity {
43+
assertEquals(1, it.getClickCount())
44+
}
45+
}
46+
47+
@RepeatedTest(3)
48+
fun repeatedTestExample(repetitionInfo: RepetitionInfo, scenario: ActivityScenario<ActivityOne>) {
49+
val count = repetitionInfo.currentRepetition
50+
51+
for (i in 0 until count) {
52+
onView(withId(R.id.button)).perform(click())
53+
}
54+
55+
scenario.onActivity {
56+
assertEquals(count, it.getClickCount())
57+
}
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
package de.mannodermaus.junit5.sample
22

33
import android.app.Activity
4+
import android.os.Bundle
5+
import android.widget.Button
6+
import android.widget.TextView
47

58
class ActivityOne : Activity() {
69

10+
private val textView by lazy { findViewById<TextView>(R.id.textView) }
11+
private val button by lazy { findViewById<Button>(R.id.button) }
12+
13+
override fun onCreate(savedInstanceState: Bundle?) {
14+
super.onCreate(savedInstanceState)
15+
setContentView(R.layout.activity_one)
16+
17+
button.setOnClickListener {
18+
val currentClickCount = getClickCount()
19+
val newClickCount = currentClickCount + 1
20+
textView.text = newClickCount.toString()
21+
}
22+
}
23+
24+
fun getClickCount(): Int {
25+
return textView.text.toString().toInt()
26+
}
27+
28+
fun setButtonLabel(newText: String) {
29+
button.text = newText
30+
}
731
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:orientation="vertical"
4+
android:gravity="center"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent">
7+
8+
<TextView
9+
android:id="@+id/textView"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:text="0"/>
13+
14+
<Button
15+
android:id="@+id/button"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:text="Click"/>
19+
</LinearLayout>

0 commit comments

Comments
 (0)