Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

InjectMockKs with MutableLiveData dependency throws ClassCastException #388

Open
3 tasks done
vmadalin opened this issue Nov 22, 2019 · 1 comment
Open
3 tasks done

Comments

@vmadalin
Copy link

vmadalin commented Nov 22, 2019

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

The test should pass.

Current Behavior

Obtain mutableLiveData value throws a java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String

Context

If you try to InjectMockKs with MutableLiveData dependency is impossible to obtain the mock value in your ClassTest. This behaviour is used especially when you use DataSource.Factory you can check the class here. Moreover the same sample with Mockito works correctly.

  • MockK version: 1.9.3
  • OS: OS X
  • Kotlin version: 1.3.50
  • JDK version: 11
  • JUnit version: 4.12
  • Type of test: unit test

Stack trace

java.lang.ClassCastException: java.lang.Object cannot be cast to java.lang.String

	at com.vmadalin.dynamicfeatures.characterslist.ui.list.paging.SampleTest$ClassTest.doSomething(FunTest.kt:35)
	at com.vmadalin.dynamicfeatures.characterslist.ui.list.paging.SampleTest.test(FunTest.kt:28)

Minimal reproducible code (the gist of this issue)

@RelaxedMockK
    private lateinit var dependency: MutableLiveData<String>

    @InjectMockKs
    var classTest: ClassTest = ClassTest()

    @Before
    fun setup() {
        MockKAnnotations.init(this)
    }

    @Test
    fun test() {
        classTest.doSomething()
        verify { dependency.value }
    }

    class ClassTest {
        lateinit var dependency: MutableLiveData<String>
        fun doSomething() : String? {
            return dependency.value
        }
    }
@lawloretienne
Copy link

I am having the same problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants