I have an activity A that launch an activity B.
I'd like to have a robotium project to test my app so I'v created a first test class for activity A and all goes well.
I'd like now to create another test class for testing Activity B but it require some init from activity A.
I tried this:
BTestClass extends ActivityInstrumentationTestCase2 {
private Solo solo;
private ATestClass testA;
#Override
protected void setUp() throws Exception {
Log.i(TAG, "setUp");
solo = new Solo(getInstrumentation(), getActivity());
testA = new ATestClass();
testA.setUp();
testA.testAddAccount();
solo.clickInList(0);
}
[… more test method]
}
I got a NullPointerException when testA is doing getActivity()
I do it this way:
public class BTest extends ActivityInstrumentationTestCase2<SplashScreenActivity> {
protected static final String TARGET_PACKAGE_ID = "app.under.test";
protected Solo solo;
public BTest() {
super(TARGET_PACKAGE_ID, StartingActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
// setup stuff
}
#Override
public void tearDown() throws Exception {
// teardown stuff
super.tearDown();
}
}
All testcases just inherit from BTest then
public class OneTest extends BTest {
public void test_OneTest() {
//test stuff
solo.clickOnButton("Ok");
}
}
Related
I am developing a test class for my application. The test class code looks like this:
public class ProfileActivityTest extends ActivityInstrumentationTestCase2<ProfileActivity> {
ProfileActivity profileActivity;
#SuppressLint("NewApi")
public ProfileActivityTest(Class<ProfileActivity> activityClass) {
super(activityClass);
// TODO Auto-generated constructor stub
}
#SuppressLint("NewApi")
public ProfileActivityTest() {
super(ProfileActivity.class);
}
protected void setup() throws Exception {
super.setUp();
profileActivity = getActivity();
}
public void test_profileActivityLoggingIn() {
assertNotNull(profileActivity);
assertEquals(View.GONE, profileActivity.findViewById(R.id.btnAddOrEdit).getVisibility());
}
protected void tearDown() throws Exception {
super.tearDown();
}
}
The problem is that I get assertionFailure on assertNotNull(profileActivity) though profileActivity is instantiated in setup(). I don't understand what I am doing wrong;
As #njzk2 has mentioned, "setup" should be changed to "setUp".
My app has only one activity and based on many fragments. How I can test this fragment in a right way? Give me an example, please.
Cause I try this test class:
#LargeTest
public class ActivityTest extends ActivityInstrumentationTestCase2<ActivityEx> {
public ActivityTest() {
super(ActivityEx.class);
}
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testTest() {
//simple example
assertEquals(true, true);
}
}
And in result I've failed due to ClassCastException.
Rather do it like this:
public class ActivityTest extends android.test.ActivityInstrumentationTestCase2
{
public ActivityTest()
{
super(ActivityEx.class);
}
#Override
protected void setUp() throws Exception
{
super.setUp();
getActivity();
}
public void testTest() {
//simple example
assertEquals(true, true);
}
}
If you interested I also posted a tutorial on testing fragments http://www.stevenmarkford.com/testing-fragments-with-android-espresso-basic-example/
I have been able to run an android junit test with one test method successfully, but when more than one test method are involved, it just runs the first test and after tearDown, the activity does not relaunch for the subsequent tests. As a result, all my test methods fail, save the first one.
On debugging, I noticed that setUp method launches the MainActivity successfully before running the first testMethod, but on being revisited before the start of second testMethod, the same activity does not get relaunched. The code is as below:
package PACKAGE.test;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class Login extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "*.*.MainActivity";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
#SuppressWarnings("unchecked")
public Login() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
#Test
public void testLoginScreen() {
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(2);
}
#Test
public void testSearch(){
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(1);
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
}
You may need to #Override your teardown method
#Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
I am running UIAutomation for android using Robotium and ActivityInstrumentationTestCase2. I have a test suite with 5 tests.
Sometimes my test randomly crash because a test starts, once the previous test has not ended yet.
Is there a way to avoid this? is it possible to manually add a 10 second delay before every test to get away from this horrible annoying bug?
EDIT:
public class MyTest<T extends RoboActivity> extends ActivityInstrumentationTestCase2<T>
{
protected Solo solo;
#Override
protected void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
#Override
protected void tearDown() throws Exception {
solo.finishOpenedActivities();
try {
solo.finalize();
}
catch (Throwable e) {
Assert.fail(e.getMessage()+ e.toString());
e.printStackTrace();
}
super.tearDown();
}
}
Maybe this could work :
mSolo = new Solo(getInstrumentation(), getActivity());
mSolo.waitForActivity(AccountDetail.class);
Seems that waitFor* methods are managing that better than a "sleep"
http://robotium.googlecode.com/svn/doc/com/robotium/solo/Solo.html#waitForActivity(java.lang.Class, int)
My tests's construction, teardown, etc. are slightly different and works well (see below). I had to add a sleep to work around some non-deterministic test-failures.
public class AccountDetailTest extends ActivityInstrumentationTestCase2<AccountDetail> {
private Solo solo;
public AccountDetailTest() {
super("com.bigcorp.breadmaker", AccountDetail.class);
}
#Override
public void setUp() {
solo = new Solo(getInstrumentation(), getActivity());
solo.sleep(500); //wait for activity to initialize
}
#SmallTest
public void testDummy() {
assertNotNull(solo);
}
#Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}
}
I have a simple HelloWorld Activity that I try to test with an Android JUnit test. The application itself runs like it should but the test fails with an
"java.lang.RuntimeException: Unable to resolve activity for: Intent { action=android.intent.action.MAIN flags=0x10000000 comp={no.helloworld.HelloWorld/no.helloworld.HelloWorld} }
at no.helloworld.test.HelloWorldTestcase.setUp(HelloWorldTestcase.java:21)"
This is my activity class:
package no.helloworld;
import android.app.Activity; import
android.os.Bundle;
public class HelloWorld extends
Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
} }
And the test:
public class HelloWorldTestcase extends ActivityInstrumentationTestCase2 {
private HelloWorld myActivity;
private TextView mView;
private String resourceString;
public HelloWorldTestcase() {
super("no.helloworld.HelloWorld", HelloWorld.class);
}
#Override
protected void setUp() throws Exception {
super.setUp();
myActivity = this.getActivity();
mView = (TextView) myActivity.findViewById(no.helloworld.R.id.txt1);
resourceString = myActivity
.getString(no.helloworld.R.string.helloworld);
}
public void testPreconditions() {
assertNotNull(mView);
}
public void testText() {
assertEquals(resourceString, (String) mView.getText());
}
protected void tearDown() throws Exception {
super.tearDown();
}
Why does the test fail? The activity is (of course) defined in AndroidManifest.xml and the application runs as it should.
The package in the constructor call should match the instrumentation target in the manifest. It should be "no.helloworld" instead of "no.helloworld.HelloWorld"