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"
Related
This question already has answers here:
What is "deps" in "implementation deps.support.app_compat"?
(2 answers)
Closed 1 year ago.
I've downloaded a repository from github and in the dependencies I've founded this
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation deps.kotlin.stdlib
implementation deps.support.app_compat
implementation deps.support.design
implementation deps.support.core_ktx
implementation deps.constraint_layout
implementation deps.arch_core.runtime
// Navigation
implementation deps.navigation.runtime_ktx
implementation deps.navigation.fragment_ktx
implementation deps.navigation.ui_ktx
// Android Testing Support Library's runner and rules
androidTestImplementation deps.atsl.runner
androidTestImplementation deps.atsl.rules
androidTestImplementation deps.room.testing
androidTestImplementation deps.arch_core.testing
// Espresso UI Testing
androidTestImplementation deps.espresso.core
androidTestImplementation deps.espresso.contrib
androidTestImplementation deps.espresso.intents
}
So I've tried to instal those dependencies to my project but I got an error
A problem occurred evaluating project ':app'.
Could not get unknown property 'deps' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
So any ideas what this "desp" means?
This is a way (with ExtraPropertiesExtension) to keep build.gradle files as clean as possible with an external file like a version.gradle file in which there are all dependencies and their version.
buildscript {
apply from: 'versions.gradle'
...
}
You can find this version.gradle at the root of the project. This file is applied in the ./build.gradle
The developer decided to manage his dependencies in this way but you can find many others.
This article present 3 ways to do it.
I've develop a project using zxing to scan the barcode. I had followed the tutorial from here, but unfortunately I got error when I tried to run the project to device. The error that I get is
This is the build.gradle(app)
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
// volley
compile 'com.android.volley:volley:1.0.0'
// butter knife
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
// picasso
implementation 'com.squareup.picasso:picasso:2.71828'
// QR Zxing Library
implementation 'com.journeyapps:zxing-android-embedded:3.6.0'
}
Below is External Libraries list.
After a day I tried to find out the solution, but everything failed. Then I decided to create a new project and copy the current source code together with build.gradle(app). Luckily no more error occurred when I run new project.
But I still don't know what the actual reason why I got that error before this, so I assume maybe some part of the old project are missing or have some bug on it.
Because at new project you change buildToolVersion
Reason : Its happened because being conflict between core.jar and zxing
Solution : Just change buildToolVersion and build , after that revert old toolVersion and build again !
After weeks of searching, the following steps help me resolve the issue like a charm:
1.Remove android platform.
2.Install cordova-plugin-facebook4
3.Create build.gradle in /plugins/cordova-plugin-facebook4/
4.Copy this
dependencies {
compile("com.facebook.android:facebook-android-sdk:4.37.0") {
exclude group: 'com.google.zxing'
}
}
to ../plugins/cordova-plugin-facebook4/build.gradle
5.Edit ../plugins/cordova-plugin-facebook4/plugins.xml change
to
6.Add platform android and build
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"
I want to use Mockito for unit testing, so I added the Mockito library into my gradle dependencies.
testImplementation 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:2.12.0'
But still, I can not use any Mockito annotations.
/androidTest/ExampleTest.kt
#RunWith(MockitoJUnitRunner::class) // Unresolved reference MockitoJUnitRunner
#Mock // Unresolved reference Mock
What I'm missing it?
You need to add the following dependencies in your app's build.gradle:
dependencies {
// ... more entries
testCompile 'junit:junit:4.12'
// required if you want to use Mockito for unit tests
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'
}
And click on sync
You may need another dependency:
androidTestCompile 'org.mockito:mockito-android:2.12.0'
Alternatively, you can try manually importing the annotations:
import static org.mockito.Mockito.*;
It could be that it didn't import properly and that's why it showed as an unresolved reference. Auto-import has its flaws
I have faced an issue with assembleDebugAndroidTest which is related to objenesis. So, based on Shylendra's answer, you may want to replace
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
with
androidTestImplementation("org.mockito:mockito-core:2.8.47")
Very confortable library over mockito:
testImplementation 'org.mockito:mockito-inline:2.21.0'
testImplementation('com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0') {
exclude group: 'org.jetbrains.kotlin'
exclude group: 'org.mockito'
}
// Also works like a charm with instrumentation tests
androidTestImplementation 'org.mockito:mockito-android:3.5.13'
androidTestImplementation('com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0') {
exclude group: 'org.jetbrains.kotlin'
exclude group: 'org.mockito'
}
when I add greenDao Library in my proyect, my project doesn't work, because I have Junit test.
testCompile 'junit:junit:4.12'
compile ('de.greenrobot:DaoGenerator:1.3.0')
Any idea?
I fix my problem, I change this
compile ('de.greenrobot:DaoGenerator:1.3.0')
to
compile('de.greenrobot:greendao:1.3.7')