Error calling Dispatchers.setMain() in unit test

John O'Reilly

Have started to try to use kotlinx-coroutines-test (https://github.com/Kotlin/kotlinx.coroutines/blob/master/core/kotlinx-coroutines-test/README.md) in JUnit unit test but getting following error when i call Dispatchers.setMain()

java.lang.IllegalArgumentException: TestMainDispatcher is not set as main dispatcher, have Main[missing, cause=java.lang.AbstractMethodError: kotlinx.coroutines.test.internal.TestMainDispatcherFactory.createDispatcher()Lkotlinx/coroutines/MainCoroutineDispatcher;] instead.

    at kotlinx.coroutines.test.TestDispatchers.setMain(TestDispatchers.kt:22)

I've tried calling Dispatchers.setMain(Dispatchers.Unconfined) and also passing in val mainThreadSurrogate = newSingleThreadContext("UI thread"). It looks like issue isn't anyway with value being passed in but rather it's tripping up in mainDispatcher test in following

public fun Dispatchers.setMain(dispatcher: CoroutineDispatcher) {
    require(dispatcher !is TestMainDispatcher) { "Dispatchers.setMain(Dispatchers.Main) is prohibited, probably Dispatchers.resetMain() should be used instead" }
    val mainDispatcher = Dispatchers.Main
    require(mainDispatcher is TestMainDispatcher) { "TestMainDispatcher is not set as main dispatcher, have $mainDispatcher instead." }
    mainDispatcher.setDispatcher(dispatcher)
}
John O'Reilly

Turned out issue was I was using older version of kotlinx-coroutines-core dependency. When I updated to v1.1.0 it worked (thanks @vigit for helping trigger that realisation!)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

OCMock unit test error

From Dev

Moq Unit test fails when calling a Service

From Dev

ngAnimate 1.4.7 unit test not calling animation functions

From Dev

Moq Unit test fails when calling a Service

From Dev

Laravel Unit Test calling route with authentication

From Dev

AngularJS Unit Test Error dependence

From Dev

VSTS Unit test error message

From Dev

Error in test method in mvc for routing unit test

From Dev

Only unit test methods not get displayed on list box, while calling unit test methods

From Dev

calling selectRow on ng-grid in a jasmine unit test

From Dev

Laravel unit testing calling a Route from a Test with authentication

From Dev

How to unit test and check if a function is calling the expected firebase method in Jest?

From Dev

Calling unit/functional test assertions after change events in Polymer

From Dev

How Can I Unit Test Calling of iOS Application Delegate Methods?

From Dev

Mocha Unit Test Error Handling from Library

From Dev

Suppress supplementary error message from unit test

From Dev

Angular Unit Test: Error: Unexpected request: GET

From Dev

Karma unit test 'undefined' is not a function error

From Java

MatDialog Service Unit Test Angular 6 Error

From Dev

karma unit test runner directory injector error

From Dev

Force a unit test case to ERROR in Python

From Dev

Catch unit test library linking error

From Dev

Controller Unit Test fails with SQLSTATE[42000] error

From Dev

unable to get error from SeeJson unit test

From Dev

Module not found error in AngularJS unit test

From Dev

Angular Unit Test Jasmine Spy error

From Dev

Boost unit test link error -- abi mismatch?

From Dev

Force a yielded function to error for unit test

From Dev

Linking error in unit test using URLRequestConvertible

Related Related

  1. 1

    OCMock unit test error

  2. 2

    Moq Unit test fails when calling a Service

  3. 3

    ngAnimate 1.4.7 unit test not calling animation functions

  4. 4

    Moq Unit test fails when calling a Service

  5. 5

    Laravel Unit Test calling route with authentication

  6. 6

    AngularJS Unit Test Error dependence

  7. 7

    VSTS Unit test error message

  8. 8

    Error in test method in mvc for routing unit test

  9. 9

    Only unit test methods not get displayed on list box, while calling unit test methods

  10. 10

    calling selectRow on ng-grid in a jasmine unit test

  11. 11

    Laravel unit testing calling a Route from a Test with authentication

  12. 12

    How to unit test and check if a function is calling the expected firebase method in Jest?

  13. 13

    Calling unit/functional test assertions after change events in Polymer

  14. 14

    How Can I Unit Test Calling of iOS Application Delegate Methods?

  15. 15

    Mocha Unit Test Error Handling from Library

  16. 16

    Suppress supplementary error message from unit test

  17. 17

    Angular Unit Test: Error: Unexpected request: GET

  18. 18

    Karma unit test 'undefined' is not a function error

  19. 19

    MatDialog Service Unit Test Angular 6 Error

  20. 20

    karma unit test runner directory injector error

  21. 21

    Force a unit test case to ERROR in Python

  22. 22

    Catch unit test library linking error

  23. 23

    Controller Unit Test fails with SQLSTATE[42000] error

  24. 24

    unable to get error from SeeJson unit test

  25. 25

    Module not found error in AngularJS unit test

  26. 26

    Angular Unit Test Jasmine Spy error

  27. 27

    Boost unit test link error -- abi mismatch?

  28. 28

    Force a yielded function to error for unit test

  29. 29

    Linking error in unit test using URLRequestConvertible

HotTag

Archive