I created a test suite class that I created to run my Espresso instrumented tests. For some reason, when I try to run it, the build fails with a load of build errors, including "Unresolved reference" for each of the modules and tests, and "An annotation argument must be a compile-time constant". Is it an issue with the tests living in different directories?
package org.x.android.group_1
import org.x.android.group_1.module_x.TestA
import org.x.android.group_1.module_x.TestB
import org.x.android.group_1.module_y.TestC
import org.x.android.group_1.module_y.TestD
import org.junit.runner.RunWith
import org.junit.runners.Suite
#RunWith(Suite::class)
#Suite.SuiteClasses(
TestA::class,
TestB::class,
TestC::class,
TestD::class
)
class TestSuiteGroup1
edit: the tests I'm passing in are written in Java, could that be an issue?
I know this was posted a while ago, but I came across this same issue.
Turned out that I needed to put my tests in the
androidTest/java/com/example/appname folder instead of the
test/java/com/example/appname folder
Once I moved everything it worked great!
Related
I am in the process of migrating to kotlin coroutines test 1.6.x
This is a very basic test that fails:
import kotlinx.coroutines.test.runTest
import org.junit.Test
class HouseDaoTest {
#Test
fun insertAndGetHouseFromDb_success() = runTest {
assert(1 == 1)
assert(2 == 2)
}
}
The error message I get when running this test is:
No virtual method find(Lkotlin/jvm/functions/Function1;)Lkotlinx/coroutines/internal/ThreadSafeHeapNode; in class Lkotlinx/coroutines/internal/ThreadSafeHeap; or its super classes (declaration of 'kotlinx.coroutines.internal.ThreadSafeHeap' appears in /data/app/~~rVhSG062Jzg60YKAgWe6uQ==/my.packagename.test-Up28py4pbfA6CKyThEVObA==/base.apk)
The line of code that hold the find method can be found here:
https://github.com/Kotlin/kotlinx.coroutines/blob/81e17dd37003a7105e542eb725f51ee0dc353354/kotlinx-coroutines-test/common/src/TestCoroutineScheduler.kt#L262
But I have honestly no clue what I am doing wrong, since this test is as basic as it gets. I found another SO post with this issue and it mentions that downgrading to 1.6.0 works, but not for me.
I also posted an issue in the respetive gihub: https://github.com/Kotlin/kotlinx.coroutines/issues/3503
Got it and I feel stupid. The tests did not have access to the coroutine-core module.
I now set it up like this:
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
One of my classes uses SpannableStringBuilder and I need to write unit test code for it. This class is for general use and does not interact with any UI components. I considered it as a local unit test but Android Studio complains that 'unit test not mocked'. I've checked Android Mock and robolectric but I can't figure out which instrumentation I have to mock. I also tried putting the test under the AndroidTest directory and ran it as an instrumentation test but still got an error 'Class not found: "....." Empty test suit'.
The unit test code:
package xxxxxxx; // Sorry but I should keep it secret now.
import android.support.test.filters.SdkSuppress;
import android.test.suitebuilder.annotation.SmallTest;
import android.text.SpannableString;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import xxxxxxx.HtmlParser;
import static org.junit.Assert.*;
#RunWith(AndroidJUnit4.class)
#SdkSuppress(minSdkVersion=16)
#SmallTest
public class HtmlParserTest {
#Test
public void TestGetHtml() {
// assertion goes here
}
#Test
public void TestGetPlainText() {
// assertion goes here
}
}
I don't know if it was because I did something wrong. Eventually I got it work and I'll post the solution here for your reference.
As metioned in my question, this unit test should run on an Android device or an emulator. So the first thing to do is moving it into the androidTest directory. Only in this way will Android Studio recognize it as an instrumentation test. Also remember to update your app's gradle.build as described here. But Android Studio (Mine is v2.1) won't allow you to run this test directly (as least I couldn't). You have to create a test suite which looks like:
package xxxxxxx.htmltestsuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
#RunWith(Suite.class)
#Suite.SuiteClasses({
HtmlParserTest.class
})
// place holder class
public class HtmlTestSuite {}
Today I meet a question that when I do junit test under test directory.When I create a SpannableStringBuilder,for example,
SpannableStringBuilder ssb = new SpannabelStringBuilder(str),
then the value of ssb is always "null". But I remove it under androidTest directory, and extends AndroidTestCase,it effect.
We are trying to use the Robolectric testing framework in Android Studio in order to test the Facebook API. The Facebook Login button works so the Facebook API is working. However, the following test fails:
package com.airportapp.test.Models;
import android.app.Activity;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.Robolectric;
import com.airportapp.test.MyRobolectricTestRunner;
import com.airportapp.LoginActivity;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
#RunWith(MyRobolectricTestRunner.class)
public class LoginActivityTest {
#Before
public void setup() {
//do whatever is necessary before every test
}
#Test
public void testActivityFound() {
Activity activity = Robolectric.buildActivity(LoginActivity.class).create().get();
Assert.assertNotNull(activity);
}
}
And the error is that Android Studio could not find the android.support file when we run the tests. You can see the error here:
The other error that shows up is:
android.view.InflateException: XML file app/src/main/res/layout/activity_login.xml line #-1 (sorry, not yet implemented): Error inflating class com.facebook.widget.LoginButton
So Android Studio is not happy with the facebook login button as well :( But it works... We think that we need to import something, but we don't know where to put it.
The InflateException is because Robolectric cannot find the resources from the Facebook SDK. To solve this, the project.properties file has to be updated to point to the Facebook SDK project. Do this by adding the following line to it:
android.library.reference.1={Path}
{Path} should be a relative path, from the project.properties file to the folder containing the AndroidManifest.xml of the Facebook SDK project. In my case that is ../../build/intermediates/exploded-aar/com.facebook.android/facebook/3.21.0.
Note that this works for all Android Library Projects which contain resources that aren't found by Robolectric. Further reading: here and here. Also, more documentation about project.properties can be found here. Note that in my project the Robolectric tests are located in the Android app project itself. So you could also try placing the project.properties file in the same directory as the AndroidManifest.xml used for testing.
As for your first problem; I have no personal experience with it. But it appears to be because Gradle cannot find the support libraries when compiling the unit tests. The answer from this question should fix it.
I tried a example provided in the, Android app testing through Selenium, i have included the selenium-java library and android-webdriver apk also installed in emulator, but when try with the sample code provide in the forum i got error in AnroidWebDriver import, in selenium library only AndroidDriver class is available, so where could i get the AdroidWebDriver jar. Plz assit.
Note: Selenium library is very latest one.
import android.test.ActivityInstrumentationTestCase2;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidWebDriver;
import simple.app.SimpleAppActivity;
public class SimpleGoogleTest extends ActivityInstrumentationTestCase2<SimpleAppActivity> {
private WebDriver driver;
private WebDriver googledriver;
public SimpleGoogleTest() {
super("simple.app", SimpleAppActivity.class);
}
#Override
protected void setUp() throws Exception {
driver = new AndroidWebDriver(getActivity());
}
........................,,,,,
}
There are two variations of the Android Driver:
AndroidDriver() which you use on your Personal Computer e.g. your
laptop, workstation, etc. which provides the richest and most
complete set of capabilities for your tests.
AndroidWebDriver() which runs on your Android device, this wraps a WebView component to provide the basic core functionality.
The example code that comes with the Android SDK and the optional support for Selenium/WebDriver runs some basic tests on the device. The tests are compiled as an Android program which extend ActivityInstrumentationTestCase2. AndroidWebDriver() is contained in sdk/extras/google/webdriver/android_webdriver_library.jar (and the Javadocs are in sdk/extras/google/webdriver/android_webdriver_library-srcs.jar
So, if you want to run your tests on your Android device, then you need to include android-webdriver-library.jar in your project. Perhaps the simplest way is to copy this jar into your test project's libs folder.
However, if you would like to run your tests on your Personal Computer you can modify the example code to use AndroidDriver instead of AndroidWebDriver. You also need to change your base class e.g. to use Junit 3 or Junit 4. I have posted a sample test as an answer to another question on Stack Overflow here Having difficulty in finding Elements using Xpath and CSS in Selenium Android Webdriver Testing
I am trying use the GPE code for App-Android Android-App communication. Both the two new created folders (Android, AppEngine) have errors. I think the problem is that there are
errors with the packages:
import com.google.web.bindery.requestfactory.shared.InstanceRequest;
import com.google.web.bindery.requestfactory.shared.Request;
import com.google.web.bindery.requestfactory.shared.RequestContext;
import com.google.web.bindery.requestfactory.shared.RequestFactory;
import com.google.web.bindery.requestfactory.shared.ServiceName;
It says "The import com.google.web cannot be resolved." I tried adding everything able to be checked checked in the build path. I also know I installed the Web Toolkit and everything. What is wrong?
Statements like the following also have error markings:
#ProxyForName(value = "com.glm.server.Message",
locator = "com.glm.server.MessageLocator") and actually there are so many errors it must be the imports.