My Android unit test never completes in Eclipse - android

When running a unit test (Run as Android JUnit test) it never completes.
Output:
[2011-03-03 21:45:43 - TestMyProj] Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
[2011-03-03 21:45:43 - TestMyProj] Collecting test information
[2011-03-03 21:45:47 - TestMyProj] Sending test information to Eclipse
[2011-03-03 21:45:47 - TestMyProj] Running tests...
...and nothing more.
Code:
public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> {
public MainActivityTest() {
super("my.app.MainActivity", MainActivity.class);
}
private MainActivity mActivity;
#Override
protected void setUp() throws Exception {
super.setUp();
mActivity = this.getActivity();
}
public void testOneEqualsOne() {
assertEquals(1,1);
}
}
What could be wrong?
Kind regards,
Christian

The constructor you are using is deprecated. This may not be the root cause of the problem but it might help.
Use:
public MainActivityTest() {
super(MainActivity.class);
}

Related

Android, Espresso - Activity stops before whole test is finished when run as part of :app:connectedAndroidTest (runs fine in isolation)

I apologise if this is a bit too vague here, but I'm not allowed to post my whole actual code. All I can say is I have a problem running this test as a part of ./gradlew connectedAndroidTest
#RunWith(AndroidJUnit4.class)
#LargeTest
public class MobileAppSanityTest extends AbstractEspressoTest {
#Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ClearPreferencesActivityTestRule<>(MainActivity.class, getFiles());
#Override
protected Context getContext() {
return mActivityRule.getActivity();
}
#BeforeClass
public static void beforeAll() {
RoboGuice.Util.reset();
}
#Test
public void test_SingleUserFlow() {
navigateSplashScreen();
logIn();
doSomethingElse();
}
}
What happens here is that when I run this test class on its own - it runs fine, but when I run it as a part of 'connectedAndroidTest' the activity is stopped right after 'navigateSplashScreen' and login cannot be performed.
Error I get is:
java.lang.RuntimeException: No activities found. Did you t to launch the activity by calling getActivity() or startActivitySync or similar?
I'm quite new to Espresso and Android in general, so it's a bit hard to wrap my head around this. Please let me know if you need more information. I'll try to provide it out if that's the case.
a jUnit TestCase looks differently; think one can only use Espresso in there.
#RunWith(AndroidJUnit4.class)
public class MainActivityTest extends TestCase {
/** Log Tag */
private static final String LOG_TAG = MainActivityTest.class.getSimpleName();
/** the Activity of the Target application */
private MainActivity mActivity;
#Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<MainActivity>(MainActivity.class) {
};
#Override
public void setUp() throws Exception {
super.setUp();
}
/* obtaining the context from the ActivityTestRule */
#Before
public void setUpTest() {
this.mActivity = this.mActivityRule.getActivity();
}
/* add Espresso code eg. here */
#Test
#UiThreadTest
public void navigateSplashScreen() {
}
#Override
public void tearDown() throws Exception {
super.tearDown();
}
}

startActivity are not working in Android Unit Tests

I've tried everything already. The startActivity are not receiving the intent. Here's the code:
public class ColaboradorTeste extends ActivityUnitTestCase<ColaboradorMainActivity> {
private UserDAO userDAO;
private ColaboradorMainActivity activity;
public ColaboradorTeste() {
super(ColaboradorMainActivity.class);
}
#Override
protected void setUp() throws Exception{
super.setUp();
startActivity(new Intent(getInstrumentation().getTargetContext(), ColaboradorMainActivity.class), null, null);
activity = (ColaboradorMainActivity)getActivity().getApplicationContext();
userDAO = new UserDAO(activity);
}
public void testBase() throws Exception{
...
}
}
Here's the error:
java.lang.NullPointerException
at br.fsw.seatafmobile.ColaboradorTeste.setUp(ColaboradorTeste.java:23)
I know the problem is in the startActivity, but I really don't know how to solve it.
If anyone could help I'll really appreciate.
I finally found the solution!
My test is using a database created on the app. So I had to change the Build Variants from Unit Test to Android Instrumentation Tests.

Android Espresso black-box testing

