Skip to content

Instantly share code, notes, and snippets.

@brianPlummer
Last active June 28, 2018 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianPlummer/4596451941684a8c07d9d73bbf2933f8 to your computer and use it in GitHub Desktop.
Save brianPlummer/4596451941684a8c07d9d73bbf2933f8 to your computer and use it in GitHub Desktop.
Loading a json file from an Android unit test
@Test
fun loadData () {
val moshi = Moshi.Builder().build()
val listType = Types.newParameterizedType(List::class.java, Complaint::class.java)
val source = Okio.buffer(Okio.source(inputStream("/gov_complaints.json")))
val complaints = moshi.adapter<List<Complaint>>(listType).fromJson(source)
Assert.assertNotNull(complaints)
Assert.assertEquals(complaints!!.isEmpty(), false)
}
private fun inputStream(path: String): InputStream {
return ComplaintsTest::class.java!!.getResourceAsStream(path)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment