I'm having trouble with AndroidJunit4.class import.
I created the test class in the androidTest/java/ folder but it seems that the class is not found.
Even if I force the right import it still doesn't work. ( import android.support.test).
#RunWith(AndroidJunit4.class)
public class RandomTest {
}
Here is my gradle :
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dependencies {
compile "com.android.support:support-annotations:23.4.0"
compile "com.android.support:appcompat-v7:23.4.0"
compile 'cat.ereza:customactivityoncrash:1.5.0'
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile('com.android.support.test:runner:0.5')
androidTestCompile('com.android.support.test:rules:0.5')
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')
testCompile('com.android.support.test:runner:0.5')
testCompile('com.android.support.test:rules:0.5')
testCompile('com.android.support.test.espresso:espresso-core:2.2.2')
testCompile('com.android.support.test.espresso:espresso-contrib:2.2.2')
testCompile('com.android.support.test.espresso:espresso-intents:2.2.2')
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile "org.robolectric:robolectric:3.1.1"
}
Weird thing is that I can find AndroidJunit4 file in my project, but in its parent AndroidJunit4ClassRunner, AndroidRunnerParams class is not resolved even if I also can find the file in my Project.
Comment out the annotation
//#RunWith(AndroidJunit4.class)
In Android Studio, choose "Build > Rebuild Project"
Get the annotation back in again
#RunWith(AndroidJunit4.class)
ALT-ENTER -> Import class
Related
Android Studio 2.2.3
Install Android Support Repository - ver. 44.0.0
I setup all as in official site for Espresso:
https://google.github.io/android-testing-support-library/docs/espresso/setup/index.html
I try to write instrumentation test (Espresso) in package androidTest. So I create StringUtilAndroidTest in folder src/androidTest/java/com/mycompany/
My StringUtilAndroidTest code:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class StringUtilAndroidTest {
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);
#Test
public void myTest() {
assert(true);
}
}
In my build.gradle:
android.defaultconfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
my dependencies:
testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1
But in StringUtilAndroidTest I get compile error:
#RunWith(AndroidJUnit4.class)
Cannot resolve symbol RunWith
Why?
Short answer: add this to your dependencies and you're golden.
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
Long answer:
In the default configuration, an Android Studio project has two different testing "variants": test & androidTest. The former uses the 'src/test/java', and the latter 'src/androidTest/java' (which is your scenario).
There's a big difference between the two: androidTest needs an emulator or a device to run, and test doesn't. This means that test is much faster to run (usually a couple seconds on the IDE), but it doesn't have access to the Android Framework (like Activities, Contexts & etc). On the other hand, androidTest takes much longer to run (not to mention the waiting time for the emulator itself), but it does have the Android framework (since it's running in one).
Since they're two separate variants, you need to declare their dependencies separately as well. testCompile and androidTestCompile each adds that dependency only to their own variant. To have JUnit on both, you have to declare to dependency on both - essentially "repeating" the line.
P.S.: Note that when you use compile, that adds it to all variants, so you don't have to repeat non-test dependencies.
You probably have missed some dependency.
//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'
Hope this will fix your code.
I'm trying to import
import android.support.test.InstrumentationRegistry;
my build.gradle file
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
in default config:
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
Is there a library I'm missing here? I'm trying to import InstrumentationRegistry but it doesn't recognise it!
Check what kind of test do you use.
InstrumentationRegistry used for Instrumented tests
that use emulator or device and they are placed in src/androidTest and use config androidTestCompile.
If you use Local unit tests for JVM from folder src/test you should use
config testCompile
testImplementation 'com.android.support.test:runner:1.0.2'
After that you can import InstrumentationRegistry, but you will get other errors at run-time.
try
compile 'com.android.support.test:runner:0.2'
instead of
testCompile 'com.android.support.test:runner:0.2'
it seems, com.android.support.test had been recently excluded from some other package (no clue which one), which also resulted in android.support.test.InstrumentationRegistryto be unknown; not excluding it from com.android.support.test:runner fixed the issue for me.
androidTestImplementation ("com.android.support.test:runner:1.0.2") {
// exclude group: "com.android.support.test"
exclude group: "com.android.support"
}
basically, androidTestImplementation needs to contain com.android.support.test once.
Another alternative - you can also add androidx.test:monitor dependency where the class InstrumentedRegistry is.
androidTestImplementation 'androidx.test:monitor:x.y.z'
I am attempting to run the test from Robolectric.org's Writing Your First Test page. The test in question looks like this:
#Test
public void clickingLogin_shouldStartLoginActivity() {
WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, WelcomeActivity.class);
assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
}
I get this compile error: Cannot resolve method 'assertThat(android.content.Intent).
The two possibilities I see for importing this method are org.hamcrest.MatcherAssert.assertThat and org.junit.Assert.assertThat, neither of which have a single-argument assertThat method as is being used in this Robolectric test.
My app's build.gradle has these dependencies:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.android.support:design:23.1.0'
testCompile "org.robolectric:robolectric:3.0"
testCompile 'junit:junit:4.12'
}
What framework/library is this test using?
It is neither junit or hamcrest assertion API. I think it is Android AssertJ or just AssertJ:
testCompile 'org.assertj:assertj-core:1.7.1'
follow the following and the issue should go away. Put the first line in you gradle build file
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'junit:junit:4.12'
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
public class SomethingTest {
#Test
public void testSomething() {
assertThat(true, 1>1);
}
}
this link should provide more details also
Android Studio and Robolectric
So I have a library module that wraps an API, and I want to write some tests. I want to import things like JUnit and MockWebServer, but only for the test sourceSet, and not for the androidTest one, as I want to use the former because the latter would cause the tests to be run in an android device or AVD, which I do not want. Therefore, I have this in my gradle file:
sourceSets {
main {
test {
setRoot('src/test')
}
}
}
...
dependencies {
...
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
}
However, this will not work, and instead I will have to import the dependencies as androidTestCompile ones. Why is this?
You should have a structure like this:
root
module
src
main
test
And all you need in your build.gradle is (without setting source sets):
dependencies {
// Unit testing dependencies.
testCompile 'junit:junit:4.12'
testCompile 'com.squareup.okhttp:mockwebserver:2.4.0'
}
You can check in the official repo from Google:
A collection of samples demonstrating different frameworks
collection of Google's Android testing tools
If you would like to use unit test and Instrumentation tests you should have:
root
module
src
main
test
androidTest
In this case your build.gradle should be:
dependencies {
// Dependencies for local unit tests
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-all:1.10.19'
testCompile 'org.hamcrest:hamcrest-all:1.3'
// Android Testing Support Library's runner and rules
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}
I couldn't find any info on how to setup powermock with Android Studio/Gradle. Everything I've tried resulted in build exceptions.
Could anybody show a correct way to do it?
Thanks.
I'm posting in order to help future readers, you need to add these dependencies for powermock in AS
testImplementation 'junit:junit:4.12'
testImplementation 'org.powermock:powermock-api-mockito:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule-agent:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4-rule:1.6.2'
testImplementation 'org.powermock:powermock-module-junit4:1.6.2'
Add the following lines to your dependencies{} block:
testCompile 'junit:junit:4.12'
testCompile 'org.powermock:powermock:1.6.5'
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
And if you would like to use PowerMockito, add the following line:
testCompile 'org.powermock:powermock-api-mockito:1.6.5'
In the build script, add the following:
sourceSets {
unitTest {
java.srcDir file('*your test directory*') //for example: tests/java
}
}
android {
sourceSets {
instrumentTest.setRoot('*your root test directory*') //for example: tests
}
}
repositories {
mavenCentral()
}
dependencies {
testCompile 'junit:junit:4.11'
testCompile 'org.powermock:powermock-mockito-release-full:1.4.9'
}
Then, do gradle unitTest from the command line.
Hope that works. If it doesn't, post the output of the command line.
If you want to use more recent versions of Mockito, you can use something like this, which is adapted from the mockito 2 Powermock docs. Do make sure you use the right version of PowerMock for the given version of Mockito.
...
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:2.4.0"
testCompile 'org.powermock:powermock-module-junit4:1.7.0RC2',
'org.powermock:powermock-api-mockito2:1.7.0RC2'
// mockito
testImplementation 'org.mockito:mockito-core:2.4.0'
androidTestImplementation 'org.mockito:mockito-core:2.4.0'
// PowerMock
testImplementation 'org.powermock:powermock-core:1.7.0RC2'
testImplementation 'org.powermock:powermock-module-junit4:1.7.0RC2'
testImplementation 'org.powermock:powermock-api-mockito2:1.7.0RC2'
I have used same as #Bhargav used with some additional features added with it
code coverage for test case (if testCoverageEnabled is true, then it enable Jacoco tool)
unit test will test only your code and do not depend on any particular behaviour of Android platform by using (UnitTests.returnDefaultValues = true)
Add this marked lines in build.gradle to enable JUnit, PowerMockito, JaCoCo
My example compiled from all the other answers I could find, with latest versions at the time of writing:
app\build.gradle
dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.13'
...
testImplementation group: 'org.powermock', name: 'powermock-api-mockito2', version: '2.0.7'
testImplementation group: 'org.powermock', name: 'powermock-module-junit4', version: '2.0.7'
}
A test class where say Android Log class was static mocked.
import android.util.Log;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
#RunWith(PowerMockRunner.class)
#PrepareForTest({Log.class})
public class DateUtilsTest {
#BeforeClass
public static void beforeClass() {
PowerMockito.mockStatic(Log.class);
}
...
}