Friday, July 30, 2010

[android-developers] Static variables cleared in JUnit tests! (???)

Hello,

I have this test-code:
=========================
package somepackage;

import android.test.ActivityInstrumentationTestCase2;
import android.util.Log;

import com.kronos.mobile.android.Constants;
import com.kronos.mobile.android.ExceptionsSummaryActivity;
import com.kronos.mobile.android.test.utils.KronosSolo;

public class ExceptionsSummaryActivityTest extends
ActivityInstrumentationTestCase2<ExceptionsSummaryActivity> {
private static String USER = "tturner";

public ExceptionsSummaryActivityTest() {
super("somepackage", ExceptionsSummaryActivity.class);
}

@Override
protected void setUp() throws Exception {
super.setUp();

Log.v(Constants.LOGTAG, "Setup for "+getName()+" "+USER);
}

@Override
protected void tearDown() throws Exception {
super.tearDown();
}

public void test010ResetData() throws Exception {
Log.v(Constants.LOGTAG, "Testcase for "+getName()+" "+USER);
}

public void test020LoadDataFromHome() throws Exception {
Log.v(Constants.LOGTAG, "Testcase for "+getName()+" "+USER);
}

public void test030UIElements() {
Log.v(Constants.LOGTAG, "Testcase for "+getName()+" "+USER);
}
}
===================
The JUnit is a separate Unit Test application (apk), running another
APK (the one under test).

When I run the above test-code (all the code is shown, nothing has
been left out), the value of USER changes magically from "tturner" to
'null'. In test010ResetData and the setup of this test, the value of
USER is "tturner". In the other test-method (and their setup() calls),
the value of USER is null.
Ouput:
Setup for test010ResetData tturner
Testcase for test010ResetData tturner
Setup for test020LoadDataFromHome null
Testcase for test020LoadDataFromHome null
Setup for test030UIElements null
Testcase for test030UIElements null


When i change this line to (adding 'final)'
private static final String USER = "tturner";
the value of USER remains "tturner" for all test-methods!
Output:
Setup for test010ResetData tturner
Testcase for test010ResetData tturner
Setup for test020LoadDataFromHome tturner
Testcase for test020LoadDataFromHome tturner
Setup for test030UIElements tturner
Testcase for test030UIElements tturner

When i then change that line to (changing String to Object, still
using 'final'):
private static final String USER = "tturner";
the value of USER is again 'null' in test020 and test030!!
Ouput:
Setup for test010ResetData tturner
Testcase for test010ResetData tturner
Setup for test020LoadDataFromHome null
Testcase for test020LoadDataFromHome null
Setup for test030UIElements null
Testcase for test030UIElements null


My question is:
Why and how are static variables (e.g. USER) of JUnit test-cases set
to null?
Is this correct behavior.

I tried in on a G1 running 1.6 and on a Nexus1 running 2.2. They both
have the same behavior.

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment