I am starting with Robolectric framework 3.0. for android testing. I am goingthrough there site and they mention the first step as adding in build gradle
testCompile "org.robolectric:robolectric:3.0"
I have done that and did a complete clean build.
Now once I start writing my first test case I get comilation issue-- cannot find symbol class RunWith for the annotation #
RunWith(RobolectricGradleTestRunner.class)
Please tell me, what I am missing? I thought mavencentral() will download the respective jar file.
Thanks in advance.
You can refer this this link.
Make sure your test case locations are proper.
Android Studio defaults to looking for tests in the following locations:
Unit Tests => src/test/java
Related
I have the following Project Structure:
Main App
--app
--featureModule1
--featureModule2
--TestKit(Library module)
where Testkit has all dependencies including app and featureModules. The Testkit included all the unit tests related to the features in app and featuremodules.
When i run unit tests from Android studio(Right click-> Run Test in Testkit), they run fine. However whenever i try running it from gradle command: ./gradlew TestKit:testInternalDebugUnitTest, it throws NoClassDefFoundError for all the dependencies of other modules(for both app and feature modules).
Also i have already added implementation and testImplementation dependencies of all the modules in test module.
Please suggest:
Do i need to add path of classes generated in other modules, if yes pls guide where.
Also running Test with Coverage fails with
java.lang.VerifyError: Bad return type
Please suggest what am i doing wrong here.
Note:
I have created a Testkit because of the multiple open issues of Roboelectric in feature modules for ex: https://github.com/robolectric/robolectric/issues/5428
Versions
AS: 4.0.1
gradle: 4.0.1
gradle-wrapper: 6.5
Robolectric: 4.3.1
I was able to solve the problem using the following runtime test dependencies:
testRuntimeOnly(files("${projectDir}/../app/build/intermediates/app_classes/internalDebug/classes.jar"))
testRuntimeOnly(files("${projectDir}/../featureModule1/build/intermediates/app_classes/internalDebug/classes.jar"))
testRuntimeOnly(files("${projectDir}/../featureModule2/build/intermediates/app_classes/internalDebug/classes.jar"))
inside build.gradle of Testkit.
Can refer to the following link for more details:https://github.com/android/app-bundle-samples/issues/11#issuecomment-675725610
http://www.tutorialspoint.com/junit/junit_environment_setup.htm
I have done everything like in this step by step tutorial and still when I try to write the line
import org.junit.*;
The "junit" part is lighten red and the error message is "cannot resolve symbol "junit"".
The tutorial you are following seems to be written for command line usage. (Download jar, set classpath etc). So the same wouldn't work in IntelliJ.
IDEs actually make these a lot easier.
To solve your current dilemma, try this. Place the cursor on the red 'junit' (which is basically a broken import) and choose "Add Junit4 to classpath". This will add the necessary junit jars that IDEA ships with to your classpath.
Open up your build.gradle and make sure JUnit is there
dependencies {
testCompile 'junit:junit:4.12'
// other dependencies...
}
And for a better link for Android-specifics, read Getting Started with Testing from the Android docs.
I feel dumb for even having to ask this, but I can't find an answer anywhere. I'm trying to write simple unit tests that test static methods from my Android app and I've already added
// Required -- JUnit 4 framework
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
To my app build.gradle file, but org.junit.test annotations or anything related to Junit4 will NOT resolve. There is no "Unit Test" build variant either.
Can anybody assist me? Thanks.
looks like you have not download dependencies:
try run "gradle build"
or sync gradle project if your IDE has this option
A new AndroidStudio 1.1 version introduced the unit testing support. This URL http://tools.android.com/tech-docs/unit-testing-support provides step-by-step instruction how to setup IDE to run JUnit tests for Android sources.
This plugin https://bitbucket.org/hvisser/android-apt used to provide Dagger2 generated files to AS and it works OK for usual Android code but unfortunately there is no generated Dagger2 files for any JUnit test class. I tried to configure dependency like
androidTestApt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
according to android-apt plugin documentation but without success.
I think the problem is in different sources directory for Unit tests - it's src/test/java instead of src/androidTest/java that used by android instrumentation tests.
Can you please provide any help or info how to resolve this trouble?
Having
// You version may vary
androidTestApt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
in your dependencies, open a terminal in your project, run
./gradlew assembleTest
This will generate the Dagger component classes living under your androidTest source set.
Go back to Android Studio, the class now exists and can be used.
I have read around, there are a number of extensive answers (like this one) but the Android world evolves so fast that they seem to be a bit outdated and the official documentation still refers to Eclipse with ADT.
I am running AS 1.1 and I am trying to setup simple junit tests to run on the emulator, without Robolectric. If I don't include junit in my build.gradle, it can't find #After, #Before and #Test and I get package org.junit does not exist. Upon adding
// unit tests
androidTestCompile 'junit:junit:4.11'
the error becomes
Error:duplicate files during packaging of APK
[...]/app/build/outputs/apk/app-debug-test-unaligned.apk
Path in archive: LICENSE.txt
Origin 1: [...]/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar
Origin 2: [...]/.gradle/caches/modules-2/files-2.1/junit/junit/4.11/4e031bb61df09069aeb2bffb4019e7a5034a4ee0/junit-4.11.jar
You can ignore those files in your build.gradle:
android {
packagingOptions {
exclude 'LICENSE.txt'
}
}
Following the console suggestion of excluding LICENSE.txt, it then works but it feels like a hack. So I'm wondering, am I maybe missing something? Thanks.
Android Studio unit testing support comes in 1.1 Beta 4 (release announcement) with Gradle plugin version 1.1.0-rc1.
More info in official document.
However it is experimental feature for now. E.g. it breaks installDebug gradle task.
For using JUnit in instrumentation tests there is good guide for Espresso library and another covering new AndroidJUnitRunner.
If it's any use I set up a boiler plate project allowing the use of Unit tests and Espresso tests by the use of switching build variants. You won't need the use of any third party plugins with this.
https://github.com/hitherejoe/Android-Boilerplate