mockk

Cast exception when mocking kotlin lambda callbacks in Mockk

青春壹個敷衍的年華 提交于 2021-01-29 18:54:26
问题 I am having some trouble mocking callback functions with Mockk. I am trying to mock a task success listener that is called like this: collection .add(Item()) .addOnSuccessListener { update(collection.document(it.id)) } Where the java signature for the callback would look like this: public interface OnSuccessListener<T> { void onSuccess(T var1); } and the signature for addOnSuccessListener looks like this public abstract Task<DocumentReference> addOnSuccessListener(@NonNull OnSuccessListener

Android UI testing: Why LiveData's observers are not being called?

柔情痞子 提交于 2021-01-29 16:17:35
问题 I have been trying, without success, to do some UI tests on Android. My app follows the MVVM architecture and uses Koin for DI. I followed this tutorial to properly set up a UI test for a Fragment with Koin, MockK and Kakao. I created the custom rule for injecting mocks, setup the ViewModel, and on the @Before call, run the expected answers and returns with MockK. The problem is that, even when the fragment's viewmodel's LiveData object is the same as the testing class's LiveData object, the

Mock private property with mockk throws an excpetion

与世无争的帅哥 提交于 2021-01-27 12:50:28
问题 I'm using mockk for my testing in kotlin. But I can't seem to override a private property in a spy object. I have this object private val driverMapSnapshotMap: MutableMap<Int, SnapshotImage> = mutableMapOf() in a class that I spy on using viewModel = spyk(DriverListViewModel(), recordPrivateCalls = true) But when I try to make it fill up with mock values I get an error every { viewModel getProperty "driverMapSnapshotMap" } returns(mapOf(1 to mockkClass(SnapshotImage::class))) The error I get

Android UI tests with Espresso + MockK crash with SIGSEGV on emulators, fine on physical devices

…衆ロ難τιáo~ 提交于 2020-08-08 05:14:10
问题 I have just started using MockK to mock all the Repositories / Services logic in an MVP-based app for UI tests. I have some UI tests running a login activity where the Espresso inputs logins and passwords and using MockK I can fake various situation where the login fails or not. All services & repositories are standard Kotlin object, so I am using mockkobject and every/coEvery to override and handle login requests & tasks. On my physical devices, there are no issues with the tests at all, but

MockK - reinitialize mocks for each test

青春壹個敷衍的年華 提交于 2020-01-24 04:10:47
问题 I have some mocks created using: val someService = mockk<SomeService>(relaxed = true) There are multiple tests in the file and I want the mock to be reset for each test Is there currently a way to do this in MockK? I know there is MockKAnnotations.init(this), but it didn't look like there was a way to set relaxed = true in the @Mock annotation 回答1: For resetting mocks in MockK you can use clearMocks . To create relaxed mock via annotation just check @RelaxedMockK 来源: https://stackoverflow.com

Kotlin Mockito always return object passed as an argument

一世执手 提交于 2019-12-24 03:04:09
问题 I am trying to use Mockito on my mocked object in such a way that it should always return the very same object that was passed in as an argument. I tried it do to it like so: private val dal = mockk<UserDal> { Mockito.`when`(insert(any())).thenAnswer { doAnswer { i -> i.arguments[0] } } } However, this line always fails with: io.mockk.MockKException: no answer found for: UserDal(#1).insert(null) The insert(user: User) method doesn't take in null as an argument (obviously User is not a

Gradle fails: Failed to resolve: io after removing kotlintest

本秂侑毒 提交于 2019-12-11 15:26:54
问题 I've created a new Android Project and set up mockk in Intrument test and Unit test: androidTestImplementation "io.mockk:mockk:{1.9.3}" testImplementation "io.mockk:mockk:{1.9.3}" Gradle sync fine. If I add kotlintest and sync again everything works: android { compileSdkVersion 29 buildToolsVersion "29.0.1" defaultConfig { applicationId "com.example.testing" minSdkVersion 15 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

How to call a lambda callback with mockk

心已入冬 提交于 2019-12-10 17:17:15
问题 I create a mock of a class with mockk. On this mock I now call a method that gets a lambda as a parameter. This lambda serves as a callback to deliver state changes of the callback to the caller of the method. class ObjectToMock() { fun methodToCall(someValue: String?, observer: (State) -> Unit) { ... } } How do I configure the mock to call the passed lambda? 回答1: You can use answers: val otm: ObjectToMock = mockk() every { otm.methodToCall(any(), any())} answers { secondArg<(String) -> Unit>