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
2
votes
0 answers

How to Test the ViewModel Factory class in Android Kotlin

I have the below class For the ViewModel Fatory class MovieViewModelFactory(private val movieUseCase: MovieUseCase) : ViewModelProvider.Factory { override fun create(modelClass: Class): T { return if…
Amit Ranjan
  • 567
  • 2
  • 11
2
votes
1 answer

Exception in thread "main @coroutine#2" java.lang.NullPointerException in ViewModelTest?

I have wrote unit testing in ViewModel with coroutines with mockito. class but I am getting following exception Exception in thread "main @coroutine#2" java.lang.NullPointerException at…
user16305099
2
votes
0 answers

Use Spek with suspend method

I am trying to create a unit test using Spek framework and nhaarman mockito kotlin in my Android Kotlin project. The problem is that when there is nested suspend method I don't know how to mock response.This is how I'm trying I defined: val…
jyodoid
  • 261
  • 2
  • 8
2
votes
2 answers

How to mock Kotlin's List.forEach?

For the given code below, in Kotlin, it's not possible to execute the function forEach (or anything similar, map, filter, so on...) because BarList is mocked. So the native implementation of forEach and relatives are not being called. After a bit…
Pedro Paulo Amorim
  • 1,838
  • 2
  • 27
  • 50
2
votes
1 answer

Android Mockito-Kotlin error - "Actually, there were zero interactions with this mock"

The following is the test code for my ViewModel class. My app architecture is based on MVI. Basically, I'm trying to mock my repo and state classes. In the test function I wrote below, I'm trying to check if state is changed in correct order when I…
Ugurcan Yildirim
  • 5,973
  • 3
  • 42
  • 73
2
votes
0 answers

Testing methods passing high order function as property

I trying to test a method that have a high order fuction as property, and I'm facing this problem: org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Invalid use of argument matchers! 2 matchers expected, 1 recorded: -> at…
2
votes
1 answer

Code is running succesfully in real device but mockito wanted but not invoked

I am getting mockito wanted but not invoked. I don't know what to do cause Im new here :). by the way when I run the code in real device its okay. Wanted but not invoked: observer.onChanged(true); TestClass @RunWith(MockitoJUnitRunner::class) …
6155031
  • 4,171
  • 6
  • 27
  • 56
2
votes
1 answer

Android Mockito Kotlin coroutine test cancel

I have a retrofit service interface Service { @PUT("path") suspend fun dostuff(@Body body: String) } It is used in android view model. class VM : ViewModel(private val service: Service){ private val viewModelJob = Job() private val…
fergdev
  • 963
  • 1
  • 13
  • 27
2
votes
1 answer

How to mock a system class for type casting

I have the following code to get the current system memory: val memClass = (context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager).memoryClass My initial goal is to return an Int value for it in testing. Something like…
1
vote
2 answers

Can I use Mockito.any() into then method

I want to use any() inside then method. Here is my test code: @ExtendWith(MockitoExtension::class) internal class UserSignInProviderTest { @Mock lateinit var authApiClient: AuthApiClient @Mock lateinit var userRepository:…
Kook
  • 111
  • 7
1
vote
0 answers

Unit Test: wanted but not invoked (Mockito)

I'm development a unit test method using mockito, I already search about this error but anithing can solve this problem. Here is my implementation: @RunWith(MockitoJUnitRunner::class) @ExperimentalCoroutinesApi class ProductsViewModelTest :…
1
vote
1 answer

How do we verify method that has lambda function as its argument?

So I have this main() method that calls printResult() method and the printResult() has a lambda argument on second parameter like this: class SomeClass() { fun main(value: Int) { printResult(value){ it -> it + it } } …
1
vote
1 answer

InjectMocks doesn't work with Kotlin constructor arguments with default values

EDIT: I've created a ticket with mockito-kotlin here I have a class defined like so: package me.jpalacios.poc class MyClass( private val myDependency: MyDependency = MyDependency() ) { fun run() { myDependency.doSomething() …
user3505258
  • 311
  • 2
  • 9
1
vote
0 answers

Android Unit Test Mockito returns null

I'm using Mockito for unit testing. I want to test my usecase and with mock repository but there's some problem. I had my repository's function returns arraylist by using mockito. (here's code…
1
vote
0 answers

How do we write unit tests for application that uses SQLDelight?

I am writing a Kotlin JVM application that uses the SQLDelight to generate classes that needs to interact with a MySQL database. How can I write unit tests for classes/services that use the classes that SQLDelight library generated? For example,…
lawkai
  • 155
  • 1
  • 9