Skip to content

Instantly share code, notes, and snippets.

@txusballesteros
Created December 29, 2017 13:41
Show Gist options
  • Save txusballesteros/62d3023ac73c6162d6d7337591154c93 to your computer and use it in GitHub Desktop.
Save txusballesteros/62d3023ac73c6162d6d7337591154c93 to your computer and use it in GitHub Desktop.
Property Base Test
package com.txusballesteros.mytaxi.core.domain.repository
import com.pholser.junit.quickcheck.generator.GenerationStatus
import com.pholser.junit.quickcheck.generator.Generator
import com.pholser.junit.quickcheck.random.SourceOfRandomness
import com.txusballesteros.mytaxi.core.domain.model.MapState
class MapStatePropertyGenerator: Generator<MapState>(MapState::class.java) {
companion object {
private val MINIMUM_VALUE = 0
private val MAXIMUM_VALUE = MapState.values().size.dec()
}
override fun generate(random: SourceOfRandomness, status: GenerationStatus): MapState {
val current = random.nextInt(MINIMUM_VALUE, MAXIMUM_VALUE)
return MapState.values()[current]
}
}
package com.txusballesteros.mytaxi.core.domain.repository
import com.nhaarman.mockito_kotlin.*
import com.pholser.junit.quickcheck.From
import com.pholser.junit.quickcheck.Property
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck
import com.txusballesteros.mytaxi.core.domain.model.MapState
import com.txusballesteros.mytaxi.core.domain.repository.stream.MapStateStream
import com.txusballesteros.mytaxi.core.testing.PropertyUnitTest
import org.junit.runner.RunWith
@RunWith(JUnitQuickcheck::class)
class MapStateRepositoryPropertyTest: PropertyUnitTest() {
private val streamMock: MapStateStream = mock()
private lateinit var repository: MapStateRepository
override fun onPrepareTest() {
repository = MapStateRepository(streamMock)
}
@Property
fun shouldRequestNewBooking(@From(MapStatePropertyGenerator::class) mapState: MapState) {
whenever(streamMock.valueOrNull()).thenReturn(mapState)
repository.newBookingRequested()
if (mapState == MapState.RADAR) {
verify(streamMock).offer(eq(MapState.OFFER))
} else {
verify(streamMock, never()).offer(any())
}
}
@Property
fun shouldRequestCashPayment(@From(MapStatePropertyGenerator::class) mapState: MapState) {
whenever(streamMock.valueOrNull()).thenReturn(mapState)
repository.cashPaymentRequested()
if (mapState == MapState.OFFER) {
verify(streamMock).offer(eq(MapState.PAYMENT))
} else {
verify(streamMock, never()).offer(any())
}
}
@Property
fun shouldCancelBookingRequest(@From(MapStatePropertyGenerator::class) mapState: MapState) {
whenever(streamMock.valueOrNull()).thenReturn(mapState)
repository.cancelBookingRequest()
verify(streamMock).offer(eq(MapState.RADAR))
}
@Property
fun shouldCancelCashPaymentRequest(@From(MapStatePropertyGenerator::class) mapState: MapState) {
whenever(streamMock.valueOrNull()).thenReturn(mapState)
repository.cancelCashPayment()
verify(streamMock).offer(eq(MapState.RADAR))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment