Espresso Dagger 2.11 - android

Struggling to use Dagger 2 for UI testing powered by Espresso. I'm trying to generate a dedicated test #Component under androidTest directory but getting this error:
Error:Bad service configuration file, or exception thrown while constructing
Processor object: javax.annotation.processing.Processor:
Provider dagger.android.processor.AndroidProcessor could not be instantiated:
java.lang.NoClassDefFoundError: com/google/common/collect/SetMultimap
Here's how dependencies look like:
androidTestCompile "com.google.dagger:dagger:2.11",
androidTestCompile "com.google.dagger:dagger-android:2.11"
androidTestCompile "com.google.dagger:dagger-android-support:2.11"
androidTestAnnotationProcessor "com.google.dagger:dagger-compiler:2.11"
androidTestAnnotationProcessor "com.google.dagger:dagger-android-processor:2.11"
Have anyone faced with this and has a clue how to solve it?
Thanks.

Found a solution, basically, the culprit was lying in build.gradle just a few lines below:
configurations {
...
androidTestCompile.exclude group: 'com.google.guava', module: 'guava'
...
}
This line was excluding some useful stuff from Dagger library and preventing it from being compilable.

Related

java.lang.ClassNotFoundException: Didn't find class "io.ktor.client.HttpClientJvmKt"

I have create kotlin multiplatform project for handling API.
I integrated this in my main project but I am getting following exception.
java.lang.NoClassDefFoundError: Failed resolution of: Lio/ktor/client/HttpClientJvmKt;
I tried to add following dependencies in my main project still issue persists.
dependencies {
implementation "io.ktor:ktor-client-core:1.3.2"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:1.3.7"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:0.20.0"
}
packagingOptions {
exclude 'META-INF/kotlinx-io.kotlin_module'
exclude 'META-INF/atomicfu.kotlin_module'
exclude 'META-INF/kotlinx-coroutines-io.kotlin_module'
exclude 'META-INF/kotlinx-coroutines-core.kotlin_module'
}
Still I am getting this issue.
Any help would be appreciated.
In MPP you need to declare dependencies for each target.
For example:
commonMain needs to have the dependency you used above:
implementation "io.ktor:ktor-client-core:1.3.2"
androidMain needs to have it's own dependency:
implementation "io.ktor:ktor-client-okhttp:1.3.2"
iosMain also needs it's own dependency:
implementation "io.ktor:ktor-client-ios:1.3.2"
Note the suffix of ktor-client-xxxx.
So my guess is, you only need to replace the dependency in your main project to io.ktor:ktor-client-okhttp (or whichever client you prefer)

androidTestCompile mockito givning Error

I'm trying to use Mockito for instrumentation tests, when I add
androidTestCompile 'org.mockito:mockito-core:2.7.22'
I get the following error:
Error:Conflict with dependency 'org.objenesis:objenesis' in project ':app'.
Resolved versions for app (2.1) and test app (2.5) differ. See
http://g.co/androidstudio/app-test-app-conflict for details.
I have no clue what's wrong, I have no org.objenesis dependency specified anywhere in my code (guess it comes through mockito).
Every help, appreciated.
Try this
configurations.all { resolutionStrategy { force 'org.objenesis:objenesis:2.1' } }
or
Just exclude it in your main project. exclude group: 'org.objenesis'
The link in the exception tells you how to remedy this.
configurations.all {
resolutionStrategy {
force 'org.objenesis:objenesis:2.1.0'
}
}
Otherwise you can use:
androidTestCompile 'org.objenesis:objenesis:2.1.0'

How to figure out which version of com.android.support.test dependencies I need?

