Skip to content

Commit 716c2f3

Browse files
committed
Restore behavior of tests passing despite exceptions thrown
This is a hacky workaround for the change in kotlinx.coroutines 1.7.0 that causes tests that throw exceptions to fail. Previously test methods that threw exceptions would not prevent tests from passing, which was a bug in kotlinx.coroutines that has now been fixed. However, significant number of our tests are currently failing because of this change, because we have bugs in the implementation of our test classes, usually related to the way we instantiate the view models in tests. See the following issue for more details: Kotlin/kotlinx.coroutines#1205. The workaround coming with this commit is taken from the related PR: Kotlin/kotlinx.coroutines#3736 and is a solution suggested by JetBrains to disable the new behavior using non-public API until we fix our tests. This should not be considered a long-term solution, rather a temporary hack.
1 parent 43836f6 commit 716c2f3

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

WooCommerce/src/test/kotlin/com/woocommerce/android/viewmodel/BaseUnitTest.kt

+16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ import org.mockito.junit.MockitoJUnitRunner
1515
@Suppress("UnnecessaryAbstractClass")
1616
@RunWith(MockitoJUnitRunner::class)
1717
abstract class BaseUnitTest(testDispatcher: TestDispatcher = UnconfinedTestDispatcher()) {
18+
19+
init {
20+
// This is a workaround for the change in kotlinx.coroutines 1.7.0 that causes tests that
21+
// throw exceptions to fail. Previously test methods that threw exceptions would not prevent
22+
// tests from passing, which was a bug in kotlinx.coroutines that has now been fixed. However,
23+
// significant number of our tests are currently failing because of this change.
24+
//
25+
// See the following issue for more details: https://github.com/Kotlin/kotlinx.coroutines/issues/1205.
26+
// The workaround below is taken from the related PR: https://github.com/Kotlin/kotlinx.coroutines/pull/3736
27+
// and is a solution suggested by JetBrains to disable the new behavior using non-public API
28+
// until we fix our tests. This should not be considered a long-term solution, rather a temporary hack.
29+
Class.forName("kotlinx.coroutines.test.TestScopeKt")
30+
.getDeclaredMethod("setCatchNonTestRelatedExceptions", Boolean::class.java)
31+
.invoke(null, false)
32+
}
33+
1834
@Rule @JvmField
1935
val rule = InstantTaskExecutorRule()
2036

libs/cardreader/src/test/java/com/woocommerce/android/cardreader/internal/connection/ConnectionManagerTest.kt

+15
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ import org.mockito.kotlin.whenever
3434

3535
@ExperimentalCoroutinesApi
3636
class ConnectionManagerTest : CardReaderBaseUnitTest() {
37+
init {
38+
// This is a workaround for the change in kotlinx.coroutines 1.7.0 that causes tests that
39+
// throw exceptions to fail. Previously test methods that threw exceptions would not prevent
40+
// tests from passing, which was a bug in kotlinx.coroutines that has now been fixed. However,
41+
// significant number of our tests are currently failing because of this change.
42+
//
43+
// See the following issue for more details: https://github.com/Kotlin/kotlinx.coroutines/issues/1205.
44+
// The workaround below is taken from the related PR: https://github.com/Kotlin/kotlinx.coroutines/pull/3736
45+
// and is a solution suggested by JetBrains to disable the new behavior using non-public API
46+
// until we fix our tests. This should not be considered a long-term solution, rather a temporary hack.
47+
Class.forName("kotlinx.coroutines.test.TestScopeKt")
48+
.getDeclaredMethod("setCatchNonTestRelatedExceptions", Boolean::class.java)
49+
.invoke(null, false)
50+
}
51+
3752
private val terminalWrapper: TerminalWrapper = mock()
3853
private val bluetoothReaderListener: BluetoothReaderListenerImpl = mock()
3954
private val discoverReadersAction: DiscoverReadersAction = mock()

0 commit comments

Comments
 (0)