How to mock Kotlin Object in android? - android

I have an object in kotlin that controls the current user's session information. I want to mock the sign in method which has a call back.
While testing, I need to mock this method in the SessionController object.
object SessionController {
...
fun signIn(username: String, password: String, signInCallBack: SignInCallBack) {
sessionApi.attemptSignIn(username,password,object: SignInCallBack{
override fun onSignInComplete() {
signInCallBack.onSignInComplete()
}
override fun onErrorOccurred(errorCode: Int, errorMessage: String) {
signInCallBack.onErrorOccurred(errorCode)
}
})
}
....
}
The AndroidTest goes something like this:
#RunWith(AndroidJUnit4::class)
class LoginActivityTest {
#Test
fun loginErrorShowing() {
test.tapUsernameField()
test.inputTextinUsernameField("wrongusername")
test.pressUsernameFieldIMEAction()
test.inputTextinPasswordField("randomPassword")
test.pressPasswordFieldIMEAction()
Espresso.onView(ViewMatchers.withId(R.id.errorText)).check(ViewAssertions.matches(withText("Wrong Password")))
}
}
Any suggestions/ideas as to how I can achieve this? I've read online to use Mockk for kotlin but have't been able to mock this method and invoke the appropriate callback. Any suggestions on improving the structure would also be appreciated.
Thanks

Well in my opinion you should made SessionController implementing an interface.
object SessionController: ISessionController {
override fun signIn(username: String, password: String, signInCallBack: SignInCallBack) {
(...)
}
}
interface ISessionController {
fun fun signIn(username: String, password: String, signInCallBack: SignInCallBack)
}
This will give you a lot of possibilities to solve your problem like:
Dependency Injection
Test product flavour
Simple mockk() cration in your test code
It is a bit hard to give you very strict answer because you didn't post any UT code ;)
EDIT
It is hard to cover such a big topic as mocking in one post ;)
Here are some great articles:
Dependency Injection: https://medium.com/#elye.project/fast-mocked-ui-tests-on-android-kotlin-89ed0a8a351a
Using different flavour for tests: https://android-developers.googleblog.com/2015/12/leveraging-product-flavors-in-android.html
Creating Unit Tests you can always do simple:
presenter.sth = mockk<ISessionController>()

Related

mockk a global property in kotlin

I am facing the same issue as asked in the below question. please help me out.
Mock a "global" property in Kotlin
I tried solution provided in above question but nothing is working. and I am asking the same question because I am not able to post any comment on the previous question.
I am trying to write test case for below class
class CustomLogger constructor(val ctx: Context, embEnabled: Boolean = false) : Logger {
private val loggers = arrayListOf<Logger>()
fun get() = loggers
init {
if (embEnabled)
loggers.add(Emb(ctx))
if (BuildConfig.DEBUG)
loggers.add(DebugLogger(ctx))
}
override fun logError(t: Throwable, msg: String?) {
loggers.forEach { logger ->
logger.logError(t, msg)
}
}
}
enter code here
Here I am trying to mock get() or init{}
that was on dam question but i got you
note this can only be used in unittest as mockito static mock is not support on Android JVM
testImplementation "org.mockito:mockito-inline:4.8.1" you gonna need
this so added
Update you need to call this i forgot to add it sorry in your test case before call the method
Mockito.mockStatic(Class.forName("com.udacity.project4.locationreminders.RemindersActivityKt"))
fun getMockForMethod(clazz: Class<*>, methodName: String, methodResponse: Any) {
val method: Method = clazz.getMethod(methodName)
Mockito.`when`(method.invoke(null)).thenReturn(methodResponse)
}
now i created the method to handle no argument methods you can modifiy it as you see fit just pass the class using it name
getMockForMethod(Class.forName("com.udacity.project4.locationreminders.RemindersActivityKt"),
"doSomething","New Response")
Assert.assertEquals("New Response", doSomething())
works like a charm Enjoy 😁
i have updated the above code for anyone to use with static members in kotlin
your updates makes this easy to do now it is a class that you can mock entirly and easliy mock any methods
val loggerMock= Mockito.mock(Logger::class.java)
Mockito.`when`(loggerMock.loggers).thenReturn(new array of loggers)

Skip Android class (WorkManager) in a Unit test

