Skip to content

Commit

Permalink
Write Unit tests for the SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanuelkehinde committed Mar 31, 2020
1 parent ef9abae commit cabc59b
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0-beta01'

//Buffup SDK
implementation project(':buffup')
Expand All @@ -57,5 +58,4 @@ dependencies {
//Exoplayer
implementation "com.google.android.exoplayer:exoplayer-core:$rootProject.exoplayer_version"
implementation "com.google.android.exoplayer:exoplayer-ui:$rootProject.exoplayer_version"
androidTestImplementation 'androidx.test:rules:1.2.0-beta01'
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

/**
* Created by Emmanuel Kehinde on 2020-03-31.
*/

@LargeTest
@RunWith(AndroidJUnit4::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class StreamPlayerViewModelTest {

@Before
fun setUp() {
streamPlayerViewModel = StreamPlayerViewModel() //
streamPlayerViewModel = StreamPlayerViewModel()
}

@Test
Expand Down
7 changes: 6 additions & 1 deletion buffup/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'

// Testing
testImplementation 'org.mockito:mockito-core:2.19.0'
testImplementation 'androidx.arch.core:core-testing:2.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
androidTestImplementation 'androidx.test:rules:1.2.0-beta01'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.4'

//MotionLayout support
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
Expand Down
12 changes: 6 additions & 6 deletions buffup/src/main/java/com/emmanuelkehinde/buffup/model/Buff.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import com.google.gson.annotations.SerializedName
* Created by Emmanuel Kehinde on 2020-03-27.
*/
data class Buff(
val id: Long?,
val priority: Int,
val author: Author?,
val question: Question?,
val answers: List<Answer>?,
val id: Long? = null,
val priority: Int? = null,
val author: Author? = null,
val question: Question? = null,
val answers: List<Answer>? = null,

@SerializedName("time_to_show")
val timeToShow: Int?
val timeToShow: Int? = null
) {

data class Author(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.emmanuelkehinde.buffup

import com.emmanuelkehinde.buffup.remote.BuffApiService
import com.emmanuelkehinde.buffup.remote.BuffResult
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

/**
* Created by Emmanuel Kehinde on 2020-03-31.
*/
@RunWith(JUnit4::class)
class BuffApiServiceTest {

@Test
fun `fetchBuff returns Success response`() = runBlocking {
val result = withContext(Dispatchers.Default) {
BuffApiService.getBuff("1")
}
assert(result is BuffResult.Success)
}

@Test
fun `ensure buffId param falls between 1 to 5`() = runBlocking {
val result = withContext(Dispatchers.Default) {
BuffApiService.getBuff("6")
}
assert(result is BuffResult.Error)
}

}
35 changes: 35 additions & 0 deletions buffup/src/test/java/com/emmanuelkehinde/buffup/BuffViewTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.emmanuelkehinde.buffup

import android.app.Activity
import com.emmanuelkehinde.buffup.exceptions.MissingInitializationException
import com.emmanuelkehinde.buffup.view.BuffView
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.junit.MockitoJUnitRunner

/**
* Created by Emmanuel Kehinde on 2020-03-31.
*/

@RunWith(MockitoJUnitRunner::class)
class BuffViewTest {

@Mock
lateinit var activity: Activity

@Mock
lateinit var buffView: BuffView

@Test
fun buffViewThrowsInitializationExceptionWhenStartIsCalled() {
try {
buffView.start(activity)
} catch (e: MissingInitializationException) {

} catch (e: NullPointerException) {

}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mock-maker-inline

0 comments on commit cabc59b

Please sign in to comment.