Skip to content

Commit 33a4051

Browse files
committed
Update mockito and mockito-kotlin dependencies and fix all tests
1 parent da6077d commit 33a4051

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

app/build.gradle

+3-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ dependencies {
4747
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0"
4848
implementation "joda-time:joda-time:2.10"
4949
testImplementation "junit:junit:4.12"
50-
testImplementation "org.mockito:mockito-core:2.23.0"
51-
testImplementation "com.nhaarman:mockito-kotlin:1.6.0"
50+
testImplementation "org.mockito:mockito-core:2.23.4"
51+
testImplementation "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
52+
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.1.0"
5253
androidTestImplementation "com.android.support.test:runner:1.0.2"
5354
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.2"
5455
}

app/src/test/java/com/karumi/kataloginlogoutkotlin/LogInLogOutKataTest.kt

+23-19
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ package com.karumi.kataloginlogoutkotlin
22

33
import arrow.core.left
44
import arrow.core.right
5-
import com.nhaarman.mockito_kotlin.whenever
5+
import com.nhaarman.mockitokotlin2.whenever
66
import junit.framework.TestCase.assertEquals
7-
import junit.framework.TestCase.assertTrue
87
import junit.framework.TestCase.assertFalse
8+
import junit.framework.TestCase.assertTrue
9+
import kotlinx.coroutines.runBlocking
910
import org.joda.time.DateTime
1011
import org.junit.Before
1112
import org.junit.Test
@@ -34,49 +35,52 @@ class LogInLogOutKataTest {
3435
}
3536

3637
@Test
37-
fun `should return an invalid username error if the username contains the first invalid char char chars`() {
38-
val logInResult = logInLogOutKata.logIn("ad,min", ANY_INVALID_PASSWORD)
38+
fun `should return an invalid username error if the username contains the first invalid char char chars`() =
39+
runBlocking {
40+
val logInResult = logInLogOutKata.logIn("ad,min", ANY_INVALID_PASSWORD)
3941

40-
assertEquals(InvalidUsername.left(), logInResult)
41-
}
42+
assertEquals(InvalidUsername.left(), logInResult)
43+
}
4244

4345
@Test
44-
fun `should return an invalid username error if the username contains the second invalid char char chars`() {
45-
val logInResult = logInLogOutKata.logIn("ad.min", ANY_INVALID_PASSWORD)
46+
fun `should return an invalid username error if the username contains the second invalid char char chars`() =
47+
runBlocking {
48+
val logInResult = logInLogOutKata.logIn("ad.min", ANY_INVALID_PASSWORD)
4649

47-
assertEquals(InvalidUsername.left(), logInResult)
48-
}
50+
assertEquals(InvalidUsername.left(), logInResult)
51+
}
4952

5053
@Test
51-
fun `should return an invalid username error if the username contains the third invalid char char chars`() {
52-
val logInResult = logInLogOutKata.logIn("ad;min", ANY_INVALID_PASSWORD)
54+
fun `should return an invalid username error if the username contains the third invalid char char chars`() =
55+
runBlocking {
56+
val logInResult = logInLogOutKata.logIn("ad;min", ANY_INVALID_PASSWORD)
5357

54-
assertEquals(InvalidUsername.left(), logInResult)
55-
}
58+
assertEquals(InvalidUsername.left(), logInResult)
59+
}
5660

