Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今からはじめるrobolectric / unit-test-with-robolectric

moriiimo
January 28, 2019

今からはじめるrobolectric / unit-test-with-robolectric

moriiimo

January 28, 2019
Tweet

More Decks by moriiimo

Other Decks in Technology

Transcript

  1. ࠓ͔Β͸͡ΊΔSPCPMFDUSJD
    !NPSJJJNP

    View full-size slide

  2. "ZBLP.PSJNPUP
    %..DPNMJGFࣄۀ෦
    !NPSJJJNP

    View full-size slide

  3. SPCPMFDUSJD
    ࠓճ͓࿩͢Δͷ͸SPCPMFDUSJD
    ࢼͯ͠ΈͨΑʂͱ͍͏͘Β͍Ͱ͢
    ྃ͝ঝ͍ͩ͘͞

    View full-size slide

  4. (FUUJOH4UBSUFE
    dependencies {
    testImplementation 'org.robolectric:robolectric:4.1'
    }
    android {
    testOptions {
    unitTests {
    includeAndroidResources = true
    }
    }
    }

    View full-size slide

  5. (FUUJOH4UBSUFE
    # Project-wide Gradle settings.
    # IDE (e.g. Android Studio) users:
    # Gradle settings configured through the IDE *will override*
    # any settings specified in this file.
    # For more details on how to configure your build environment visit
    # http://www.gradle.org/docs/current/userguide/build_environment.html
    # Specifies the JVM arguments used for the daemon process.
    # The setting is particularly useful for tweaking memory settings.
    org.gradle.jvmargs=-Xmx1536m
    # When configured, Gradle will run in incubating parallel mode.
    # This option should only be used with decoupled projects. More
    details, visit
    # http://www.gradle.org/docs/current/userguide/
    multi_project_builds.html#sec:decoupled_projects
    # org.gradle.parallel=true
    android.enableUnitTestBinaryResources=true

    View full-size slide

  6. .BJO"DUJWJUZ
    class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    title = "Robolectric onCreate"
    val button = findViewById(R.id.button)
    button.setOnClickListener {
    findViewById(R.id.textView).text = "Hello Robolectric"
    }
    }
    override fun onResume() {
    super.onResume()
    title = "Robolectric onResume"
    }
    }

    View full-size slide

  7. .BJO"DUJWJUZ5FTU
    @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [LOLLIPOP])
    class MainActivityTest {
    @Test
    @Throws(Exception::class)
    fun activityEventIsCorrect() {
    val controller =
    Robolectric.buildActivity(MainActivity::class.java).create().start()
    val activity = controller.get()
    assertTrue(activity.title.toString() == "Robolectric onCreate")
    // onResumeΠϕϯτൃੜ
    controller.resume()
    assertTrue(activity.title.toString() == "Robolectric onResume")
    // onClickΠϕϯτൃੜ
    activity.findViewById(R.id.button).performClick()
    assertTrue(activity.findViewById(R.id.textView).text == "Hello
    Robolectric")
    }
    }

    View full-size slide

  8. .BJO"DUJWJUZ5FTU
    @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [LOLLIPOP])
    class MainActivityTest {
    @Test
    @Throws(Exception::class)
    fun activityEventIsCorrect() {
    val controller =
    Robolectric.buildActivity(MainActivity::class.java).create()
    .start()
    val activity = controller.get()
    assertTrue(activity.title.toString() == "Robolectric
    onCreate")
    // onResumeΠϕϯτൃੜ
    controller.resume()
    assertTrue(activity.title.toString() == "Robolectric
    onResume")
    // onClickΠϕϯτൃੜ
    activity.findViewById(R.id.button).performClick()
    assertTrue(activity.findViewById(R.id.textView).te
    xt == "Hello Robolectric")
    }
    }
    @RunWith(RobolectricTestRunner::class)

    View full-size slide

  9. .BJO"DUJWJUZ5FTU
    @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [LOLLIPOP])
    class MainActivityTest {
    @Test
    @Throws(Exception::class)
    fun activityEventIsCorrect() {
    val controller =
    Robolectric.buildActivity(MainActivity::class.java).create()
    .start()
    val activity = controller.get()
    assertTrue(activity.title.toString() == "Robolectric
    onCreate")
    // onResumeΠϕϯτൃੜ
    controller.resume()
    assertTrue(activity.title.toString() == "Robolectric
    onResume")
    // onClickΠϕϯτൃੜ
    activity.findViewById(R.id.button).performClick()
    assertTrue(activity.findViewById(R.id.textView).te
    xt == "Hello Robolectric")
    }
    }
    @Config(sdk = [LOLLIPOP])

    View full-size slide

  10. .BJO"DUJWJUZ5FTU
    @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [LOLLIPOP])
    class MainActivityTest {
    @Test
    @Throws(Exception::class)
    fun activityEventIsCorrect() {
    val controller =
    Robolectric.buildActivity(MainActivity::class.java).create()
    .start()
    val activity = controller.get()
    assertTrue(activity.title.toString() == "Robolectric
    onCreate")
    // onResumeΠϕϯτൃੜ
    controller.resume()
    assertTrue(activity.title.toString() == "Robolectric
    onResume")
    // onClickΠϕϯτൃੜ
    activity.findViewById(R.id.button).performClick()
    assertTrue(activity.findViewById(R.id.textView).te
    xt == "Hello Robolectric")
    }
    }
    @Test
    @Throws(Exception::class)
    fun activityEventIsCorrect() {
    val controller =
    Robolectric.buildActivity(MainActivit
    y::class.java).create().start()
    val activity = controller.get()
    assertTrue(activity.title.toString()
    == "Robolectric onCreate")

    View full-size slide

  11. .BJO"DUJWJUZ5FTU
    @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [LOLLIPOP])
    class MainActivityTest {
    @Test
    @Throws(Exception::class)
    fun activityEventIsCorrect() {
    val controller =
    Robolectric.buildActivity(MainActivity::class.java).create()
    .start()
    val activity = controller.get()
    assertTrue(activity.title.toString() == "Robolectric
    onCreate")
    // onResumeΠϕϯτൃੜ
    controller.resume()
    assertTrue(activity.title.toString() == "Robolectric
    onResume")
    // onClickΠϕϯτൃੜ
    activity.findViewById(R.id.button).performClick()
    assertTrue(activity.findViewById(R.id.textView).te
    xt == "Hello Robolectric")
    }
    }
    controller.resume()
    assertTrue(activity.title.toString()
    == "Robolectric onResume”)
    activity.findViewById(R.id.button)
    .performClick()
    assertTrue(activity.findViewById
    (R.id.textView).text == "Hello
    Robolectric")
    }
    }

    View full-size slide

  12. ࣮ߦͯ͠ΈΔʂ

    View full-size slide

  13. ʘ͠͹Β͓͘଴ͪԼ͍͞ʗ
    ʜʜʜ

    View full-size slide

  14. Ϣʔεέʔε

    View full-size slide

  15. 3PPN5PEP%BUBCBTF
    @Database(entities = [TodoEntity::class], version = 1)
    abstract class TodoDatabase : RoomDatabase() {
    abstract fun todoDao(): TodoDao
    companion object {
    private var instance: TodoDatabase? = null
    fun getInstance(context: Context): TodoDatabase =
    instance ?: synchronized(this) {
    instance ?: buildDatabase(context).also { instance = it }
    }
    private fun buildDatabase(context: Context): TodoDatabase =
    Room.databaseBuilder(
    context,
    TodoDatabase::class.java,
    TodoDatabase::class.java.simpleName
    ).build()
    }
    }

    View full-size slide

  16. 3PPN5PEP&OUJUZ 5PEP%BP
    @Entity(tableName = "todo")
    data class TodoEntity(
    val name: String,
    val category: Int
    ) {
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    }
    @Dao
    interface TodoDao {
    @Query("SELECT * FROM todo")
    fun selectAll(): List
    @Query("SELECT * FROM todo WHERE id = :id")
    fun selectById(id: Int): TodoEntity
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertAll(list: List)
    @Query("SELECT COUNT(*) FROM todo")
    fun count(): Int
    }

    View full-size slide

  17. @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [O_MR1])
    class TodoDataBaseTest {
    private lateinit var database: TodoDatabase
    @Before
    fun openDatabase() {
    database = Room
    .inMemoryDatabaseBuilder(
    InstrumentationRegistry.getInstrumentation().targetContext,
    TodoDatabase::class.java
    ).allowMainThreadQueries()
    .build()
    }
    @After
    fun closeDatabase() {
    database.close()
    }
    @Test
    @Throws(Exception::class)
    fun insert() {
    assertThat(database.todoDao().count(), `is`(0))
    val list = listOf(
    TodoEntity(
    name = "Robolectric࢖͏",
    category = 1
    ),
    TodoEntity(
    name = "JUnit࢖͏",
    category = 1
    )
    )
    database.todoDao().upsertAll(list)
    assertThat(database.todoDao().count(), `is`(2))
    }
    }

    View full-size slide

  18. @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [O_MR1])
    class TodoDataBaseTest {
    private lateinit var database: TodoDatabase
    @Before
    fun openDatabase() {
    database = Room
    .inMemoryDatabaseBuilder(
    InstrumentationRegistry.getInstrumentation().targetContext,
    TodoDatabase::class.java
    ).allowMainThreadQueries()
    .build()
    }
    @After
    fun closeDatabase() {
    database.close()
    }
    @Test
    @Throws(Exception::class)
    fun insert() {
    assertThat(database.todoDao().count(), `is`(0))
    val list = listOf(
    TodoEntity(
    name = "Robolectric࢖͏",
    category = 1
    ),
    TodoEntity(
    name = "JUnit࢖͏",
    category = 1
    )
    )
    database.todoDao().upsertAll(list)
    assertThat(database.todoDao().count(), `is`(2))
    }
    }
    private lateinit var database:
    TodoDatabase
    @Before
    fun openDatabase() {
    database = Room
    .inMemoryDatabaseBuilder(
    InstrumentationRegistry.getInstrumentat
    ion().targetContext,
    TodoDatabase::class.java
    ).allowMainThreadQueries()
    .build()
    }

    View full-size slide

  19. @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [O_MR1])
    class TodoDataBaseTest {
    private lateinit var database: TodoDatabase
    @Before
    fun openDatabase() {
    database = Room
    .inMemoryDatabaseBuilder(
    InstrumentationRegistry.getInstrumentation().targetContext,
    TodoDatabase::class.java
    ).allowMainThreadQueries()
    .build()
    }
    @After
    fun closeDatabase() {
    database.close()
    }
    @Test
    @Throws(Exception::class)
    fun insert() {
    assertThat(database.todoDao().count(), `is`(0))
    val list = listOf(
    TodoEntity(
    name = "Robolectric࢖͏",
    category = 1
    ),
    TodoEntity(
    name = "JUnit࢖͏",
    category = 1
    )
    )
    database.todoDao().upsertAll(list)
    assertThat(database.todoDao().count(), `is`(2))
    }
    }
    @After
    fun closeDatabase() {
    database.close()
    }

    View full-size slide

  20. @RunWith(RobolectricTestRunner::class)
    @Config(sdk = [O_MR1])
    class TodoDataBaseTest {
    private lateinit var database: TodoDatabase
    @Before
    fun openDatabase() {
    database = Room
    .inMemoryDatabaseBuilder(
    InstrumentationRegistry.getInstrumentation().targetContext,
    TodoDatabase::class.java
    ).allowMainThreadQueries()
    .build()
    }
    @After
    fun closeDatabase() {
    database.close()
    }
    @Test
    @Throws(Exception::class)
    fun insert() {
    assertThat(database.todoDao().count(), `is`(0))
    val list = listOf(
    TodoEntity(
    name = "Robolectric࢖͏",
    category = 1
    ),
    TodoEntity(
    name = "JUnit࢖͏",
    category = 1
    )
    )
    database.todoDao().upsertAll(list)
    assertThat(database.todoDao().count(), `is`(2))
    }
    }
    @Test
    @Throws(Exception::class)
    fun insert() {
    assertThat(database.todoDao().count(),
    `is`(0))
    val list = listOf(
    TodoEntity(
    name = "Robolectric࢖͏",
    category = 1
    ),
    TodoEntity(
    name = "JUnit࢖͏",
    category = 1
    )
    )
    database.todoDao().insertAll(list)
    assertThat(database.todoDao().count(),
    `is`(2))
    }

    View full-size slide

  21. FYQFDUFEΛΘ͟ͱʹ͢ΔͱͪΌΜͱࣦഊ͠·ͨ͠
    ˎ৺഑ͳͷͰҰԠ

    View full-size slide

  22. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View full-size slide