Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
6 changes: 4 additions & 2 deletions recipes/testFixtures/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 [UserRepoFixture](lib/src/testFixtures/java/UserRepoFixture.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:
Expand Down
13 changes: 12 additions & 1 deletion recipes/testFixtures/app/src/test/kotlin/ViewModelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ 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)

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])
}
}
3 changes: 3 additions & 0 deletions recipes/testFixtures/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment thread
emartynov marked this conversation as resolved.
Outdated
# Can't find official documentation https://emartynov.medium.com/android-project-test-fixtures-dec50c5d8533

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://issuetracker.google.com/issues/259523353#comment21 might worth a mention? (The thread has a lot of info)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to mention compatibility with tooling versions?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that's the most official doc we have.

Comment thread
emartynov marked this conversation as resolved.
Outdated
android.experimental.enableTestFixturesKotlinSupport=true
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
import java.util.HashMap;
import java.util.ArrayList;

public class UserRepoFixture {
public class JavaUserRepoFixture {

private Map<Integer, User> 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"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Int, User> =
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<Int> =
ArrayList()

override fun getUsers(): List<User> {
return ArrayList(users.values)
}

override fun updateUser(user: User) {
users[user.id] = user
events.add(AppR.string.userUpdated)
}

override fun getUserEvents(): List<Int> {
return ArrayList(events)
}
}

fun getUsers(): List<User> {
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)
}
}