What’s Testing? / take measures to check the quality, performance, or reliability of (something), especially before putting it into widespread use or practice.
/ @Test fun testConversion() { //Declare necessary inputs val stringList = listOf("GDG", "Yangon", "DevFest") //Declare expected value val expected = "GDG,Yangon,DevFest" //Declare actual val actual = StringUtils.convertStringListToString(stringList) }
/ @Test fun testConversion() { //Declare necessary inputs val stringList = listOf //Check if two values are equal("GDG", "Yangon", "DevFest") //Declare expected value val expected = "GDG,Yangon,DevFest" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
/ @Test fun testWithFourStrings() { //Declare necessary inputs val stringList = listOf("GDG", "Yangon", "DevFest", "Android") //Declare expected value val expected = "GDG,Yangon,DevFest,Android" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
/ @Test fun testWithThreeStringCaseTwo() { //Declare necessary inputs val stringList = listOf("GDG", "Yangon", "Best") //Declare expected value val expected = "GDG,Yangon,Best" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
/ fun convertStringListToString(stringList: List): String { val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(",") } } return stringBuilder.toString() }
Strategies to green-light • Fake It till you make it • “Return a constant and gradually replace constants with variables until you have the real code” • Use Obvious Implementation /
/ fun convertStringListToString(stringList: List): String { val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(",") } } return stringBuilder.toString() }
Strategies to green-light • Fake It till you make it • “Return a constant and gradually replace constants with variables until you have the real code” • Use Obvious Implementation • “Type in the real implementation.” /
/ @Test fun testWithDifferentDelimiter() { //Declare necessary inputs val stringList = listOf("GDG", "Yangon", "Best") //Declare expected value val expected = "GDG|Yangon|Best" //Declare actual val actual = StringUtils.convertStringListToString(stringList) //Check if two values are equal org.junit.Assert.assertEquals(expected, actual) }
/ fun convertStringListToString(stringList: List): String { val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(",") } } return stringBuilder.toString() }
/ val stringBuilder = StringBuilder() stringList.forEachIndexed { index, string -> stringBuilder.append(string) //If Index is last, we don't append delimiter if (index != stringList.lastIndex) { stringBuilder.append(delimiter) } } return stringBuilder.toString()
/ fun testTrueCase() { val fakeDataSource = object : DataSource { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return true } } val expected = 9 val getCalculatedValue = GetCalculatedValue() val actual = getCalculatedValue.execute() assertEquals(expected, actual) }
/ fun testTrueCase() { val fakeDataSource = object : DataSource { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return true } } val expected = 9 val getCalculatedValue = GetCalculatedValue() val actual = getCalculatedValue.execute(fakeDataSource) assertEquals(expected, actual) }
/ fun testFalseCase() { val fakeDataSource = object : DataSource { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return false } } val expected = 6 val getCalculatedValue = GetCalculatedValue() val actual = getCalculatedValue.execute(fakeDataSource) assertEquals(expected, actual) }
/ val getCalculatedValue = GetCalculatedValue(fakeDataSource) val actual = getCalculatedValue.execute() class GetCalculatedValue constructor(val dataSource: DataSource) { }
/ val fakeDataSource = object : DataSource { override fun getValue(): Int { return 3 } override fun isSomething(): Boolean { return true } } val getCalculatedValue = GetCalculatedValue(fakeDataSource) val actual = getCalculatedValue.execute()
/ @Test fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9 val actual = getCalculatedValue.execute() assertEquals(expected, actual) }
/ @Test fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9 val actual = getCalculatedValue.execute() assertEquals(expected, actual) }
/ fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9 val actual = getCalculatedValue.execute() verify(fakeDataSource).isSomething() verify(fakeDataSource).getValue() assertEquals(expected, actual) }
/ fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9 val actual = getCalculatedValue.execute() verify(fakeDataSource).isSomething() verify(fakeDataSource).getValue() assertEquals(expected, actual) }
/ fun testTrueCase() { whenever(fakeDataSource.isSomething()).thenReturn(true) whenever(fakeDataSource.getValue()).thenReturn(3) val expected = 9 val getCalculatedValue = GetCalculatedValue(fakeDataSource) val actual = getCalculatedValue.execute() verify(fakeDataSource, times(1)).isSomething() verify(fakeDataSource, times(1)).getValue() assertEquals(expected, actual) }