Android Instrumented Unit Test With Hamcrest (Testing Moshi Adapter)
May 14, 2018There are 2 types on Unit Test in Android:
- Local Unit Tests: run on your development machine (not Android devices), run faster, no dependencies on Android OS/services (require mock object). Identified as
src/test
in Android Studio. - Instrumented Unit Tests: run on Android device on emulator, run slower (compile & deploy), better simulation of production environment. Identified as
src/androidTest
in Android Studio.
Android Studio usually created ExampleInstrumentedTest
in src/androidTest
(app/java/{package_name}/androidTest
)
Let’s create the following MoshiTest
.
- Put initialization code at
@Before
. It shall be run once per class before test begin. - Use
@After
to close objects when all test are completed. - Use
@Test(expected = ...)
when exception is expected - There are many ways to check for assertion. I use
org.hamcrest.MatcherAssert.assertThat
, or you can useorg.junit.Assert.assertEquals
as well.
import org.hamcrest.CoreMatchers.`is` as Is
@RunWith(AndroidJUnit4::class)
class MoshiTest {
lateinit var moshi: Moshi
lateinit var adapter: JsonAdapter<PhotoPin>
@Before
fun init() {
moshi = Moshi.Builder()
.add(LocationAdapter())
.add(KotlinJsonAdapterFactory())
.build()
adapter = moshi.adapter<PhotoPin>(PhotoPin::class.java)
}
@After
fun destroy() {
}
@Test(expected = EOFException::class)
fun testEmptyJson() {
var item = adapter.fromJson("")!!
assertThat(item.location, Is(nullValue()))
}
@Test
fun testLocationMissing() {
var item = adapter.fromJson("""{}""")!!
assertThat(item.location, Is(nullValue()))
}
@Test
fun testLocationValue() {
var item = adapter.fromJson("""{"geo":"1.0, 2"}""")!!
assertThat(item.location, Is(notNullValue()))
assertThat(item.location?.lat, Is(1.0))
assertThat(item.location?.lng, Is(2.0))
}
}
To run the test, right click on MoshiTest
and select run 'MoshiTest'
(CTRL + SHIFT + F10).
Once completed, the result is shown in the Run
tab.
NOTE: It is more logical to run the above Moshi
test as Local Unit Tests
since there is no Android system dependencies.
- algo-trading
- algolia
- analytics
- android
- android-ktx
- android-permission
- android-studio
- apps-script
- bash
- binance
- bootstrap
- bootstrapvue
- chartjs
- chrome
- cloud-functions
- coding-interview
- contentresolver
- coroutines
- crashlytics
- crypto
- css
- dagger2
- datastore
- datetime
- docker
- eslint
- firebase
- firebase-auth
- firebase-hosting
- firestore
- firestore-security-rules
- flask
- fontawesome
- fresco
- git
- github
- glide
- godot
- google-app-engine
- google-cloud-storage
- google-colab
- google-drive
- google-maps
- google-places
- google-play
- google-sheets
- gradle
- html
- hugo
- inkscape
- java
- java-time
- javascript
- jetpack-compose
- jetson-nano
- kotlin
- kotlin-serialization
- layout
- lets-encrypt
- lifecycle
- linux
- logging
- lubuntu
- markdown
- mate
- material-design
- matplotlib
- md5
- mongodb
- moshi
- mplfinance
- mysql
- navigation
- nginx
- nodejs
- npm
- nuxtjs
- nvm
- pandas
- payment
- pip
- pwa
- pyenv
- python
- recylerview
- regex
- room
- rxjava
- scoped-storage
- selenium
- social-media
- ssh
- ssl
- static-site-generator
- static-website-hosting
- sublime-text
- ubuntu
- unit-test
- uwsgi
- viewmodel
- viewpager2
- virtualbox
- vue-chartjs
- vue-cli
- vue-router
- vuejs
- vuelidate
- vuepress
- web-development
- web-hosting
- webpack
- windows
- workmanager
- wsl
- yarn