Skip to content

Instantly share code, notes, and snippets.

View ProArun's full-sized avatar

Arun Kumar Aditya ProArun

View GitHub Profile
@ProArun
ProArun / ExampleUnitTest.kt
Last active August 19, 2023 13:36
Unit Test 1
class ExampleUnitTest{
@Test
fun addition_isCorrect(){
assertEquals(expected:4,actual:2+2)
}
}
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest{
@Test
fun useAppContext(){
//Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.cheezycode.unittestingexample", appContext.packageName)
}
}
class Helper {
fun isPallindrome(input: String): Boolean{
var i = 0
var j = input.length - 1
var result = true
while (i < j){
if(input[i] != input[j]){
result = false
break
@ProArun
ProArun / ParameterizedExample.kt
Created August 19, 2023 14:13
parameterized Test Case
@RunWith(value = Parameterized::class)
class ParameterizedExample(val input:String,val expectedValue:Boolean){
@Test
fun test(){
val helper = Helper()
val result = helper.isPallindrome(input)
assertEquals(expectedValue,result)
}
@ProArun
ProArun / Quote.kt
Created August 19, 2023 14:49
Instrumentation testing1
data class Quote(val text:String,val author:String)
@ProArun
ProArun / QuoteManagerTest.kt
Created August 19, 2023 14:53
instrumentation test
import android.content.Context
import androidx.test.core.app.ApplicationProvider
import com.google.gson.JsonSyntaxException
import org.junit.Assert.*
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.io.FileNotFoundException
@ProArun
ProArun / PasswordTest.kt
Created August 19, 2023 15:42
Unit test Example
class PasswordTest{
@Test
fun `validatePassword blank input expected required field`(){
val sut = Utils()
val result = sut.validatePassword(" ")
assertEquals("Password is required field", result)
}
@Test
fun `validatePassword 2CharInput expected validation Msg`(){
@ProArun
ProArun / LiveDataUtils.kt
Created August 19, 2023 16:32
Room database testing
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
fun <T> LiveData<T>.getOrAwaitValue(
time: Long = 2,
timeUnit: TimeUnit = TimeUnit.SECONDS,
afterObserve: () -> Unit = {}
@ProArun
ProArun / User.kt
Created August 19, 2023 17:21
Mocking using mockito
data class User(
val id:Int,
val name:String,
val email:String,
val password:String
)
enum class LOGIN_STATUS{
INVALID_USER,
INVALID_PASSWORD,
@ProArun
ProArun / QuoteManagerTest.kt
Created August 19, 2023 17:38
convert instrumentation test to jvm test
class QuoteManagerTest{
@Mock
lateinit var context: Context
@Mock
lateinit var assertManager: AssertManager
@Before
fun setup(){