I'm just starting to play around with Android and I'm having trouble with some androidTest examples that I've come across. Basically, I get a Cannot resolve symbol 'test' error message from Android Studio in the import statements for the InstrumentationRegistry and AndroidJUnit4 classes of my instrumented test file:
ExampleInstrumentedTest.java
package com.example.androidtestexample;
import android.content.Context;
import android.support.test.platform.app.InstrumentationRegistry;
import android.support.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
#Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.androidtestexample", appContext.getPackageName());
}
}
As a consequence, the AndroidJUnit4 symbol of the #RunWith statement can't be resolved either and when I try to run the instrumentation test I get an 'Edit configuration' window with an 'Instrumentation runner class not specified' error.
I'm using Android Studio 3.5.3, but this example uses 'com.android.tools.build:gradle:2.2.3'. The app gradle.build is as follows (I had to add the aaptOptions statements as suggested here to get the minimum reproducible example working):
build.gradle (Module: app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.example.androidtestexample"
minSdkVersion 22
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
aaptOptions.cruncherEnabled = false
aaptOptions.useNewCruncher = false
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.0.1'
testCompile 'junit:junit:4.12'
androidTestCompile 'junit:junit:4.12'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
As suggested in this question, I have double-checked that the debug build variant was selected and that the instrumentation test source file was in the src/androidTest/java/ folder.
I understand that this example is quite outdated and, from another of the answers to the aforementioned question and from this page, that the android.support.test package is deprecated and AndroidX should be used instead. However, from this page I also understand that it should still be possible to use the Android Support library for historical artifacts, hence my question:
Is there a way for me to fix this problem and run such old examples with the android.support.test package without needing to migrate them to AndroidX?
Related
I'm writing an android plugin for unity to be able to check if notifications are enabled for the game. I have one java class with a method for checking if notifications are enabled. When i build the plugin and then the .apk with unity everything works fine. But after installation, when calling the mehthod i get the following exception:
java.lang.NoClassDefFoundError: Failed resolution of: Landroidx/core/app/NotificationManagerCompat;
The Java class
package com.example.plugin;
import android.app.Activity;
import androidx.core.app.NotificationManagerCompat;
public class NotificationPlugin {
public static boolean areNotificationEnabled(Activity unityActivity) {
return NotificationManagerCompat.from(unityActivity).areNotificationsEnabled();
}
}
buld.gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
gradle.properties
org.gradle.jvmargs=-Xmx1536m
android.useAndroidX=true
android.enableJetifier=true
Haven't found a solution anywhere else yet. Thank in advance. It's probably a super simple fix that i miss.
Update
Custom gradle.properties in unity set with android.enableJetifier and android.useAndroidX set to true
Jetifier was enabled in unity
Androidx.Core libary was added to the dependencies of the gradle.build file of the plugin
None of the above solved the issue
Solution
As Hamid Yusifli suggested in his answer a custom gradle build template needs to be enabled (Project Settings>Player>Publishing Settings>Custom Main Gralde Template) and the dependencies for the libary need to be added (implementation 'androidx.core:core:1.5.0' in my case). This solved the issue.
So, seems like in your case unity ignores your library gradle dependencies,
there are many different reasons why this could happen. To force unity to include missing dependencies you must provide a custom Gradle build template and add your dependencies in that file.
I am a beginner to testing. I have created a simple test case for login activity in android studio. But I got an error and I could not solve it. Here is my test code. Help will be really appreciated.
package com.example.hassidiczaddic.testinglist;
import android.app.Application;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.ApplicationTestCase;
import android.test.suitebuilder.annotation.LargeTest;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
#RunWith(AndroidJUnit4.class)
#LargeTest
public class ApplicationTest {
public static final String STRING_TO_BE_TYPED = "Wolfmatrix";
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void LoginActivity() {
onView(withId(R.id.etFName))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withId(R.id.etLName))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard());
onView(withId(R.id.btnSubmit))
.perform(click());
onView(withId(R.id.tvView))
.check(matches(withText(STRING_TO_BE_TYPED)));
}
}
This is my error:
Running tests
$ adb shell am instrument -w -r -e debug false -e class
com.example.hassidiczaddic.testinglist.ApplicationTest
com.example.hassidiczaddic.testinglist.
test/android.test.InstrumentationTestRunner
Client not ready yet..Test running started
junit.framework.AssertionFailedError: No tests found in
com.example.hassidiczaddic.testinglist.ApplicationTest
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at
android.test.InstrumentationTestRunner.onStart
(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run
(Instrumentation.java:1619)
Tests ran to completion.
Here is my gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.example.hassidiczaddic.testinglist"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.0.1'
// App dependencies
compile 'com.android.support:support-annotations:23.0.1'
compile 'com.google.guava:guava:18.0'
androidTestCompile 'com.android.support:support-annotations:23.0.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}
}
You have forgotten to set AndroidJUnitRunner as the default test instrumentation runner.
https://developer.android.com/topic/libraries/testing-support-library/index.html
To set AndroidJUnitRunner as the default test instrumentation runner in your Gradle project, specify this dependency in your build.gradle file:
android {
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
Update
with androidx things have changed a little, check https://developer.android.com/training/testing/set-up-project
If you are targeting SDK Version 28, you'll be using the re-packaged androidx classes from the support library, so AndroidJUnitRunner is configured like so...
android {
defaultConfig {
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
}
check build.gradle
replace testInstrumentationRunner "androidx.test.runner.AndroidJUnit4"
android {
defaultConfig {
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
}
Look for app/src/androidTest/AndroidManifest.xml
change android:name to androidx.multidex.MultiDexApplication
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="android.support.multidex.MultiDexApplication">
To
<application
android:allowBackup="true"
android:label="Components Tests App"
android:supportsRtl="true"
android:name="androidx.multidex.MultiDexApplication">
More suggestions:
No tests found when running instrumented tests with AndroidX
If you landed here by generating the tests from the method... Try renaming the test method in your test class.. to something similar to actualMethod() to testActualMethod()
Check the logcat!
This can happen for a multitude of reasons and the build logs aren't always that helpful, even when using the --stracktrace --info, or --debug flags.
In my case, this was due to a missing runtime dependency. I only figured this out after checking the logcat and seeing the error there.
If you've checked all the obvious things mentioned in the other answers and your still having this issue, check the logcat for errors.
I'm trying to implement a test class in Android Studio to make some test on a DBAdapter. Since I need to run the test on my mobile phone in order to use the database, I have created an Instrumentation Unit Test (cause I've tried to do it just with a Unit test but I need to use the database and so, and those are only run locally).
The problem is that when I try to run the test class, using my mobile phone as running device, the compiler throws the following error:
error: package org.junit does not exist
I've been looking for a solutione, but I found none.
This is my test class (just the skeleton):
import org.junit.Test;
import android.support.test.runner.AndroidJUnit4;
import static org.junit.Assert.*;
public class DbAdapterTest {
#Test
public void testCreateSeries() throws Exception {
}
}
And this one is the build.gradle script:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.w2w.whattowatch"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
testOptions {
unitTests.returnDefaultValues = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
I also have another issue. As you can see, I also imported this:
import android.support.test.runner.AndroidJUnit4;
But, even before running, it says of "runner" that "cannot resolve the symbol". I've added the TestInstrumentationRunner on build.gradle, but still not working.
OK, I solve it so, this is the solution that worked for me.
I didn't have this dependences, so I add them to the build.gradle script:
androidTestCompile 'com.android.support:support-annotations:23.1.1'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
I am trying to access a raw resource called test.txt located in androidTest/res/raw. I have setup my project according to the documentation found here.
My project structure:
src
main
java
res
androidTest
java
res
raw
test.txt
Here is my test class:
package something.traveltracker;
import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.SmallTest;
import java.io.InputStream;
import something.traveltracker.models.RuterStop;
public class RuterStopAndroidTest extends InstrumentationTestCase {
#SmallTest
public void testCreateRuterStopFromJSON() {
InputStream is = getInstrumentation().getTargetContext().getResources().openRawResource(R.raw.test);
RuterStop ruterStop = RuterStop.createFromJSON(is);
assertSomething...
}
}
The problem is that R.raw. only lists up the resources in the main/res/raw folder. I read somewhere that I had to add .test when importing R;
import something.traveltracker.test.R
However this is not working for me. I am using Android studio 1.0.2 with gradle 2.2.1.
Heres my androidManifest.xml
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "something.traveltracker"
minSdkVersion 19
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
lintOptions {
abortOnError false
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}
Question:
How can I access this test resource? I need the resource only when running this unit test, and I dont want it to be included in the final apk.
EDIT: I am now able to import something.traveltracker.test.R in my class by adding the following line to my build.gradle file:
sourceSets {
androidTest.res.srcDirs = ["src/androidTest/res"]
}
This is quite an old post, but since there are no answers and in case someone like me wonders how to do this:
Create the folder src/androidTest/resources
Inside the folder, add your resources under the package of the test where you will load them (so if accessing from com.test.MyTest, put the files in src/androidTest/resources/com/test/test.txt)
Load the file using getClass().getResourceAsStream('test.txt')
So I've set up a gradle project with android and tried to get some tests to run. Unfortunately they don't seem to. It's possible that I'm missing something obvious but here goes...
I am running gradle 1.11 and as I understand the documentation that's the new folder (since 0.9 I believe?) that should be used for tests.
So I have my testclass ::
package se.coinhunter.multigradle.test;
import org.junit.Test;
import static org.junit.Assert.*;
public class HelloAndroidTests {
#Test
public void testHelper() {
assertEquals(1,1);
}
}
}
That lives in src/androidTest and here is my build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
defaultConfig {
minSdkVersion 11
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':MultiGradleSubmodule')
}
This is a multi-project build and the submodule mentioned in the dependencies block is a plain java project that has its' own scource and unit tests running quite smoothly. I was able to specify that it should tell me when it runs its' tests and give me some feedback and that works fine. That was achieved for that project using
test {
testLogging {
events 'started', 'passed'
}
}
in its' build.gradle. I havn't come across anything like this for android projects. The whole project builds and runs, but I either can't get the tests to run, or they're running but I'm not getting any output.
You're using jUnit 4 (package name "org.junit" with #Test annotation). Android gradle only works with jUnit 3 (package name "junit.framework" with no annotations).
Android tests run in the Dalvik virtual machine on a device or emulator so your test class should also extend "AndroidTestCase" (or one of the other junit subclasses in Android - depending on what you're testing).
UPDATED: also add the following to default config:
testInstrumentationRunner "android.test.InstrumentationTestRunner"
testFunctionalTest true
Run the test using
gradle connectedCheck