@@ -3,14 +3,19 @@ package com.karumi.kataloginlogoutkotlin
3
3
import arrow.core.Either
4
4
import arrow.core.left
5
5
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
9
13
import org.junit.Before
10
14
import org.junit.Test
11
15
import org.junit.runner.RunWith
12
16
import org.mockito.Mock
13
17
import org.mockito.junit.MockitoJUnitRunner
18
+ import java.util.concurrent.Executors
14
19
15
20
@RunWith(MockitoJUnitRunner ::class )
16
21
class PresenterTest {
@@ -29,61 +34,62 @@ class PresenterTest {
29
34
30
35
@Before
31
36
fun setUp () {
37
+ Dispatchers .setMain(Executors .newSingleThreadExecutor().asCoroutineDispatcher())
32
38
presenter = Presenter (kata, view)
33
39
}
34
40
35
41
@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 {
37
43
givenTheLogInProcessReturns(InvalidCredentials .left())
38
44
39
- presenter.onLogInButtonTap(ANY_USERNAME , ANY_PASS )
45
+ presenter.onLogInButtonTap(ANY_USERNAME , ANY_PASS ).join()
40
46
41
47
verify(view).showError(R .string.log_in_error_message)
42
48
}
43
49
44
50
@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 {
46
52
givenTheLogInProcessReturns(InvalidUsername .left())
47
53
48
- presenter.onLogInButtonTap(ANY_USERNAME , ANY_PASS )
54
+ presenter.onLogInButtonTap(ANY_USERNAME , ANY_PASS ).join()
49
55
50
56
verify(view).showError(R .string.invalid_username_error_message)
51
57
}
52
58
53
59
@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 {
55
61
givenTheLogOutProcessReturns(false )
56
62
57
- presenter.onLogOutButtonTap()
63
+ presenter.onLogOutButtonTap().join()
58
64
59
65
verify(view).showError(R .string.log_out_error_message)
60
66
}
61
67
62
68
@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())
65
71
66
- presenter.onLogInButtonTap(ANY_USERNAME , ANY_PASS )
72
+ presenter.onLogInButtonTap(ANY_USERNAME , ANY_PASS ).join( )
67
73
68
- verify(view).hideLogInForm()
69
- verify(view).showLogOutForm()
70
- }
74
+ verify(view).hideLogInForm()
75
+ verify(view).showLogOutForm()
76
+ }
71
77
72
78
@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 )
75
81
76
- presenter.onLogOutButtonTap()
82
+ presenter.onLogOutButtonTap().join ()
77
83
78
- verify(view).hideLogOutForm()
79
- verify(view).showLogInForm()
80
- }
84
+ verify(view).hideLogOutForm()
85
+ verify(view).showLogInForm()
86
+ }
81
87
82
88
private fun givenTheLogOutProcessReturns (result : Boolean ) {
83
- whenever(kata.logOut()).thenReturn(result)
89
+ runBlocking { whenever(kata.logOut()).thenReturn(result) }
84
90
}
85
91
86
92
private fun givenTheLogInProcessReturns (result : Either <LogInError , String >) {
87
- whenever(kata.logIn(any(), any())).thenReturn(result)
93
+ runBlocking { whenever(kata.logIn(any(), any())).thenReturn(result) }
88
94
}
89
95
}
0 commit comments