1

I don't have much experience on android JUnit test. Here I'm doing a simple test application for JUnit.The application contains two activity when the button pressed in the first activity Intent use to start the second activity. But I'm getting two failures as junit.framework.AssertionFailedError. This is the code that I have tried:

public void testLayout(){
        buttonId=com.example.androidunittest.R.id.button;
        assertNotNull(activity.findViewById(buttonId));
        Button view=(Button) activity.findViewById(buttonId);
        assertEquals("Incorrect label of the button","Start",view.getText());
    }

    public void testIntentTriggerViaOnClick(){
        buttonId=com.example.androidunittest.R.id.button;
        Button view=(Button) activity.findViewById(buttonId);
        assertNotNull("Button not allowed to be null",view);
            view.performClick();
            Intent triggeredIntent=getStartedActivityIntent();
        assertNotNull("Intent was null",triggeredIntent);
        String data=triggeredIntent.getExtras().getString("URL");

        assertEquals("Incorrect data passed via the intent","http://www.google.com", data);
}

The failure line are assertNotNull("Button not allowed to be null",view); and assertNotNull(activity.findViewById(buttonId)); according to the JUnit test panel result.

Could anyone please explain me what is happening here? Also how to fix the error in the application code?

I have another question, how to run all test class once in eclipse? What I did is as per this answer , right click on the project -> Run as-> Android JUnit test. But only active class was gave the result.

Any help would be appreciated.

Community
  • 1
  • 1
Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
  • If you are following that SO answer, your Test method needs to be annotated with @Test. – Mukus Apr 08 '14 at 06:42
  • So do I have to use @Test for all methods in test classes to run all of them once? – Zusee Weekin Apr 08 '14 at 06:45
  • I believe with JUnit version 3 if you extended the TestCase class it would run all your test methods provided they also followed some sort of convention like public void testSomething(). But with Junit 4 (I think that's what you are using) you do not extend that class and annotate your test methods with @Test. – Mukus Apr 08 '14 at 06:57
  • I'm following this tutorial http://www.vogella.com/tutorials/AndroidTesting/article.html. I did not use @Test for any method. when I tried to use it, it gave me an error. This is for android test case. I extend android.test.ActivityUnitTestCase – Zusee Weekin Apr 08 '14 at 07:02

0 Answers0