5761
@Test
58-
fun `should return an invalid credentials error if the username is not correct`() {
62+
fun `should return an invalid credentials error if the username is not correct`() = runBlocking {
5963
val logInResult = logInLogOutKata.logIn(ANY_INVALID_USERNAME, ANY_VALID_PASSWORD)
6064

6165
assertEquals(InvalidCredentials.left(), logInResult)
6266
}
6367

6468
@Test
65-
fun `should return an invalid credentials error if the password is not correct`() {
69+
fun `should return an invalid credentials error if the password is not correct`() = runBlocking {
6670
val logInResult = logInLogOutKata.logIn(ANY_VALID_USERNAME, ANY_INVALID_PASSWORD)
6771

6872
assertEquals(InvalidCredentials.left(), logInResult)
6973
}
7074

7175
@Test
72-
fun `should return the user username and a success result if the user and password are corrects`() {
76+
fun `should return the user username and a success result if the user and password are corrects`() = runBlocking {
7377
val logInResult = logInLogOutKata.logIn(ANY_VALID_USERNAME, ANY_VALID_PASSWORD)
7478

7579
assertEquals(ANY_VALID_USERNAME.right(), logInResult)
7680
}
7781

7882
@Test
79-
fun `should return an error if the second when the log out is performed is odd`() {
83+
fun `should return an error if the second when the log out is performed is odd`() = runBlocking {
8084
givenNowIs(DateTime(3))
8185

8286
val logOutResult = logInLogOutKata.logOut()
@@ -85,7 +89,7 @@ class LogInLogOutKataTest {
8589
}
8690

8791
@Test
88-
fun `should return success if the second when the log out is performed is even`() {
92+
fun `should return success if the second when the log out is performed is even`() = runBlocking {
8993
givenNowIs(DateTime(2))
9094

9195
val logOutResult = logInLogOutKata.logOut()

app/src/test/java/com/karumi/kataloginlogoutkotlin/PresenterTest.kt

+29-23
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@ package com.karumi.kataloginlogoutkotlin
33
import arrow.core.Either
44
import arrow.core.left
55
import arrow.core.right
6-
import com.nhaarman.mockito_kotlin.any
7-
import com.nhaarman.mockito_kotlin.verify
8-
import com.nhaarman.mockito_kotlin.whenever
6+
import com.nhaarman.mockitokotlin2.any
7+
import com.nhaarman.mockitokotlin2.verify
8+
import com.nhaarman.mockitokotlin2.whenever
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.asCoroutineDispatcher
11+
import kotlinx.coroutines.runBlocking
12+
import kotlinx.coroutines.test.setMain
913
import org.junit.Before
1014
import org.junit.Test
1115
import org.junit.runner.RunWith
1216
import org.mockito.Mock
1317
import org.mockito.junit.MockitoJUnitRunner
18+
import java.util.concurrent.Executors
1419

1520
@RunWith(MockitoJUnitRunner::class)
1621
class PresenterTest {
@@ -29,61 +34,62 @@ class PresenterTest {
2934

3035
@Before
3136
fun setUp() {
37+
Dispatchers.setMain(Executors.newSingleThreadExecutor().asCoroutineDispatcher())
3238
presenter = Presenter(kata, view)
3339
}
3440

3541
@Test
36-
fun `should show an invalid credentials error if the log in process returns InvalidCredentials`() {
42+
fun `should show an invalid credentials error if the log in process returns InvalidCredentials`() = runBlocking {
3743
givenTheLogInProcessReturns(InvalidCredentials.left())
3844

39-
presenter.onLogInButtonTap(ANY_USERNAME, ANY_PASS)
45+
presenter.onLogInButtonTap(ANY_USERNAME, ANY_PASS).join()
4046

4147
verify(view).showError(R.string.log_in_error_message)
4248
}
4349

4450
@Test
45-
fun `should show an invalid username error if the log in process returns InvalidUsername`() {
51+
fun `should show an invalid username error if the log in process returns InvalidUsername`() = runBlocking {
4652
givenTheLogInProcessReturns(InvalidUsername.left())
4753

48-
presenter.onLogInButtonTap(ANY_USERNAME, ANY_PASS)
54+
presenter.onLogInButtonTap(ANY_USERNAME, ANY_PASS).join()
4955

5056
verify(view).showError(R.string.invalid_username_error_message)
5157
}
5258

5359
@Test
54-
fun `should show a could not perform log out error if the log out process fails`() {
60+
fun `should show a could not perform log out error if the log out process fails`() = runBlocking {
5561
givenTheLogOutProcessReturns(false)
5662

57-
presenter.onLogOutButtonTap()
63+
presenter.onLogOutButtonTap().join()
5864

5965
verify(view).showError(R.string.log_out_error_message)
6066
}
6167

6268
@Test
63-
fun `should hide the log in form and show the log out form if the log in process finished properly`() {
64-
givenTheLogInProcessReturns(ANY_USERNAME.right())
69+
fun `should hide the log in form and show the log out form if the log in process finished properly`() = runBlocking {
70+
givenTheLogInProcessReturns(ANY_USERNAME.right())
6571

66-
presenter.onLogInButtonTap(ANY_USERNAME, ANY_PASS)
72+
presenter.onLogInButtonTap(ANY_USERNAME, ANY_PASS).join()
6773

68-
verify(view).hideLogInForm()
69-
verify(view).showLogOutForm()
70-
}
74+
verify(view).hideLogInForm()
75+
verify(view).showLogOutForm()
76+
}
7177

7278
@Test
73-
fun `should hide the log out form and show the log in form if the log out process finished properly`() {
74-
givenTheLogOutProcessReturns(true)
79+
fun `should hide the log out form and show the log in form if the log out process finished properly`() = runBlocking {
80+
givenTheLogOutProcessReturns(true)
7581

76-
presenter.onLogOutButtonTap()
82+
presenter.onLogOutButtonTap().join()
7783

78-
verify(view).hideLogOutForm()
79-
verify(view).showLogInForm()
80-
}
84+
verify(view).hideLogOutForm()
85+
verify(view).showLogInForm()
86+
}
8187

8288
private fun givenTheLogOutProcessReturns(result: Boolean) {
83-
whenever(kata.logOut()).thenReturn(result)
89+
runBlocking { whenever(kata.logOut()).thenReturn(result) }
8490
}
8591

8692
private fun givenTheLogInProcessReturns(result: Either<LogInError, String>) {
87-
whenever(kata.logIn(any(), any())).thenReturn(result)
93+
runBlocking { whenever(kata.logIn(any(), any())).thenReturn(result) }
8894
}
8995
}

0 commit comments

Comments
 (0)