val URL = "https://m.imdb.com"} fun url() : String { return cacheService.retrieve<Movie>(Detail)?.let { "$URL/title/${imdb.value}" } ?: URL } W R I T I N G U N I T T E S T
fun `cache return value`() { whenever(cache.retrieve<Movie>(any())) .thenAnswer { movie } } private val movie = Movie(name = "Madman", url = "tt123") Mockito/Mockito Kotlin W R I T I N G U N I T T E S T - M O C K I N G
"receipts" fun create(receiptInfo: FileReceipt): File { val pathFile = context.cacheDir return pathFile .resolve(RECEIPTS_PATH) .apply { if (!exists()) mkdir() } .resolve(“receipt.pdf”) .apply { this.writeBytes(receiptInfo.byteArray) } } } W R I T I N G U N I T T E S T
@Before fun `before each test`() { val context = RuntimeEnvironment.application.applicationContext creator = ReceiptFileCreator(context) } @Test fun `given a file receipt should create a file`() { val file = creator.create(receiptInfo) assertThat(file.name).isEqualTo("fileName.pdf") } W R I T I N G U N I T T E S T - A N D R O I D robolectric
val api: BlogService) { fun process(): Observable<Result> { return api .retrieveBlogPost() .map{ text -> Result( everyTenthPosition = GetEveryTenthPosition(text), wordCount = CountTheWords(text)) } } } data class Result( val everyTenthPosition: String, val wordCount:Map<String, Int> )
E G R AT I O N T E S T val useCase = mock<BlogPostProcessor>() val vm = BlogPostProcessViewModel(useCase) val api = mock<BlogService>() val useCase = BlogPostProcessor(api) val vm = BlogPostProcessViewModel(useCase)
E G R AT I O N T E S T fun `api return blog post`(){ val post = "a string to test the count count the words words" whenever(api.retrieveBlogPost()) .thenAnswer{ Observable.just(post) } } val expected = Result( wordCount = mapOf( "test" to 1, "count" to 2, "words" to 2), everyTenthPosition = "t t n n h w” ) @Test fun `given a blog post process the words`(){ `api return blog post`() val test = vm.retrieveBlogPostInformation().test() test.assertResult(expected) } RxJava Test
E P TA N C E T E S T @get:Rule val activity = ActivityTestRule(MainActivity::class.java, true, false) val response = MockResponse() .setBody(shows) //json .setResponseCode(200) var server = MockWebServer() @Test fun verify_content_proper_showed() { server.start(8080) server.enqueue(response) activity.launchActivity() onView(withId(R.id.toolbar)).check(matches(isDisplayed())) onView(withId(R.id.moviesRv)).check(matches(isDisplayed())) }