Questions tagged [mockito-kotlin]

a library for Kotlin that provides helper functions to work with Mockito, a popular mock framework.

A library that provides helper functions to work with Mockito in Kotlin. Mockito is a popular mock framework.

66 questions
45
votes
7 answers

Mockito 3.4.0 Static Mocking Exception

I am getting the following exception when trying to mock the static method. For SettingsUtility, static mocking is already registered in the current thread To create a new mock, the existing static mock registration must be…
Qumber Abbas
  • 560
  • 1
  • 4
  • 11
12
votes
3 answers

Cannot initialize MockMaker

This is my testing class: class RocketListVMTest { @get:Rule var instantTaskExecutorRule = InstantTaskExecutorRule() private lateinit var sut: RocketListVM private var activeOnlyToggle = false private val repo: Repo = mock() …
Sorry
  • 532
  • 2
  • 6
  • 19
7
votes
0 answers

How to mock inline function with lambda using Mockito

Have a problem with mocking the inline function with lambda. Have class UserController, trying to mock tracer with function createSpan. UserController class: class UserController(private val tracer:Tracer) { fun subscribeUser() { …
Serhii Zadorozhnyi
  • 576
  • 2
  • 8
  • 25
6
votes
1 answer

how to add a library to testImplementation and androidTestImplementation

kotlin library for both test and android tests, unit tests and UI tests. If I write the bellow two lines as part of build.gradle it works fine. my question is, is this the right way to add the library to use in both test and android…
BRDroid
  • 3,920
  • 8
  • 65
  • 143
6
votes
2 answers

Mockito cannot mock/spy because : Final Class

I know this question is asked a lot of time, but I followed a lot of answer and it still didnt work How to mock a final class with mockito in this link, they said that we have to add in our gradle : testImplementation…
LikseN
  • 169
  • 1
  • 1
  • 7
4
votes
0 answers

How to Stub on Spy Object’s suspend method?

I got the idea how to stub suspend functions of regular mock object and also how to stub regular method of spy object. However, I did not find a way to stub the suspend function of spy objects. For e.g: If I do whenever(method).thenReturn() gives me…
4
votes
1 answer

Kotlin: How to verify an extension function is called on a mock

Say I have a Java class Metrics. I defined some extension functions on Metrics in Kotlin fun Merics.expose(name: String, value: Number) { // do something } Note that the Java class Metrics also has a method called expose but with different…
Rich
  • 1,669
  • 2
  • 19
  • 32
4
votes
2 answers

Test a class that involves listener callback inside AsyncTask

I'm trying to test a listener callback that is being triggered inside an AsyncTask, Listener class : interface LoaderListener { fun onByteSuccess(..., ..., ...) fun onByteFailure(..., ...) } The class containing AsyncTask : class Loader…
3
votes
1 answer

mockito, in koltin how to verify a static method

Having a kotlin singleton static method internal object TestSingleton { @JvmStatic fun staticMethod1 (str: String) { println("+++ === +++ TestSingleton.staticMethod(), $str") staticMethod2 (str) } @JvmStatic fun…
lannyf
  • 9,865
  • 12
  • 70
  • 152
3
votes
3 answers

Can't mock `willReturn` with Optional in Kotlin

I have a method fun getUser(userId: UserId): Optional = userRepository.findById(userId) that returns an Optional in Java. So I want to mock that method call in my Kotlin unit test. This was my first guess... given {…
xetra11
  • 7,671
  • 14
  • 84
  • 159
3
votes
1 answer

How to mock internally constructed instance with mockk.io?

I am writing unit test case in mockk and Junit5 for a static method defined in companion object of FileUtility class. the method is as below, class FileUtility { companion object { fun getFileObject(fileName: String): File { …
Akash Patel
  • 189
  • 1
  • 5
  • 13
3
votes
2 answers

Android Testing Repository with mock and Coroutines returns NullPointerException

I kinda confused on how am I supposed to test this I have this repository class: class AuthenticationBox( private val dataMapper: AuthenticationDataMapper, private val dataSource: AuthenticationDataSource ) :…
julioribeiro
  • 1,565
  • 2
  • 14
  • 22
3
votes
1 answer

Write unit Testcase for ViewModel in kotlin

I am using Junit & Mockito 4 for unit testing of viewModel. ViewModel class class MainViewModel(app: Application, private val githubRepo: GithubRepository) : BaseViewModel(app) { private val _trendingLiveData by lazy {…
3
votes
0 answers

Kotlin mockito argument matcher issue

I am trying to verify a particular argument among multiple arguments. The other arguments I wish to verify are of a simple nature. But one argument I wish to use check { } to verify it thoroughly. This fails with an error. I have put down the code…
sethu
  • 8,181
  • 7
  • 39
  • 65
2
votes
0 answers

Check If Service Is Started With Espresso By Click On View

I want to test that when i click on btnRestore , CloudRestoreService should be start. how to test this ? @Test fun testBtnRestore_expectedIntentCloudRestoreService() { Intents.init() onView(withId(R.id.btnRestore)) …
1
2 3 4 5