Does anyone try to do black-box testing with Android Espresso?
Could anyone provides me with some simple example?
I had tried some example before, but failed every time!
Example, I had tried this one:
public class ApplicationTest extends ActivityInstrumentationTestCase2
{
private static final String ACTIVITY_CLASSNAME = "com.example.kai_yu.blackboxtest";
private static Class launchActivityClass;
static
{
try
{
launchActivityClass = Class.forName(ACTIVITY_CLASSNAME);
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
public ApplicationTest()
{
super(launchActivityClass);
}
#Test
public void testClick()
{
}
}
But Android Studio said:
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.kai_yu.blackboxtest"
com.example.kai_yu.blackboxtest is applicationId which is another installed application on my phone
Thank you!
Espresso can only run as part of an instrumentation test.
Instrumentation tests can only act upon the app under test ( i.e. the target of the instrumentation ).
UIAutomator might be better for your use case.
https://developer.android.com/tools/testing-support-library/index.html#UIAutomator
In Espresso docs you would find this line:
While it can be used for black-box testing, Espresso's full power is unlocked by those who are familiar with the code base under test."
For that reason Espresso testing is called by gray-box testing.
If you're not familiar with programming in Java or Android, or you want just to write black-box testing in the clearest way as possible try to learn instead of Espresso this framework
Calabash-iOS and Calabash-Android are the underlying low-level libraries that empower the Cucumber tool to run automated functional tests on Android...
Website: https://calaba.sh/
GitHub: https://github.com/calabash
Here would you find how and why to start using this framework:
http://blog.teddyhyde.com/2013/11/04/a-better-way-to-test-android-applications-using-calabash/
#RunWith(AndroidJUnit4.class)
#LargeTest
public class EspressoTest1 extends ActivityInstrumentationTestCase2<MainActivity>{
public EspressoTest1() {
super(MainActivity.class);
}
#Before
public void setUp() throws Exception {
super.setUp();
injectInstrumentation(InstrumentationRegistry.getInstrumentation());
}
#Test
public void test1ChatId() {
getActivity();
onView(withId(R.id.anuja)).check(matches(isDisplayed()));
}
#After public void tearDown() throws Exception {
super.tearDown();
}
}
There are two ways to write Espresso Test case one is as per shown above
The Examples are taken from this blog
http://qaautomated.blogspot.in/p/blog-page.html
Where you can find details of hot to run the espresso test case in detail.

Android - ActivityUnitTestCase - Tests Always Pass

I am using Android Studio to try and test my activity. Here is the basic code:
public class MyActivityTest extends ActivityUnitTestCase<MyActivity> {
public MyActivityTest() {
super(MyActivity.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
}
#SmallTest
public void testSomething() {
Assert.assertNotNull("something is null", null);
}
}
I would expect that this test case fails. Everything I try passes though. This seems like a strange question, but how can I make my test case fail? What am I doing wrong?
I managed to get this working, sort of. I found this on a bug report:
We are in the process of deprecating ActivityUnitTestCase. We recommend to move business logic to a separate class and unit test it with gradle unit test support (mockable android.jar).
So I extended ActivityInstrumentationTestCase2 instead and ran the test as an Instrumentation Test rather than a Unit Test. That worked. Here is basically what I have now:
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
public MyActivityTest() {
super(MyActivity.class);
}
public void testSomething() throws Exception {
//test goes here
Assert.assertEquals(message, expectedObject, actualObject);
}
}
I'm still not sure why I was seeing the behavior I was earlier, but at least I can test now. Here is a screenshot of my Test Build Configuration:

Android JUnit tests not detecting in Robotium

I am trying to run following Android JUnit3 test with robotium:
import android.test.ActivityInstrumentationTestCase2;
import com.package.sample.MyActivityClass;
import com.jayway.android.robotium.solo.Solo;
public class TestSample extends ActivityInstrumentationTestCase2<MyActivityClass> {
private Solo solo;
public TestSample() {
super("com.package.sample", MyActivityClass.class);
}
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
public void clickbutton1() throws Exception{
solo.clickOnButton("abc");
solo.setActivityOrientation(Solo.LANDSCAPE);
assertTrue(solo.searchText("load"));
}
#Override
protected void tearDown() throws Exception{
solo.finishOpenedActivities();
}
}
The JUnit explores shows 0/0 tests runs. The debugger does not hit any code path mentioned here and Console outputs "test run finished"??:
Uploading MyAppTest.apk onto device 'emulator-5554'
Installing MyAppTest.apk...
Success!
Project dependency found, installing: MyApp
Uploading MyApp.apk onto device 'emulator-5554'
Installing MyApp.apk...
Success!
Launching instrumentation android.test.InstrumentationTestRunner on device emulator-5554
Collecting test information
Sending test information to Eclipse
Running tests...
Test run finished
Does anyone has an idea what is wrong? I have followed all steps in robotium tutorials religiously. I know there have been many questions around this but I have take care of all answers.
Also Robotium has similar problem with JUnit4 here.
Yes, I know what is wrong.
In order to run your tests, you need to begin them with test. So your code should be like this:
import android.test.ActivityInstrumentationTestCase2;
import com.package.sample.MyActivityClass;
import com.jayway.android.robotium.solo.Solo;
public class TestSample extends ActivityInstrumentationTestCase2<MyActivityClass> {
private Solo solo;
public TestSample() {
super("com.package.sample", MyActivityClass.class);
}
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
//the test methods MUST begin with test...
public void testClickbutton1() throws Exception{
solo.clickOnButton("abc");
solo.setActivityOrientation(Solo.LANDSCAPE);
assertTrue(solo.searchText("load"));
}
#Override
protected void tearDown() throws Exception{
solo.finishOpenedActivities();
}
}
I had a similar issue awhile ago, and Robotium doesn't have the greatest documentation. I hope this helps you out :)
I did everything what you said, but I still got error with 0/0 test.I had found the solution, which is related to constructors:
// I DELETED THIS CONSTRUCTOR
public TestHomeScreenTest(String pkg, Class<ScreenSlidePagerActivity> activityClass) {
super(pkg, activityClass);
}
//GOOD SOLUTION:
public TestHomeScreenTest() {
super("com.dolphin.homescreen", ScreenSlidePagerActivity.class);
}
and it now WOOORKS ! :)

Categories

Resources