Android kotlin mockMaker issues - android

Hi i am trying to mock a final class(as all classes in kotlin are final by default) and added the following dependencies in my gradle:
testImplementation 'junit:junit:4.12'
testImplementation 'au.com.dius:pact-jvm-consumer-junit_2.11:3.5.10'
testImplementation "org.mockito:mockito-android:2.13.0"
testImplementation 'org.mockito:mockito-inline:2.13.0'
testImplementation "org.mockito:mockito-core:2.13.0"
//testImplementation 'io.mockk:mockk:1.8'
testImplementation 'org.assertj:assertj-core:3.8.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'junit:junit:4.12'
androidTestImplementation "org.mockito:mockito-core:2.13.0"
androidTestImplementation "org.mockito:mockito-android:2.13.0"
androidTestImplementation 'org.mockito:mockito-inline:2.13.0'
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.2"
the mockito-inline is supposed to enable you to mock a final kotlin class and so i added to both my java unit test and my instrumental test using testImplementation and androidTestImplementation
On building the project i get the following error:
More than one file was found with OS independent path 'mockito-extensions/org.mockito.plugins.MockMaker'
Any ideas whats going on? if i remove the androidTestImplementation of the mockitio inline, it compiles fine but when running intrumental test i get the mockito error saying it cannot mock a final class.

In order to be able to mock final classes in Kotlin, you’ll need to create a file
org.mockito.plugins.MockMaker (literally) that contains only this line
mock-maker-inline
and place it into
test/resources/mockito-extensions.
For more info please read https://antonioleiva.com/mockito-2-kotlin/.

mockito-inline won't work for instrumentation tests. In order to be able to mock final classes in instrumentation tests you just need to include the following line only:
androidTestImplementation com.linkedin.dexmaker:dexmaker-mockito-inline:2.28.0
Refer to its Github page for more details
This thread might also be useful.

As per this answer you cannot mock final classes in androidTest- it is a limitation explained in the Mockito Github issue tracker here:
There is no real possibility to make this [mocking of final classes] work in Android at the moment as it lacks the instrumentation API on top of which we are operating. The Android VM is not a standard VM and only implements a subset of the Java specification. As long as Google does not choose to extend its JVM, I am afraid that this feature will not work.

In my case, I got this error because I use implementation "org.mockito:mockito-inline:2.13.0" instead of testImplementation "org.mockito:mockito-inline:2.13.0"

Related

Instrumented Test not running in Android Tests Passed 0 Passed

I have been trying to run a test case , present in androidTest Package.
But as i execute the test, Emulator launches and I get tests passed : 0 Passed.
and getting this Error in logcat
E/AndroidJUnitRunner: An unhandled exception was thrown by the app.
E/InstrumentationResultPrinter: Failed to mark test No Tests as finished after process crash
here is my code.
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.test.assertEquals
#RunWith(AndroidJUnit4::class)
class MyAndroidTest {
#Test
fun test_simple() {
assertEquals(2, 1+1)
}
}
I have added all required dependencies.
testImplementation 'junit:junit:4.12'
androidTestImplementation 'junit:junit:4.12'
testImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.3"
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
testImplementation "org.robolectric:robolectric:4.6"
testImplementation "com.google.truth:truth:1.1.3"
androidTestImplementation "com.google.truth:truth:1.1.3"
testImplementation 'org.mockito:mockito-core:2.24.5'
// required if you want to use Mockito for Android tests
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
When I run the same test case in test package, it runs successfully.
One thing is, I created The androidTest package by myself. It was somehow deleted earlier..
I get this after I run the test case,
plz help.
Add testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" in defaultConfig block inside gradle module file
Is missing in that project and it's necessary.

Unable to run Android Instrumented Tests

I am trying to run some UI tests using espresso on AS Arctic Fox 2020.3.1 Patch 3 ARM preview. Following Google instructions for setting up these are my dependencies:
// Core library
androidTestImplementation 'androidx.test:core:1.4.0'
// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test:rules:1.4.0'
androidTestUtil 'androidx.test:orchestrator:1.4.0'
// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.ext:truth:1.4.0'
androidTestImplementation 'com.google.truth:truth:1.0.1'
// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.4.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.4.0'
I have also added the testInstrumentationRunner in my Gradle to testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
I am trying to run the simplest possible test below:
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.junit.Test
import org.junit.runner.RunWith
#RunWith(AndroidJUnit4::class)
class DummyTest {
#Test
fun test() {
assert(false)
}
}
but when I click the Run of my test it looks like I have 0 tests. Is there something I am missing? Thanks
If AS shows TestResult 0/0 , delete that dependency
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0'
Found a way to fix this one after spending many hours. I noticed that the issue had to do with the
androidx.test.espresso:espresso-contrib:3.4.0
check this answer for more insight link
Thought I was not getting any errors or issues. I had to disable the Test Matrix of the AS. After disabling I was getting more clear framework crash errors in the logical which helped me identify the issue.

