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

0f9e5363528f70e1b274f24ada191eb8?s=47 moriiimo
January 28, 2019

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

0f9e5363528f70e1b274f24ada191eb8?s=128

moriiimo

January 28, 2019
Tweet

Transcript

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

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

  3. SPCPMFDUSJD

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

  5. (FUUJOH4UBSUFE dependencies { testImplementation 'org.robolectric:robolectric:4.1' } android { testOptions {

    unitTests { includeAndroidResources = true } } }
  6. (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
  7. .BJO"DUJWJUZ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?)

    { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) title = "Robolectric onCreate" val button = findViewById<Button>(R.id.button) button.setOnClickListener { findViewById<TextView>(R.id.textView).text = "Hello Robolectric" } } override fun onResume() { super.onResume() title = "Robolectric onResume" } }
  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<Button>(R.id.button).performClick() assertTrue(activity.findViewById<TextView>(R.id.textView).text == "Hello Robolectric") } }
  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<Button>(R.id.button).performClick() assertTrue(activity.findViewById<TextView>(R.id.textView).te xt == "Hello Robolectric") } } @RunWith(RobolectricTestRunner::class)
  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<Button>(R.id.button).performClick() assertTrue(activity.findViewById<TextView>(R.id.textView).te xt == "Hello Robolectric") } } @Config(sdk = [LOLLIPOP])
  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<Button>(R.id.button).performClick() assertTrue(activity.findViewById<TextView>(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")
  12. .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<Button>(R.id.button).performClick() assertTrue(activity.findViewById<TextView>(R.id.textView).te xt == "Hello Robolectric") } } controller.resume() assertTrue(activity.title.toString() == "Robolectric onResume”) activity.findViewById<Button>(R.id.button) .performClick() assertTrue(activity.findViewById<TextView> (R.id.textView).text == "Hello Robolectric") } }
  13. ࣮ߦͯ͠ΈΔʂ

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

  15. 4VDDFTT

  16. ☺ʁ

  17. Ϣʔεέʔε

  18. 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() } }
  19. 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<TodoEntity> @Query("SELECT * FROM todo WHERE id = :id") fun selectById(id: Int): TodoEntity @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertAll(list: List<TodoEntity>) @Query("SELECT COUNT(*) FROM todo") fun count(): Int }
  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)) } }
  21. @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() }
  22. @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() }
  23. @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)) }
  24. None
  25. FYQFDUFEΛΘ͟ͱʹ͢ΔͱͪΌΜͱࣦഊ͠·ͨ͠ ˎ৺഑ͳͷͰҰԠ

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