1+ package io.mockk.bdd
2+
3+ import androidx.test.ext.junit.runners.AndroidJUnit4
4+ import io.mockk.clearAllMocks
5+ import io.mockk.mockk
6+ import io.mockk.slot
7+ import kotlinx.coroutines.runBlocking
8+ import org.junit.After
9+ import org.junit.Before
10+ import org.junit.Test
11+ import org.junit.runner.RunWith
12+ import kotlin.test.assertEquals
13+ import kotlin.test.assertTrue
14+
15+ /* *
16+ * Android instrumentation tests for BDD-style API.
17+ * Verifies that the BDD functions work correctly in Android environment.
18+ */
19+ @RunWith(AndroidJUnit4 ::class )
20+ class BDDAndroidTest {
21+
22+ interface AndroidTestService {
23+ fun getValue (): String
24+ fun setValue (value : String )
25+ suspend fun getAsyncValue (): List <String >
26+ fun processData (data : Any ): Boolean
27+ }
28+
29+ private lateinit var service: AndroidTestService
30+
31+ @Before
32+ fun setup () {
33+ service = mockk()
34+ }
35+
36+ @After
37+ fun tearDown () {
38+ clearAllMocks()
39+ }
40+
41+ @Test
42+ fun givenShouldWorkInAndroidTests () {
43+ // Given
44+ given { service.getValue() } returns " Android BDD Style"
45+
46+ // When
47+ val result = service.getValue()
48+
49+ // Then
50+ assertEquals(" Android BDD Style" , result)
51+ }
52+
53+ @Test
54+ fun coGivenShouldWorkInAndroidTests () = runBlocking {
55+ // Given
56+ coGiven { service.getAsyncValue() } returns listOf (" Android" , " BDD" , " Async" )
57+
58+ // When
59+ val result = service.getAsyncValue()
60+
61+ // Then
62+ assertEquals(listOf (" Android" , " BDD" , " Async" ), result)
63+ }
64+
65+ @Test
66+ fun thenShouldWorkInAndroidTests () {
67+ // Given
68+ val captureSlot = slot<String >()
69+ given { service.setValue(capture(captureSlot)) } returns Unit
70+
71+ // When
72+ service.setValue(" Android test value" )
73+
74+ // Then
75+ then { service.setValue(" Android test value" ) }
76+ assertTrue(captureSlot.isCaptured)
77+ assertEquals(" Android test value" , captureSlot.captured)
78+ }
79+
80+ @Test
81+ fun coThenShouldWorkInAndroidTests () = runBlocking {
82+ // Given
83+ coGiven { service.getAsyncValue() } returns listOf (" test" )
84+
85+ // When
86+ service.getAsyncValue()
87+
88+ // Then
89+ coThen { service.getAsyncValue() }
90+ }
91+
92+ @Test
93+ fun thenWithParametersShouldWorkInAndroidTests () {
94+ // Given
95+ given { service.getValue() } returns " test"
96+
97+ // When
98+ repeat(3 ) { service.getValue() }
99+
100+ // Then
101+ then(exactly = 3 ) { service.getValue() }
102+ then(atLeast = 2 ) { service.getValue() }
103+ then(atMost = 5 ) { service.getValue() }
104+ }
105+
106+ @Test
107+ fun shouldWorkWithAndroidSpecificObjects () {
108+ // Given
109+ given { service.processData(any()) } returns true
110+
111+ // When
112+ val result = service.processData(" Android specific data" )
113+
114+ // Then
115+ then { service.processData(any()) }
116+ assertEquals(true , result)
117+ }
118+ }
0 commit comments