Android Studio Javadoc won't generate because "Selected scope contain no Java classes"

I am trying to generate the JavaDoc for the Activity class source code in Android Studio 3.6.3. The file is open in the editor, and I use "Tools | Generate JavaDoc".
When I click OK, it fails with the message "Error running 'Javadoc': Selected scope contain no Java classes". I also tried the "Custom Scope" option with "Current File" choice, but same results.
Can someone help why can I not generate the JavaDoc for this file?
The file is too large to paste here, it is 410832 characters, only 30000 allowed.
I could paste some of it if that would help.
I think maybe JavaDocs needs compiled code with no errors, missing references, etc. When I open Activity.java, It is not compilable.
Thanks for the idea. It was someone else's code, so I looked at another project of mine where the build.gradle file for the module has:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
Then I followed the declaration chain in my code for an Activity up to the Activity superclass -- and it goes to the same source file. I guess my SDK has one source file regardless of the library used. Anyway, same problem, it won't print the JavaDoc. I don't understand why it won't print that source file, since it exists.

RxTextView and other widgets not found while using latest com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2 library

I wanna use RxJava binding APIs for Android UI widgets in my project.
Therefore following the guidance as per this site 'https://github.com/JakeWharton/RxBinding'
But I am unable to import any Android UI widgets in my Kotlin File.
Where as its working fine if I am consuming these widgets in Java File.
Hence, not been to find the actual of this issue.
For reference following are the gradle file and class files(both kotlin and java) using in same project
build.gradle
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
implementation 'androidx.core:core-ktx:1.2.0-alpha01'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
implementation 'io.reactivex.rxjava2:rxjava:2.2.8'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
//RxBinding
implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha2'
}
BindingExample.java class
RxBindingExample.kt class
Have tried exploring this issue on S.O. but questions or solutions are available for previous version of lib 'com.jakewharton.rxbinding2:rxbinding'
Check this reference:
https://github.com/JakeWharton/RxBinding/blob/master/rxbinding/src/main/java/com/jakewharton/rxbinding3/widget/TextViewTextChangeEventObservable.kt
There are different ways to use depending on the language, note the #file:JvmName("RxTextView") at the start. If you're using java the class RxTextView is visible, in kotlin you should use the extension functions provided by the lib, textChangeEvents() is an example of it.
like that, this will aquire the observable e.g.:
val someTextView = TextView(context)
someTextView.textChangeEvents()
Edit:
Seems that the old class that I've referenced was deleted, here is another one:
https://github.com/JakeWharton/RxBinding/blob/master/rxbinding/src/main/java/com/jakewharton/rxbinding4/widget/TextViewAfterTextChangeEventObservable.kt

Mockito+PowerMock gradle configuration

I need to use in my android instrumented tests mockito and powermock.
The main problem is that both of them have some problems with configuring it in gradle because of conflicts and other stuff.
Maybe somebody who has working configuration of .gradle file for mockito+powermock in android instrumented tests could share it?
This is my gradle configuration to use mockito and powerMock:
dependencies {
...
/**Power mock**/
testCompile "org.powermock:powermock-core:1.7.3"
testCompile "org.powermock:powermock-module-junit4:1.7.3"
testCompile "org.powermock:powermock-api-mockito2:1.7.3"
/**End of power mock **/
}
NOTE: I had to remove the mockito dependency in order to make it works:
//Remove this line
testImplementation "org.mockito:mockito-core:2.13.0"
Here is the configuration I am using and it's working perfectly fine.
after 1.7.0 powermock-api-mockito change to powermock-api-mockito2
testImplementation 'org.mockito:mockito-all:1.10.19'
testImplementation "org.powermock:powermock-module-junit4:2.0.7"
testImplementation "org.powermock:powermock-module-junit4-rule:2.0.7"
testImplementation "org.powermock:powermock-api-mockito2:2.0.7"
testImplementation "org.powermock:powermock-classloading-xstream:1.6.6"

Categories

Resources