From 671e74cb0e43311e0619d9e2e024ea4a9314c99e Mon Sep 17 00:00:00 2001 From: Eugen Martynov Date: Sat, 1 Mar 2025 12:32:08 +0100 Subject: [PATCH 1/4] Enable and introduce kotlin test fixtures --- .../app/src/test/kotlin/ViewModelTest.kt | 11 +++++ recipes/testFixtures/gradle.properties | 3 ++ .../java/KotlinUserRepoFixture.kt | 45 +++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 recipes/testFixtures/lib/src/testFixtures/java/KotlinUserRepoFixture.kt diff --git a/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt b/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt index b6370fdd..2577f503 100644 --- a/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt +++ b/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt @@ -31,4 +31,15 @@ class ViewModelTest{ fixture.inDataSet(user) fixture.assertEventIsUpdateUser(model.getEvents()[0]) } + + @Test + fun updateUserKotlinLogic(){ + val fixture = KotlinUserRepoFixture() + val model = ViewModel(fixture.repository) + val user = model.users[0].copy(status = "sick") + model.updateStatus(user.id, user.status) + + fixture.inDataSet(user) + fixture.assertEventIsUpdateUser(model.getEvents()[0]) + } } diff --git a/recipes/testFixtures/gradle.properties b/recipes/testFixtures/gradle.properties index 55cce922..a15e0e52 100644 --- a/recipes/testFixtures/gradle.properties +++ b/recipes/testFixtures/gradle.properties @@ -21,3 +21,6 @@ kotlin.code.style=official # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true +# Enbale test kotlin fixtures +# Can't find official documentation https://emartynov.medium.com/android-project-test-fixtures-dec50c5d8533 +android.experimental.enableTestFixturesKotlinSupport=true diff --git a/recipes/testFixtures/lib/src/testFixtures/java/KotlinUserRepoFixture.kt b/recipes/testFixtures/lib/src/testFixtures/java/KotlinUserRepoFixture.kt new file mode 100644 index 00000000..18457add --- /dev/null +++ b/recipes/testFixtures/lib/src/testFixtures/java/KotlinUserRepoFixture.kt @@ -0,0 +1,45 @@ +import com.google.common.truth.Truth + +import com.example.android.recipes.fixtureLib.R as AppR + +class KotlinUserRepoFixture { + private val users: MutableMap = + HashMap() + + init { + users[1] = User(1, "Bob", "Wilson", "active") + users[2] = User(2, "John", "Johnson", "vacation") + } + + val repository: UserRepository + get() = object : UserRepository { + private val events: MutableList = + ArrayList() + + override fun getUsers(): List { + return ArrayList(users.values) + } + + override fun updateUser(user: User) { + users[user.id] = user + events.add(AppR.string.userUpdated) + } + + override fun getUserEvents(): List { + return ArrayList(events) + } + } + + fun getUsers(): List { + return ArrayList(users.values) + } + + fun inDataSet(user: User) { + Truth.assertThat(users[user.id]).isEqualTo(user) + } + + fun assertEventIsUpdateUser(event: Int) { + Truth.assertThat(event) + .isEqualTo(AppR.string.userUpdated) + } +} From 383b3be27f81a67bae286fdb315197c27ad0e663 Mon Sep 17 00:00:00 2001 From: Eugen Martynov Date: Sat, 1 Mar 2025 12:40:23 +0100 Subject: [PATCH 2/4] Add Java prefix to test fixture in java --- recipes/testFixtures/README.md | 2 +- recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt | 2 +- .../java/{UserRepoFixture.java => JavaUserRepoFixture.java} | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename recipes/testFixtures/lib/src/testFixtures/java/{UserRepoFixture.java => JavaUserRepoFixture.java} (96%) diff --git a/recipes/testFixtures/README.md b/recipes/testFixtures/README.md index 49ece8cc..6ccd1b24 100644 --- a/recipes/testFixtures/README.md +++ b/recipes/testFixtures/README.md @@ -26,7 +26,7 @@ Test dependency must be declared in Gradle script to target the fixture artifact You can check details of logic we test in [ViewModel](app/src/main/kotlin/ViewModel.kt), test itself in [ViewModelTest](app/src/test/kotlin/ViewModelTest.kt) and -fixture [UserRepoFixture](lib/src/testFixtures/java/UserRepoFixture.java). +fixture [JavaUserRepoFixture](lib/src/testFixtures/java/JavaUserRepoFixture.java). ## To Run To execute example and run tests you need to enter command: diff --git a/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt b/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt index 2577f503..84bfaef3 100644 --- a/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt +++ b/recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt @@ -23,7 +23,7 @@ class ViewModelTest{ @Test fun updateUserLogic(){ - val fixture = UserRepoFixture() + val fixture = JavaUserRepoFixture() val model = ViewModel(fixture.getRepository()) val user = model.users[0].copy(status = "sick") model.updateStatus(user.id, user.status) diff --git a/recipes/testFixtures/lib/src/testFixtures/java/UserRepoFixture.java b/recipes/testFixtures/lib/src/testFixtures/java/JavaUserRepoFixture.java similarity index 96% rename from recipes/testFixtures/lib/src/testFixtures/java/UserRepoFixture.java rename to recipes/testFixtures/lib/src/testFixtures/java/JavaUserRepoFixture.java index 2b678e2b..f219aaca 100644 --- a/recipes/testFixtures/lib/src/testFixtures/java/UserRepoFixture.java +++ b/recipes/testFixtures/lib/src/testFixtures/java/JavaUserRepoFixture.java @@ -20,11 +20,11 @@ import java.util.HashMap; import java.util.ArrayList; -public class UserRepoFixture { +public class JavaUserRepoFixture { private Map users; - public UserRepoFixture() { + public JavaUserRepoFixture() { users = new HashMap<>(); users.put(1, new User(1, "Bob", "Wilson", "active")); users.put(2, new User(2, "John", "Johnson", "vacation")); From 61b3d6c5c623407bbd6df20bda87bb2b55e7a73e Mon Sep 17 00:00:00 2001 From: Eugen Martynov Date: Sat, 1 Mar 2025 12:43:08 +0100 Subject: [PATCH 3/4] Update documentation --- recipes/testFixtures/README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/recipes/testFixtures/README.md b/recipes/testFixtures/README.md index 6ccd1b24..99e783be 100644 --- a/recipes/testFixtures/README.md +++ b/recipes/testFixtures/README.md @@ -4,7 +4,8 @@ This recipe shows how to use test fixtures with Android projects. --- -**Be Aware:** we don't support writing test-fixtures in Kotlin for AGP versions up to 8.4, only Java +**Be Aware:** we don't support writing test-fixtures in Kotlin for AGP versions up to 8.4, only Java. +Kotlin test fixtures are supported from AGP 8.5 and later. --- Recipe has the following module structure: @@ -26,7 +27,8 @@ Test dependency must be declared in Gradle script to target the fixture artifact You can check details of logic we test in [ViewModel](app/src/main/kotlin/ViewModel.kt), test itself in [ViewModelTest](app/src/test/kotlin/ViewModelTest.kt) and -fixture [JavaUserRepoFixture](lib/src/testFixtures/java/JavaUserRepoFixture.java). +fixture using Java [JavaUserRepoFixture](lib/src/testFixtures/java/JavaUserRepoFixture.java) or +fixture using Kotlin [KotlinUserRepoFixture](lib/src/testFixtures/java/KotlinUserRepoFixture.kt). ## To Run To execute example and run tests you need to enter command: From 486b0dcf617d5748b0cffcbabffbcdae559a4a6d Mon Sep 17 00:00:00 2001 From: Eugen Martynov Date: Mon, 3 Mar 2025 20:37:16 +0100 Subject: [PATCH 4/4] Fix spelling and remove private blog link (code review comments) --- recipes/testFixtures/gradle.properties | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/recipes/testFixtures/gradle.properties b/recipes/testFixtures/gradle.properties index a15e0e52..27197a81 100644 --- a/recipes/testFixtures/gradle.properties +++ b/recipes/testFixtures/gradle.properties @@ -21,6 +21,5 @@ kotlin.code.style=official # resources declared in the library itself and none from the library's dependencies, # thereby reducing the size of the R class for that library android.nonTransitiveRClass=true -# Enbale test kotlin fixtures -# Can't find official documentation https://emartynov.medium.com/android-project-test-fixtures-dec50c5d8533 +# Enable test kotlin fixtures android.experimental.enableTestFixturesKotlinSupport=true