All Questions
70 questions
0
votes
0
answers
83
views
Junit4 : Misplaced or misused argument matcher detected here
There is one function which is present inside SampleActivity.
@AndroidEntryPoint
class SampleActivity : BaseActivity {
@JvmStatic
fun isConfigEnabled(string1: String?, string2: ...
1
vote
0
answers
337
views
Failed to release mocks : Getting this error while testing firebase insertion function in Android Studio
I have a FirebaseRepository file that contains a method addDonor that allows user to add Donor object to firebase.
package com.example.carepro
import android.util.Log
import com.google.firebase....
1
vote
0
answers
101
views
Testing android room with Junit5 context
I'm trying to migrate from Junit4 to Junit5 to make some parametrized test where room is involved. Currently for Junit4 it's working with @RunWith(AndroidJUnit4::class) class annotation and for Junit5 ...
2
votes
0
answers
350
views
kotlin android junit4 parameterized test
i got an issue,
i try to do a multi argument parameterized test with junit for instrumented test:
@RunWith(Parameterized::class)
class PileAddEditTopbarColorTest(
private val ...
0
votes
1
answer
326
views
Android Coroutine JUnit test failed
I'm new in kotlin testing. Today I will try first UnitTest
I have this testing class:
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
open class ...
0
votes
1
answer
57
views
Unfinished stubbing detected with JUnit4
I am testing my authentication class using JUnit4 and Mockito. I wrote the following simple test class:
@ExperimentalCoroutinesApi
class FirebaseAuthenticationDataSourceTest {
@get:Rule
val ...
0
votes
1
answer
44
views
use okhttp with junit4 cannot get response data
I found a confuse problem when I use okhttp with Junit in a kotlin project
by the way ,here is my environment:
okhttp 4.9.0
junit 4.12
kotlin 1.4.20
here is my simple code:
class ...
0
votes
1
answer
1k
views
Android Unit test with Room database error
Writing unit test to insert object to room database, take it from database and assert are the 2 mentioned equal. Here is the code:
@RunWith(AndroidJUnit4::class)
@SmallTest
class GdprDaoTest {
...
0
votes
1
answer
210
views
what's the difference between @Mockk and mockk<*>()?
In this Q&A, it says there's no difference. And some people says annotation is better or using constructor(mockk<*>()) is better.
For me, if they are equivalent, less line of code(not using ...
3
votes
0
answers
582
views
testing Repository class that takes DAO parameter
I am learning Kotlin to build a note app. I have created a repository class as shown below which takes a Dao parameter. For now, the source of data is just Dao but in the tutorial I am following, it ...
0
votes
2
answers
119
views
cannot find implementation for com.application.unittest.data.local.ShoppingItemDatabase. ShoppingItemDatabase_Impl does not exist
I have a problem with Room test I have checked all stackoverflow solutions, but nothing works for me.
I'm so beginner at test.
I tried a lot of solution like change kapt and add other libraries for ...
2
votes
1
answer
863
views
assertEquals fails for Error implementation but pass for Success one
I have these sealed interface
sealed interface Result<out T> {
data class Success<T>(val data: T) : Result<T>
data class Error(val exception: Throwable? = null) : Result<...
1
vote
0
answers
330
views
Mockito Test: verify static method was called inside object class
// writing junit ExerciseMainLogger class
so how to verify AnalyticsLog.insertEventLog(builder) using Mockito
I have mocked AnalyticsLog class but getting error Actually, there were zero ...
3
votes
2
answers
6k
views
How to unit test a kotlin coroutine that throws?
(Kotlin 1.5.21, kotlinx-coroutines-test 1.5.0)
Please consider the following code inside a androidx.lifecycle.ViewModel:
fun mayThrow(){
val handler = CoroutineExceptionHandler { _, t -> throw ...
1
vote
1
answer
975
views
Mocking methods that call JNI with Mockk
I am trying to add unit tests to an existing Android app that has some JNI libraries (only for ARM). I am using Mockk and Junit4, and I have the following:
This static class is mine, and it calls the ...
0
votes
2
answers
2k
views
Kotlin - Companion object not allowed in an inner class
I am trying to create a test class with an inner class but I am getting an error "companion object is not allowed here". What is supposedly be the problem? Please see my sample code snippet ...
0
votes
1
answer
1k
views
An annotation argument must be a compile-time constant @RunWith(AndroidJUnit4::class)
My test suite won't run and it appears to be the @RunWith(AndroidJUnit4::class). I was following along with a tutorial, so I'm not 100% sure what that part does, or what this means.
The @RunWith(...
2
votes
1
answer
3k
views
Mockk set private properties of collection in Kotlin
While accessing the private variables in Mockk, not able to set the values of private property.
We have CustomerImpl class and which has 1 private property called customerData.
We want to set data in ...
0
votes
1
answer
980
views
How to assert multi line text in TextView in Kotlin test
I have an EditText element in which an arbitrary text is allowed to be entered in. After the text has been entered, the user can click next and move to an activity that has the text they wrote inside ...
0
votes
0
answers
591
views
LocalDateTime.of returns null in test class
I have a class with a formatToSpecificText(inputDate: LocalDateTime) method that changes the date to text. In this method, I use the Duration.between(LocalDateTime.now(), inputDate) function. My ...
1
vote
2
answers
5k
views
How to use github actions bot for testing gradle java
I am trying to implement Github-actions(bot), which runs gradle test when PR has been created.
To assure that my workflow file works as I expected, I explicitly wrote a test method which should cause ...
3
votes
1
answer
707
views
How to apply a Rule to all test cases in a AndroidJUnitRunner?
I'm trying to apply a TestWatcher as a rule across all my test cases run by a particular runner.
MetadataCollector:
class MetadataCollector : TestWatcher() { ... }
TestRunner:
class TestRunner : ...
4
votes
1
answer
2k
views
JUnit 5: How do I use multiple extensions in a test written in Kotlin?
I am trying to migrate my tests for a ViewModel from JUnit 4 to JUnit 5, and use MockK in conjunction. In JUnit4, I have made use of rules--namely rules for RxJava2, LiveData, and Coroutines within ...
0
votes
1
answer
2k
views
unit test and mock a repository
I want to inquire how to mock a repository class and make a unit test for its functions.
Repository Class
class RequestRepository {
/** suspend function to get the result of token request from ...
1
vote
1
answer
177
views
calendar test passes when using AndroidJunit4 only
I have a weird case. I am running a local test and using the Calendar class within that test.
when annotating the test class with @RunWith(AndroidJUnit4::class) the test passes, otherwise, the test ...
1
vote
3
answers
4k
views
How can I ignore some JSON and object properties during unit testing?
I have a method that is migrating old JSON structure to a new one that I would like to test, but there is one randomly generated property (UUID) that I would like to ignore.
The interesting part of ...
3
votes
0
answers
61
views
Custom AndroidJUnitRunner dependencies injection
I have a custom implementation of AndroidJUnitRunner (let's call it MyRunner) and will need to access my dagger graph from it.
Actually, I need some of the injected components to register an ...
4
votes
0
answers
413
views
Espresso tests can fail when running in succession, but tend to pass when ran individually?
I'm running Espresso tests using JUnit4, using Android 9. When I run tests individually, they successfully pass. However, when I run tests using classes or suites of classes, one or two tests tend to ...
0
votes
1
answer
417
views
Android WireMock Junit Rule and Non Junit Java Usage do not work
I want to write a test for an Android Application with WireMock Integration.
I tried using all 3 different Wiremock Integration approaches -
1. Writing a test with JUnit Rule
2. Non-JUnit and ...
2
votes
1
answer
11k
views
Argument passed to when() is not a mock! Example of correct stubbing: doThrow(new RuntimeException()).when(mock).someMethod();
When using Mockito in JUnit4, I encounter the error in the title.
My code is shown below
package cn.patest.judgerAutoscalerKotlin
import org.junit.Test
import org.mockito.*
import java.lang....
0
votes
1
answer
2k
views
how to create unit test cases for kotlin data classes
I have created few data classes for my sample application. I need to write unit test cases for those data classes now. I am using Junit4.12. Here is my data class:
data class Tracking(val param1 : ...
1
vote
0
answers
269
views
Unresolved Reference From Sub Module Test
i want use some code in my main module those code presents in the sub module library in androidTest folder but when compilin the error shown unresolved reference
here is my code
fun performClick(@...
1
vote
0
answers
159
views
Kotlin-Android studio 3.6.1 Gradle 5.6.4 Could not determine the dependencies of task ':app:compileDebugUnitTestJavaWithJavac'
Andoid-Studio-3.6.1 - Kotlin- am trying to do some tests for my application but this Error aquire and i cant fix it (am new at Android developement)
Error :
FAILURE: Build failed with an exception.
...
4
votes
0
answers
668
views
Problem with dagger injection in Fragment Test
Im basing my app development on GithubBrowserSample from android/architecture-components-samples including tests. I've already done my local unit tests but with the UI test I'm facing some trouble.
...
4
votes
1
answer
4k
views
Kotlin Mockk test for suspend cancellable coroutine cancellation
I have classes
// final class from some library like okhttp
class NetworkCaller {
fun call() {
// performs some real operation
}
fun cancel() {
// .... cancels the ...
0
votes
3
answers
3k
views
Android data class unit test
I have a data class below -
data class MyViewState(
val loading: Boolean = false,
val data: String? = null,
val error: String? = null
)
I have a simple JUnit4 test -
@Test
fun ...
22
votes
2
answers
20k
views
Android unit testing view model that receives flow
I have a ViewModel that talks to a use case and gets a flow back i.e Flow<MyResult>. I want to unit test my ViewModel. I am new to using the flow. Need help pls. Here is the viewModel below -
...
1
vote
2
answers
1k
views
Android unit testing : calling a method from fragment which uses context
I am trying to call a method that is defined in a fragment from a unit test class. The method uses 'getString()' method to access a string resource, because of this I'm getting below exception :
java....
2
votes
0
answers
760
views
jUnit4 - Why testing a suspendable function fails with `RuntimeException: ... AndroidJUnit4 could not be loaded`?
When I test a simple db written with Room, a test that expects an exception fails when dao functions are declared with suspend.
The error is:
java.lang.RuntimeException: Delegate runner 'androidx....
1
vote
1
answer
1k
views
Mockito / Powermock: matcher any() falsely does not match (but anyOrNull() does
I am using Mockito / Powermock in an Android unit test, written in Kotlin.
I have code as follow:
verify(myCompanionMock, atLeastOnce()).someMethod(any(), any())
But I get this error:
Comparison ...
2
votes
1
answer
524
views
Not able to run unit tests on kotlin Multiplatform project
I am trying to create a kotlin Multiplatform library which can later convert into java and javascript using IDEA 2019.3, kotlin 1.3 .
I created a simple junit (4) test class and configured it as ...
0
votes
2
answers
381
views
How would I go about unit testing this function in Kotlin?
I'm pretty new to unit testing. I've been given the task of testing this code. I understand that I have to use assertEquals to check if for example if
RegionData.Key.DEV returns VZCRegion.Development....
1
vote
1
answer
487
views
Kotlin Spring Boot Unit Test - adding in @TestExecutionListeners doesn't inject dependencies
I'm trying to use the Flyway Test Extensions library, and one of its instructions was to add in:
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
...
12
votes
1
answer
11k
views
JUnit Local Test - 'Unresolved reference: test'
Expected
Importing libraries such as androidx.test:core:1.2.0, androidx.test.ext:truth:1.2.0, com.google.truth:truth:0.44, and etc. into a local JUnit test class named ExampleUnitTest.kt.
Observed
...
3
votes
0
answers
246
views
Why are Robolectric involved when specifying @RunWith(AndroidJUnit4::class) as my testrunner?
private static String getRunnerClassName() {
String runnerClassName = System.getProperty("android.junit.runner", null);
if (runnerClassName == null) {
// TODO: remove this logic when ...
1
vote
1
answer
2k
views
Tests fail when run together but succeed individually even when instances are re-mocked before each test
I've looked at this SO post and made sure my tests don't share the same mocked instances, but the tests still fail when run together but succeed when run individually.
I suspect there might be ...
0
votes
2
answers
605
views
RxJava Unit Testing for Observable.Interval, "Context has changed" error
I am trying to write unit tests for the code block below :
Observable.interval(INITIAL_DELAY, REFRESH_PERIOD, TimeUnit.SECONDS)
.flatMap {thisMethodReturnsAnObservable()}
....
6
votes
1
answer
2k
views
Kotlin: IllegalAccessException: Class BlockJUnit4ClassRunner can not access a member of class Foo with modifiers “private”
Using Kotlin with Junit 4 I get the following exception for Parameter field injection:
java.lang.IllegalAccessException: Class org.junit.runners.parameterized.BlockJUnit4ClassRunnerWithParameters ...
0
votes
1
answer
3k
views
mocking internal classes with mockito
i have a class under Test which is marked as "interal"
internal class UnderTest{
fun methodToTest(){}
}
In my JUnit Test i want test UnderTest
import com.nhaarman.mockito_kotlin.mock
class ...
1
vote
1
answer
396
views
Unit test cases for Callbacks using Junit
I'm trying to write some unit-test cases for a class with functions that has callbacks as arguments (Please see code below)
class NetworkApi(private val url: String) {
fun getToken(listener: (...