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. (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
  2. .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" } }
  3. .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") } }
  4. .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)
  5. .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])
  6. .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")
  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<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") } }
  8. 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() } }
  9. 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 }
  10. @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)) } }
  11. @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() }
  12. @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() }
  13. @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)) }