I need to write some automated tests in the project I'm working on. So I just copied the required dependencies from some Google's example:
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
// Espresso UI Testing dependencies.
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
The problem I ran into was something you could find in a million questions on SO:
Warning:Conflict with dependency 'com.android.support:recyclerview-v7'. Resolved versions for app (25.3.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.
Here I am not asking about the required versions of the test runner, test rules, and the other test dependencies, to be able to sync Gradle and move on.
My question. Is there any quick way to see which versions of com.android.support.test:runner, com.android.support.test:rules, etc. depend on Support Library 25.3.0?
Of course, I could keep replacing 0.5 with 0.6, 0.7, etc, until Gradle syncs successfully, but it's boring and would take me long.
The short answer is that currently there isn't a version that depends on Support Library 25.3.0 since that version of the support library was released long after the official stable testing artifacts.
If you want to be able to discern this with future versions, you would need to look at the transitive dependencies of each of the artifacts. You can see these in the maven pom files, or via gradle by running:
gradle :your_module_name:dependencies
This being said, you can usually use any version of the support library by telling gradle to ignore Espresso's transitive dependencies like this:
androidTestCompile("com.android.support.test.espresso:espresso-contrib:2.2.2") {
exclude group: 'com.android.support', module: 'appcompat'
exclude group: 'com.android.support', module: 'support-v4'
exclude module: 'recyclerview-v7'
}
you can use the gradle dependencies command above in order to figure out which espresso artifacts are pulling in the transitive dependencies that are causing conflicts.

java.lang.IllegalStateException: Could not initialize plugin: MockMaker

Trying to run instrumentation test on AS.
stuck with this Error:
java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy4.isTypeMockable(Unknown Source)
ExampleInstrumentedTest.java
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Mock
Context context;
#Before
public void init(){
MockitoAnnotations.initMocks(this);
}
#Test
public void testDisabledFlag() {
ChanceValidator chanceValidator = new ChanceValidator(context);
Validator.ValidationResult result = chanceValidator.validate(2);
assertEquals(result, Validator.ValidationResult.NO_ERROR);
}
}
build.gradle
apply plugin: 'com.android.application'
android{
..
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
// Unit testing dependencies
testCompile 'junit:junit:4.12'
// Set this dependency if you want to use the Hamcrest matcher library
testCompile 'org.hamcrest:hamcrest-library:1.3'
// more stuff, e.g., Mockito
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
compile project(':mortar')
compile project(':mockito-core-2.6.6')
}
Update:
After commenting line-
MockitoAnnotations.initMocks(this);
It is building fine(No Exception) but context mocked is now null.
This Worked in my case:
dependencies {
def mockito_version = '2.7.1' // For local unit tests on your development machine
testCompile "org.mockito:mockito-core:$mockito_version" // For instrumentation tests on Android devices and emulators
androidTestCompile "org.mockito:mockito-android:$mockito_version"
}
I didn’t comment initMocks
In my case, I was working on a project that does not use the maven build system. So this is what worked for me.
Navigated to the maven repo for mockito (used v2.26): https://mvnrepository.com/artifact/org.mockito/mockito-core/2.26.0. I downloaded the jar.
On the same page at the bottom, I looked up the dependencies. For mockito 2.26.0, these dependencies are:
Byte Buddy v.1.9.10
(https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy/1.9.10)
Byte Buddy Java Agent v1.9.10
(https://mvnrepository.com/artifact/net.bytebuddy/byte-buddy-agent/1.9.10)
Objenesis v2.6
(https://mvnrepository.com/artifact/org.objenesis/objenesis/2.6) I
downloaded the jar files for the above mockito dependencies.
In Eclipse I created a user library containing the four jar file and added it to my project.
NB: (creating the library is optional, you can add the jars directly to your project build path)
Hope this helps someone.
Do not explicitly include mockito, let powermock pull in what it needs.
I got this problem resolved after adding transitive dependencies for 'mockito-core'.
I was facing this problem in eclipse. I was using 'mockito-core 3.8.0' along with 'mockito-junit-jupiter 3.8.0'.
At first I tried to resolve this by changing JRE to JDK in Project/ Java Build Path ((as many have posted this as resolution), but that did not solve the problem.
Then I added below 3 transitive dependencies for 'mockito-core 3.8.0' explicitly, and it worked!
1. net.bytebuddy » byte-buddy v1.10.20
2. net.bytebuddy » byte-buddy-agent v1.10.20
3. org.objenesis » objenesis v3.1
(https://mvnrepository.com/artifact/org.mockito/mockito-core/3.8.0 - see compiled dependencies)
I am using Quarkus on a big project with many people.
Most of our microservices used this dependency version
<net.bytebuddy.version>1.12.9</net.bytebuddy.version>
One microservice used:
<net.bytebuddy.version>1.11.0</net.bytebuddy.version>
Which was not compatible with our
<artifactId>quarkus-junit5-mockito</artifactId>
When I added more tests on a resource, I got the error of this question.
I changed the bytebuddy to 1.12.9 and mockito worked.
Make sure your bytebyddy's version is compatible with you mockito version.
Updated either one of them to be compatible with each other.

Android : What is the correct gradle configuration to use Mockito to support espresso and unit test both?

I am running couple of tests using Espresso and some tests are plain junit, both tests require mockito.
Wondering how to include mockito correctly to support both , if I use one of below configuration other wouldn't work.
1) testCompile "org.mockito:mockito-core:2.+"
2) androidTestCompile "org.mockito:mockito-core:2.+
I currently configure and run them in separate build types, because of the dexmaker conflict. Not sure whether it is the intended/correct way or there is a better way of doing it.
So assuming you have already a "debug" build type, define another, i.e. "espresso". Then the build gradle would look like:
android {
...
testBuildType 'espresso'
}
...
dependencies {
...
def mockito = "org.mockito:mockito-core:2.+"
espressoCompile(mockito) {
exclude module: 'hamcrest-core'
}
espressoCompile("com.crittercism.dexmaker:dexmaker-mockito:$dexMakerVersion") {
exclude module: 'hamcrest-core'
}
espressoCompile "com.crittercism.dexmaker:dexmaker-dx:$dexMakerVersion"
androidTestCompile "com.android.support.test.espresso:espresso-intents:$espressoVersion"
androidTestCompile "com.android.support.test.espresso:espresso-contrib:$espressoVersion"
androidTestCompile mockito
testCompile mockito
}
When you want to run the tests, you need to call the targeted gradle scripts, i.e. 'testDebug' or 'installEspressoAndroidTest'.
Or within Android Studio, you need to run the espresso tests after switching to "espresso" build variant.
Hope it helps or give you some idea about it.

Categories

Resources