Skip to content

Commit b19c873

Browse files
Rename JUnit Rule for Coroutines
1 parent 3f2873b commit b19c873

File tree

10 files changed

+70
-51
lines changed

10 files changed

+70
-51
lines changed

.idea/codeStyles/Project.xml

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

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase1/PerformSingleNetworkRequestViewModelTest.kt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase1
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44
import com.lukaslechner.coroutineusecasesonandroid.mock.mockAndroidVersions
5-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
5+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
66
import kotlinx.coroutines.ExperimentalCoroutinesApi
77
import kotlinx.coroutines.test.runBlockingTest
88
import org.junit.Assert.assertEquals
@@ -18,13 +18,13 @@ class PerformSingleNetworkRequestViewModelTest {
1818
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
1919

2020
@get: Rule
21-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
21+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
2222

2323
private val receivedUiStates = mutableListOf<UiState>()
2424

2525
@Test
2626
fun `should return Success when network request is successful`() =
27-
coroutineTestRule.runBlockingTest {
27+
mainCoroutineScopeRule.runBlockingTest {
2828
val fakeApi = FakeSuccessApi()
2929
val viewModel =
3030
PerformSingleNetworkRequestViewModel(fakeApi)
@@ -44,16 +44,17 @@ class PerformSingleNetworkRequestViewModelTest {
4444
}
4545

4646
@Test
47-
fun `should return Error when network request fails`() = coroutineTestRule.runBlockingTest {
48-
val fakeApi = FakeErrorApi()
47+
fun `should return Error when network request fails`() =
48+
mainCoroutineScopeRule.runBlockingTest {
49+
val fakeApi = FakeErrorApi()
4950

50-
val viewModel =
51-
PerformSingleNetworkRequestViewModel(fakeApi)
52-
observeViewModel(viewModel)
51+
val viewModel =
52+
PerformSingleNetworkRequestViewModel(fakeApi)
53+
observeViewModel(viewModel)
5354

54-
assertTrue(receivedUiStates.isEmpty())
55+
assertTrue(receivedUiStates.isEmpty())
5556

56-
viewModel.performSingleNetworkRequest()
57+
viewModel.performSingleNetworkRequest()
5758

5859
assertEquals(
5960
listOf(

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase10/CalculationInBackgroundViewModelTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase10
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
4+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
55
import kotlinx.coroutines.test.runBlockingTest
66
import org.junit.Assert
77
import org.junit.Rule
@@ -14,15 +14,15 @@ class CalculationInBackgroundViewModelTest {
1414
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
1515

1616
@get: Rule
17-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
17+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
1818

1919
private val receivedUiStates = mutableListOf<UiState>()
2020

2121
@Test
2222
fun `performCalculation() should perform correct calculations`() =
23-
coroutineTestRule.runBlockingTest {
23+
mainCoroutineScopeRule.runBlockingTest {
2424
val viewModel =
25-
CalculationInBackgroundViewModel(coroutineTestRule.testDispatcher).apply {
25+
CalculationInBackgroundViewModel(mainCoroutineScopeRule.testDispatcher).apply {
2626
observe()
2727
}
2828

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase14/AndroidVersionRepositoryTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import com.lukaslechner.coroutineusecasesonandroid.mock.AndroidVersion
44
import com.lukaslechner.coroutineusecasesonandroid.mock.MockApi
55
import com.lukaslechner.coroutineusecasesonandroid.mock.VersionFeatures
66
import com.lukaslechner.coroutineusecasesonandroid.mock.mockAndroidVersions
7-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
87
import com.lukaslechner.coroutineusecasesonandroid.utils.EndpointShouldNotBeCalledException
8+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
99
import junit.framework.Assert.assertEquals
1010
import junit.framework.Assert.fail
1111
import kotlinx.coroutines.*
@@ -19,7 +19,7 @@ import org.junit.Test
1919
class AndroidVersionRepositoryTest {
2020

2121
@get: Rule
22-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
22+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
2323

2424
private var insertedIntoDb = false
2525

@@ -30,34 +30,34 @@ class AndroidVersionRepositoryTest {
3030

3131
@Test
3232
fun `getLocalAndroidVersions() should return android versions from database`() =
33-
coroutineTestRule.runBlockingTest {
33+
mainCoroutineScopeRule.runBlockingTest {
3434
val fakeDatabase = createFakeDatabase()
3535

36-
val repository = AndroidVersionRepository(fakeDatabase, coroutineTestRule)
36+
val repository = AndroidVersionRepository(fakeDatabase, mainCoroutineScopeRule)
3737
assertEquals(mockAndroidVersions, repository.getLocalAndroidVersions())
3838
}
3939

4040
@Test
4141
fun `loadRecentAndroidVersions() should return android versions from network`() =
42-
coroutineTestRule.runBlockingTest {
42+
mainCoroutineScopeRule.runBlockingTest {
4343
val fakeDatabase = createFakeDatabase()
4444
val fakeApi = createFakeApi()
4545
val repository = AndroidVersionRepository(
4646
fakeDatabase,
47-
coroutineTestRule,
47+
mainCoroutineScopeRule,
4848
api = fakeApi
4949
)
5050
assertEquals(mockAndroidVersions, repository.loadAndStoreRemoteAndroidVersions())
5151
}
5252

5353
@Test
5454
fun `loadRecentAndroidVersions() should continue to load and store android versions when calling scope gets cancelled`() =
55-
coroutineTestRule.runBlockingTest {
55+
mainCoroutineScopeRule.runBlockingTest {
5656
val fakeDatabase = createFakeDatabase()
5757
val fakeApi = createFakeApi()
5858
val repository = AndroidVersionRepository(
5959
fakeDatabase,
60-
coroutineTestRule,
60+
mainCoroutineScopeRule,
6161
api = fakeApi
6262
)
6363

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase2/Perform2SequentialNetworkRequestsViewModelTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase2
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesAndroid10
5-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
5+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
66
import kotlinx.coroutines.ExperimentalCoroutinesApi
77
import kotlinx.coroutines.test.runBlockingTest
88
import org.junit.Assert
@@ -17,13 +17,13 @@ class Perform2SequentialNetworkRequestsViewModelTest {
1717
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
1818

1919
@get: Rule
20-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
20+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
2121

2222
private val receivedUiStates = mutableListOf<UiState>()
2323

2424
@Test
2525
fun `should return Success when both network requests are successful`() =
26-
coroutineTestRule.runBlockingTest {
26+
mainCoroutineScopeRule.runBlockingTest {
2727
val fakeApi = FakeSuccessApi()
2828
val viewModel =
2929
Perform2SequentialNetworkRequestsViewModel(
@@ -46,7 +46,7 @@ class Perform2SequentialNetworkRequestsViewModelTest {
4646

4747
@Test
4848
fun `should return Error when first network requests fails`() =
49-
coroutineTestRule.runBlockingTest {
49+
mainCoroutineScopeRule.runBlockingTest {
5050

5151
val fakeApi = FakeVersionsErrorApi()
5252
val viewModel =
@@ -70,7 +70,7 @@ class Perform2SequentialNetworkRequestsViewModelTest {
7070

7171
@Test
7272
fun `should return Error when second network requests fails`() =
73-
coroutineTestRule.runBlockingTest {
73+
mainCoroutineScopeRule.runBlockingTest {
7474

7575
val fakeApi = FakeFeaturesErrorApi()
7676
val viewModel =

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase3/PerformNetworkRequestsConcurrentlyViewModelTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesAndroid10
55
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesOreo
66
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesPie
7-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
7+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
88
import kotlinx.coroutines.ExperimentalCoroutinesApi
99
import kotlinx.coroutines.test.runBlockingTest
1010
import org.junit.Assert
@@ -19,13 +19,13 @@ class PerformNetworkRequestsConcurrentlyViewModelTest {
1919
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
2020

2121
@get: Rule
22-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
22+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
2323

2424
private val receivedUiStates = mutableListOf<UiState>()
2525

2626
@Test
2727
fun `performNetworkRequestsSequentially should return data after 3 times the response delay`() =
28-
coroutineTestRule.runBlockingTest {
28+
mainCoroutineScopeRule.runBlockingTest {
2929
val responseDelay = 1000L
3030
val fakeApi = FakeSuccessApi(responseDelay)
3131
val viewModel = PerformNetworkRequestsConcurrentlyViewModel(fakeApi)
@@ -61,7 +61,7 @@ class PerformNetworkRequestsConcurrentlyViewModelTest {
6161

6262
@Test
6363
fun `performNetworkRequestsConcurrently should return data after the response delay`() =
64-
coroutineTestRule.runBlockingTest {
64+
mainCoroutineScopeRule.runBlockingTest {
6565
val responseDelay = 1000L
6666
val fakeApi = FakeSuccessApi(responseDelay)
6767
val viewModel = PerformNetworkRequestsConcurrentlyViewModel(fakeApi)
@@ -96,7 +96,7 @@ class PerformNetworkRequestsConcurrentlyViewModelTest {
9696

9797
@Test
9898
fun `performNetworkRequestsConcurrently should return Error when network request fails`() =
99-
coroutineTestRule.runBlockingTest {
99+
mainCoroutineScopeRule.runBlockingTest {
100100
val responseDelay = 1000L
101101
val fakeApi = FakeErrorApi(responseDelay)
102102
val viewModel = PerformNetworkRequestsConcurrentlyViewModel(fakeApi)

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase4/VariableAmountOfNetworkRequestsViewModelTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesAndroid10
55
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesOreo
66
import com.lukaslechner.coroutineusecasesonandroid.mock.mockVersionFeaturesPie
7-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
7+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
88
import kotlinx.coroutines.ExperimentalCoroutinesApi
99
import kotlinx.coroutines.test.runBlockingTest
1010
import org.junit.Assert
@@ -19,13 +19,13 @@ class VariableAmountOfNetworkRequestsViewModelTest {
1919
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
2020

2121
@get: Rule
22-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
22+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
2323

2424
private val receivedUiStates = mutableListOf<UiState>()
2525

2626
@Test
2727
fun `performNetworkRequestsSequentially() should return Success UiState on successful network requests after 4000ms`() =
28-
coroutineTestRule.runBlockingTest {
28+
mainCoroutineScopeRule.runBlockingTest {
2929
val responseDelay = 1000L
3030
val fakeApi = FakeSuccessApi(responseDelay)
3131
val viewModel = VariableAmountOfNetworkRequestsViewModel(fakeApi)
@@ -59,7 +59,7 @@ class VariableAmountOfNetworkRequestsViewModelTest {
5959

6060
@Test
6161
fun `performNetworkRequestsSequentially() should return Error UiState on unsuccessful recent-android-versions network request`() =
62-
coroutineTestRule.runBlockingTest {
62+
mainCoroutineScopeRule.runBlockingTest {
6363
val responseDelay = 1000L
6464
val fakeApi = FakeVersionsErrorApi(responseDelay)
6565
val viewModel = VariableAmountOfNetworkRequestsViewModel(fakeApi)
@@ -82,7 +82,7 @@ class VariableAmountOfNetworkRequestsViewModelTest {
8282

8383
@Test
8484
fun `performNetworkRequestsSequentially() should return Error UiState on unsuccessful android-version-features network request`() =
85-
coroutineTestRule.runBlockingTest {
85+
mainCoroutineScopeRule.runBlockingTest {
8686
val responseDelay = 1000L
8787
val fakeApi = FakeFeaturesErrorApi(responseDelay)
8888
val viewModel = VariableAmountOfNetworkRequestsViewModel(fakeApi)
@@ -105,7 +105,7 @@ class VariableAmountOfNetworkRequestsViewModelTest {
105105

106106
@Test
107107
fun `performNetworkRequestsConcurrently() should return Error UiState on successful network requests after 2000ms`() =
108-
coroutineTestRule.runBlockingTest {
108+
mainCoroutineScopeRule.runBlockingTest {
109109
val responseDelay = 1000L
110110
val fakeApi = FakeSuccessApi(responseDelay)
111111
val viewModel = VariableAmountOfNetworkRequestsViewModel(fakeApi)
@@ -139,7 +139,7 @@ class VariableAmountOfNetworkRequestsViewModelTest {
139139

140140
@Test
141141
fun `performNetworkRequestsConcurrently() should return Error UiState on unsuccessful recent-android-versions network request`() =
142-
coroutineTestRule.runBlockingTest {
142+
mainCoroutineScopeRule.runBlockingTest {
143143
val responseDelay = 1000L
144144
val fakeApi = FakeVersionsErrorApi(responseDelay)
145145
val viewModel = VariableAmountOfNetworkRequestsViewModel(fakeApi)
@@ -162,7 +162,7 @@ class VariableAmountOfNetworkRequestsViewModelTest {
162162

163163
@Test
164164
fun `performNetworkRequestsConcurrently() should return Error UiState on unsuccessful android-version-features network request`() =
165-
coroutineTestRule.runBlockingTest {
165+
mainCoroutineScopeRule.runBlockingTest {
166166
val responseDelay = 1000L
167167
val fakeApi = FakeFeaturesErrorApi(responseDelay)
168168
val viewModel = VariableAmountOfNetworkRequestsViewModel(fakeApi)

app/src/test/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase5/NetworkRequestWithTimeoutViewModelTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase5
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
44
import com.lukaslechner.coroutineusecasesonandroid.mock.mockAndroidVersions
5-
import com.lukaslechner.coroutineusecasesonandroid.utils.CoroutineTestRule
5+
import com.lukaslechner.coroutineusecasesonandroid.utils.MainCoroutineScopeRule
66
import kotlinx.coroutines.ExperimentalCoroutinesApi
77
import kotlinx.coroutines.test.runBlockingTest
88
import org.junit.Assert
@@ -17,13 +17,13 @@ class NetworkRequestWithTimeoutViewModelTest {
1717
val testInstantTaskExecutorRule: TestRule = InstantTaskExecutorRule()
1818

1919
@get: Rule
20-
val coroutineTestRule: CoroutineTestRule = CoroutineTestRule()
20+
val mainCoroutineScopeRule: MainCoroutineScopeRule = MainCoroutineScopeRule()
2121

2222
private val receivedUiStates = mutableListOf<UiState>()
2323

2424
@Test
2525
fun `performNetworkRequest() should return Success UiState on successful network request within timeout`() =
26-
coroutineTestRule.runBlockingTest {
26+
mainCoroutineScopeRule.runBlockingTest {
2727
val responseDelay = 1000L
2828
val timeout = 1001L
2929
val fakeApi = FakeSuccessApi(responseDelay)
@@ -47,7 +47,7 @@ class NetworkRequestWithTimeoutViewModelTest {
4747

4848
@Test
4949
fun `performNetworkRequest() should return Error UiState with timeout error message if timeout gets exceeded`() =
50-
coroutineTestRule.runBlockingTest {
50+
mainCoroutineScopeRule.runBlockingTest {
5151
val responseDelay = 1000L
5252
val timeout = 999L
5353
val fakeApi = FakeSuccessApi(responseDelay)
@@ -71,7 +71,7 @@ class NetworkRequestWithTimeoutViewModelTest {
7171

7272
@Test
7373
fun `performNetworkRequest() should return Error UiState on unsuccessful network response`() =
74-
coroutineTestRule.runBlockingTest {
74+
mainCoroutineScopeRule.runBlockingTest {
7575
val responseDelay = 1000L
7676
val timeout = 1001L
7777
val fakeApi = FakeVersionsErrorApi(responseDelay)

0 commit comments

Comments
 (0)