20. Mockito導入(2)
@Test
fun testWithMock() {
val dependee: Dependee = mock {
// Mockito-Kotlin は mock {} でモックの設定ができる
on { doSomething() }.then { Result(1) }
}
val depending = Depending(dependee)
val actual = depending.doAnotherThingWithSomething()
assertEquals(Result(1), actual)
}
21. Instrumented Unit Test
● Googleは結合テストの段階ではAndroidの上でテストすることを推奨している
● Test Support Libraryを導入して
RunWithアノテーションを書いて
androidTestディレクトリ以下にテストコードを置けば良いだけ
● 普通のUnitテストとほぼ何も変わらない
22. Instrumented Unit Testの例
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun testSaveSharedPreferences() {
val sp = InstrumentationRegistry
.getTargetContext()
.getSharedPreferences("hoge", Context.MODE_PRIVATE)
sp
.edit()
.putInt("hoge", 1)
.apply()
assertEquals(sp.getInt("hoge", 0), 1)
}
}