I can't change project code, I can only write tests.
I have a function for testing:
fun funToBeTested() {
ClasWithWorkManager().veryVeryBadFunc(TAG)
// a lot of code to be tested below
...
}
and class with WorkManager:
class ClasWithWorkManager() {
companion object {
#JvmStatic
fun veryVeryBadFunc(tag: String) {
WorkManager.getInstance().cancelAllWorkByTag(tag)
WorkManager.getInstance().pruneWork()
}
}
}
There are no Android class references in the rest of this func.
Can I somehow cover with Unit test all funToBeTested() except of its first line?
Right now I have obvious WorkManager is not initialized properly. problem

ArgumentMatchers in MockK

I'm converting from Java to Kotlin, and from Mockito to MockK.
I'm stuck at converting Argument Matchers from Mockito to MockK.
Mockito can do any() to match anything, including nulls and varargs. (imports ArgumentMatchers.any)
For example:
verify(object1).store(any(SomeClass.class));
Does MockK have anything for that? In this specific situation, it's not any primitive type. I'm trying to match a class Object.
Thank you!
If you want to match a specific type, you could do
verify { object1.store(ofType(SomeClass::class)) }
In mockk, you can any() like this.
verify { object1.store(any()) }
In the case of migrate of Mockito to Mockk, consider the following:
With Mockito is encapsuled the class inside verify method, for example:
verify(object1).store(any(SomeClass.class));
In Mockk use lambda with receiver, similar to apply function (but without return), for example:
verify{ object1.store(any<SomeClass::class>()) }
And response your question, for specify the type can use any<YourType::class>, although the compiler marks it as unnecessary code because it has type inference, This is mainly useful when you have overloaded a function, so you can differentiate which parameters to receive for each one, for example:
class YourClass {
fun someMethod(value: String) {
}
fun someMethod(value: Int) {
}
}
fun test() {
val mock: YourClass = mockk()
verify { mock.someMethod(any<String>()) }
verify { mock.someMethod(any<Int>()) }
}

Verify suspend function with Matchers.anyObject()

I'm attempting to add coroutines to our Android application but I'm hitting a snag with our mocking framework. My interface has a suspend function like so:
interface MyInterface {
suspend fun makeNetworkCall(id: String?) : Response?
}
Here is how I'm attempting to verify the code was executed in my unit test
runBlocking {
verify(myInterface).makeNetworkCall(Matchers.anyObject())
}
When I do this I'm getting the following error
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at com.myproject.MyTest$testFunction$1.invokeSuspend(MyTest.kt:66)
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
Is there another way we should verify that the appropriate method is being called when using coroutines? Any help would be appreciated.
I tried to write similar test using the code you have provided. Initially, I got same error as yours. However, when I used mockito-core v2.23.4, the tests were passed.
Here are quick steps which you can try:
add testCompile "org.mockito:mockito-core:2.23.4" to the dependencies list in your build.gradle file.
Run the tests again, and you should not get similar error.
As Matchers.anyObject() is deprecated, I used ArgumentMatchers.any().
Below you can see the client code:
data class Response(val message: String)
interface MyInterface {
suspend fun makeNetworkCall(id: String?) : Response?
}
class Client(val myInterface: MyInterface) {
suspend fun doSomething(id: String?) {
myInterface.makeNetworkCall(id)
}
}
Here is the test code:
class ClientTest {
var myInterface: MyInterface = mock(MyInterface::class.java)
lateinit var SUT: Client
#Before
fun setUp() {
SUT = Client(myInterface)
}
#Test
fun doSomething() = runBlocking<Unit> {
// Act
SUT.doSomething("123")
// Verify
Mockito.verify(myInterface).makeNetworkCall(ArgumentMatchers.any())
}
}

How to pass null to an Observable with nullable type in RxJava 2 and Kotlin

