add BoardRepositoryTest

This commit is contained in:
Raffaele Mignone 2019-12-03 18:03:18 +01:00
parent 2b0a9b690b
commit a645d5f64b
Signed by: norangebit
GPG Key ID: F5255658CB220573
2 changed files with 173 additions and 0 deletions

View File

@ -59,6 +59,8 @@ dependencies {
// TESTING // TESTING
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
testImplementation "io.mockk:mockk:1.9.3"
testImplementation "com.squareup.okhttp3:mockwebserver:4.2.1"
androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion" androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

View File

@ -0,0 +1,171 @@
package it.unisannio.ding.ids.wedroid.app
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import it.unisannio.ding.ids.wedroid.app.data.dao.BoardDao
import it.unisannio.ding.ids.wedroid.app.data.entity.Board
import it.unisannio.ding.ids.wedroid.app.data.repository.BoardRepository
import it.unisannio.ding.ids.wedroid.app.util.PreferenceReader
import it.unisannio.ding.ids.wedroid.wrapper.api.BoardService
import it.unisannio.ding.ids.wedroid.wrapper.entity.BoardBackgroundColor
import junit.framework.TestCase.*
import kotlinx.coroutines.runBlocking
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import org.junit.After
import org.junit.Before
import org.junit.Test
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.net.HttpURLConnection
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlin.reflect.full.declaredFunctions
import kotlin.reflect.jvm.isAccessible
class BoardRepositoryTest {
private val reader = mockk<PreferenceReader>()
private val webServer = MockWebServer()
private lateinit var service: BoardService
private lateinit var latch: CountDownLatch
@Before
fun setUp() {
webServer.start()
service = Retrofit.Builder()
.baseUrl(webServer.url("/"))
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(BoardService::class.java)
every { reader.userId } returns "user id"
latch = CountDownLatch(1)
}
@After
fun teardown() {
webServer.close()
}
@Test
fun insertWithOkOnServer() {
val dao = mockk<BoardDao>()
var inInsert = false
coEvery {
dao.insert(any())
} answers {
inInsert = true
assertEquals("id", arg<Board>(0).id)
assertEquals("title", arg<Board>(0).title)
latch.countDown()
}
webServer.enqueue(
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
.setBody("{ \"_id\": \"id\" }")
)
val repository = BoardRepository(
dao, service, reader
)
repository.insertBoard(
"title", true, BoardBackgroundColor.LIMEGREEN
)
latch.await(5, TimeUnit.SECONDS)
assertTrue(inInsert)
}
@Test
fun notInsertWithErrorOnServer() {
val dao = mockk<BoardDao>()
var inInsert = false
coEvery {
dao.insert(any() as Board)
} answers {
inInsert = true
latch.countDown()
}
webServer.enqueue(
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST)
)
val repository = BoardRepository(
dao, service, reader
)
repository.insertBoard(
"title", true, BoardBackgroundColor.LIMEGREEN
)
latch.await(3, TimeUnit.SECONDS)
assertFalse(inInsert)
}
@Test
fun deleteWithOkOnServer() {
val dao = mockk<BoardDao>()
var inDelete = false
coEvery {
dao.delete(any() as Board)
} answers {
assertEquals("id", arg<Board>(0).id)
inDelete = true
latch.countDown()
}
webServer.enqueue(
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_OK)
)
val repository = BoardRepository(
dao, service, reader
)
repository.deleteBoard("id")
latch.await(5, TimeUnit.SECONDS)
assertTrue(inDelete)
}
@Test
fun notDeleteWithErrorOnServer() {
val dao = mockk<BoardDao>()
var inDelete = false
coEvery {
dao.delete(any() as Board)
} answers {
assertEquals("id", arg<Board>(0).id)
inDelete = true
latch.countDown()
}
webServer.enqueue(
MockResponse()
.setResponseCode(HttpURLConnection.HTTP_BAD_REQUEST)
)
val repository = BoardRepository(
dao, service, reader
)
repository.deleteBoard("id")
latch.await(3, TimeUnit.SECONDS)
assertFalse(inDelete)
}
}