Skip to content

Enable and introduce kotlin test fixtures #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: studio-main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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])
}
}
2 changes: 2 additions & 0 deletions recipes/testFixtures/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +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
# Enable test kotlin fixtures
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)
}
}