I initialize my variable like this:-
val user: BehaviorSubject<User?> user = BehaviorSubject.create()
But I can't do this. IDE throws an error:-
user.onNext(null)
And doing this, IDE says u will never be null:-
user.filter( u -> u!=null)
As Guenhter explained, this is not possible. However, instead of proposing the null-object pattern, I'd recommend an implementation of the Optional type:
data class Optional<T>(val value: T?)
fun <T> T?.asOptional() = Optional(this)
This makes your intent much clearer, and you can use a destructuring declaration in your functions:
Observable.just(Optional("Test"))
.map { (text: String?) -> text?.substring(1)?.asOptional() }
.subscribe()
Using the null-object pattern here can cause more bugs than it solves.
If you use rxkotlin/rxjava 2.0 (I assume so) than the answer is: you can't. The reason is explained here.
This is a break of the interface. Have a look at the Observable Interface
public interface Observer<T> {
/** ... */
void onSubscribe(#NonNull Disposable d);
/** ... */
void onNext(#NonNull T t);
/** ... */
void onError(#NonNull Throwable e);
/** ... */
void onSubscribe(#NonNull Disposable d);
/** ... */
void onNext(#NonNull T t);
/** ... */
void onError(#NonNull Throwable e);
...
The #NonNull will be considered by the Kotlin compiler and therefore you CAN'T pass null.
Even if you could, the onNext would immediately throw an error:
#Override
public void onNext(T t) {
if (t == null) {
onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
return;
}
...
}
If you really need such a thing as null you have to fake it. e.g. by creating a static object of User which represents your null-element.
e.g.
data class User(val username, val password) {
companion object {
val NULL_USER = User("", "")
}
}
...
val user = BehaviorSubject.create<User>()
...
user.onNext(User.NULL_USER)
...
user.filter { it !== User.NULL_USER }
But if is somehow possible, try to avoid the null concept and maybe think of another solution where this isn't needed.
Thank you very much for all your answers but I ultimately went with this solution:-
class UserEnvelope(val user:User?) {}
And using this in the observables.
This best suited my requirements.
I am new to Kotlin so I don't know how to use Optionals. But from what I understand, I would have to typecast it to User type everytime I need to observe the values right?
To implement the solution mentioned in the nhaarman's answer, you can use the util class Optional (doc) from the Android SDK which was added in API level 24.
If your app's minSdkVersion less than 24 then you still need to implement it by yourself.
Since RxJava 2 does not support null values, there are some other acceptable solutions you can use:
Work with a custom or third party wrapper library of Optionals like some of the posted answers suggest. When I got rid of Java in favour of Kotlin, Optionals went away in the same package since Kotlin per se supports nullability as part of its type System. Just by this change the code was much more clearer, and I personally don't want to get Optionals back in my code as long as I can avoid them.
Emit Any class instances with your subject type. For example you could create an Empty.INSTANCE enum class which would emulate the null value and then filter by the enum class.
The last one is the one I use and prefer being a variant of the previous solution and is based on specialisations. Our friends of JetBrains always emphasise that classes are very cheap in Kotlin, so this would be a quick example to distinguish logged users and not logged ones:
abstract class SessionUser
sealed class LoggedUser(val username: String, val password: String) : SessionUser()
sealed class LogoutUser : SessionUser()
private val user = BehaviorSubject.create<SessionUser>()
private val loggedUser =
user.filter { it is LoggedUser }.cast(LoggedUser::class.java)
fun login(username: String, password: String) {
user.onNext(LoggedUser(username, password))
}
fun logout() {
user.onNext(LogoutUser())
}
I've taken an approach similar to Optional<User> and UserEnvelope. I make a simple User class and a ReifiedUser class that inherits from it. The User class has a companion object that has a NONE instance. The BehaviorSubject is instantiated with the User.NONE instance. It looks something like this:
open class User {
companion object {
val NONE = User()
}
}
class ReifiedUser(
#field:JsonProperty(J.FirstName) val firstName: String,
#field:JsonProperty(J.LastName) val lastName: String
) : User()
My BehaviorSubject is instantiated like this:
val activeUser: BehaviorSubject<User> = BehaviorSubject.createDefault(User.NONE)
And wherever I need to use activeUser I either flatMap it to Observable.empty() if it's NONE or just figure out what it is and what to do in the subscriber.
I don't like mixing java Optional with kotlin nullable because mixing map and let gets really confusing and ugly. This way it's very obvious what's going on.
I think it makes more sense to write a container class such as Result. An example of that would be
data class Result<T>(value: T?, error: Throwable?)
Usage
Observable.create { observer ->
upstreamService.listen(object: UpstreamListener {
onSuccess(data: User) {
observer.onSuccess(Result(data))
}
onError(exception: Throwable) {
observer.onSuccess(Result(null, exception))
}
}
}

Categories

Resources