Skip to content

Commit 307c46b

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 307c46b

File tree

1 file changed

+15
-0
lines changed
  • WooCommerce/src/test/kotlin/com/woocommerce/android/viewmodel

1 file changed

+15
-0
lines changed

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

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ 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+
}
1833
@Rule @JvmField
1934
val rule = InstantTaskExecutorRule()
2035

0 commit comments

Comments
 (0)