Skip to content

Commit 1fc8658

Browse files
Small fixes
1 parent 2bbbfc8 commit 1fc8658

File tree

17 files changed

+166
-32
lines changed

17 files changed

+166
-32
lines changed

.idea/codeStyles/Project.xml

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/compiler.xml

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/gradle.xml

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/jarRepositories.xml

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tutorial4-2Kotlin-MockK/build.gradle

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ dependencies {
3939
implementation 'androidx.appcompat:appcompat:1.1.0'
4040
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
4141

42+
// Coroutines
43+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutinesVersion"
44+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutinesVersion"
45+
4246

4347
// Mockito for JUnit 5
4448
testImplementation 'org.mockito:mockito-junit-jupiter:3.0.0'
@@ -57,10 +61,11 @@ dependencies {
5761
// (Optional) If you need "Parameterized Tests"
5862
testImplementation "org.junit.jupiter:junit-jupiter-params:5.4.0"
5963

60-
// (Optional) If you also have JUnit 4-based tests
61-
// testImplementation "junit:junit:4.12"
62-
// testRuntimeOnly "org.junit.vintage:junit-vintage-engine:5.3.1"
64+
// Coroutines Test
65+
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.6'
6366

67+
// Truth
68+
testImplementation "com.google.truth:truth:1.0.1"
6469

6570
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
6671
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.smarttoolfactory.tutorial4_2kotlin_mockk.viewmodel
2+
3+
import kotlinx.coroutines.delay
4+
5+
class CalculationUseCase {
6+
7+
8+
suspend fun doCalculationWithDelay(base: Int): Int {
9+
10+
delay(2000)
11+
return base * 10
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.smarttoolfactory.tutorial4_2kotlin_mockk.viewmodel
2+
3+
class CalculationViewModel(val calculationUseCase: CalculationUseCase) {
4+
5+
6+
}

Tutorial4-2Kotlin-MockK/src/test/java/com/smarttoolfactory/tutorial4_2kotlin_mockk/Test10VerifyAtLeastAtMostExactly.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package com.smarttoolfactory.tutorial4_2kotlin_mockk
33
import com.smarttoolfactory.tutorial4_2kotlin_mockk.car.Car
44
import io.mockk.confirmVerified
55
import io.mockk.mockk
6+
import io.mockk.verify
67
import org.junit.jupiter.api.Test
78

89
class Test10VerifyAtLeastAtMostExactly {
@@ -17,11 +18,11 @@ class Test10VerifyAtLeastAtMostExactly {
1718
car.accelerate(fromSpeed = 20, toSpeed = 30)
1819

1920
// all pass
20-
// verify(atLeast = 3) { car.accelerate(allAny(), allAny()) }
21-
// verify(atMost = 2) { car.accelerate(fromSpeed = 10, toSpeed = or(20, 30)) }
22-
// verify(exactly = 1) { car.accelerate(fromSpeed = 10, toSpeed = 20) }
21+
verify(atLeast = 3) { car.accelerate(allAny(), allAny()) }
22+
verify(atMost = 2) { car.accelerate(fromSpeed = 10, toSpeed = or(20, 30)) }
23+
verify(exactly = 1) { car.accelerate(fromSpeed = 10, toSpeed = 20) }
2324
// // means no calls were performed
24-
// verify(exactly = 0) { car.accelerate(fromSpeed = 30, toSpeed = 10) }
25+
verify(exactly = 0) { car.accelerate(fromSpeed = 30, toSpeed = 10) }
2526

2627
confirmVerified(car)
2728

Tutorial4-2Kotlin-MockK/src/test/java/com/smarttoolfactory/tutorial4_2kotlin_mockk/Test12VerificationConfirmation.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Test12VerificationConfirmation {
2222

2323

2424
/**
25-
* ConfirmVerified check when, and then sections of the test.
25+
* ConfirmVerified check WHEN, and THEN sections of the test.
2626
* Not mocking with same params we used in verify does not have to be same.
2727
*
2828
*/
@@ -35,11 +35,11 @@ class Test12VerificationConfirmation {
3535
every { car.drive(Direction.NORTH) } returns Outcome.OK
3636
every { car.drive(Direction.SOUTH) } returns Outcome.OK
3737

38-
// When
38+
// WHEN
3939
car.drive(Direction.NORTH) // returns OK
4040
car.drive(Direction.SOUTH) // returns OK
4141

42-
// Then
42+
// THEN
4343
verify {
4444
car.drive(Direction.SOUTH)
4545
car.drive(Direction.NORTH)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.smarttoolfactory.tutorial4_2kotlin_mockk
2+
3+
import com.google.common.truth.Truth
4+
import kotlinx.coroutines.GlobalScope
5+
import kotlinx.coroutines.async
6+
import kotlinx.coroutines.delay
7+
import kotlinx.coroutines.runBlocking
8+
import org.junit.jupiter.api.Test
9+
10+
class Test16Coroutines {
11+
12+
@Test
13+
fun `First Coroutine test`()= runBlocking {
14+
15+
// GIVEN
16+
val expected = "Hello World"
17+
18+
// WHEN
19+
val actual = GlobalScope.async {
20+
mockSomeDelayedWork()
21+
}
22+
23+
// THEN
24+
Truth.assertThat(actual.await()).isEqualTo(expected)
25+
}
26+
27+
28+
}
29+
30+
31+
private suspend fun mockSomeDelayedWork(): String {
32+
33+
delay(10_000)
34+
return "Hello World"
35+
36+
}
37+

0 commit comments

Comments
 (0)