android中的junit - 删除测试会更改不同测试的结果

时间:2013-10-20 17:02:32

标签: android junit android-testing junit3

我只是右键单击该项目并使用android框架作为Junit Test运行 - 该项目有3个文件

基础课程

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.test.AndroidTestCase;

public class AccessPreferencesTest extends AndroidTestCase {

    static Context ctx;
    static SharedPreferences prefs;
    Editor e;
    static final boolean DEFAULT_BOOLEAN = true;
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        ctx = getContext();
        prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
        e = prefs.edit();
    }
}

抛出的文件......

public final class AccessPreferencesNullValuesTest extends
        AccessPreferencesTest {

    public void testNullBollean() {
        prefs.getString("BOOLEAN_KEY", "DEFAULT_STRING");
    }
}

...如果我从此文件中删除testPutNullBoolean()

import gr.uoa.di.android.helpers.AccessPreferences;

public final class AccessPreferencesBooleanTest extends AccessPreferencesTest {

    public void testPutNullBoolean() {
        AccessPreferences.put(ctx, "BOOLEAN_KEY", null);
        Boolean b = AccessPreferences.get(ctx, "BOOLEAN_KEY", null);
        assertEquals(null, b);
    }

    public void testPutBoolean() {
        AccessPreferences.put(ctx, "BOOLEAN_KEY", DEFAULT_BOOLEAN);
        boolean b = AccessPreferences.get(ctx, "BOOLEAN_KEY", null);
        assertEquals(DEFAULT_BOOLEAN, b);
    }
}

gr.uoa.di.android.helpers.AccessPreferences(完全是alpha所以不要拍)

我的launcher

毋庸置疑,它需要一整天才能将其减少到这3个文件。

所以,如果我有testPutNullBoolean

enter image description here

如果我删除它:

enter image description here

“失败追踪”的内容为:

java.lang.ClassCastException: java.lang.Boolean
at android.app.ContextImpl$SharedPreferencesImpl.getString(ContextImpl.java:2699)
at gr.uoa.di.android.helpers.test.AccessPreferencesNullValuesTest.testNullBollean(
    AccessPreferencesNullValuesTest.java:7)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(
    InstrumentationTestRunner.java:520)
at android.app.Instrumentation$InstrumentationThread.run(
    Instrumentation.java:1447)

我不介意CCE(这也是一个很简单的测试版本+一个WIP) - 我没有得到的是为什么测试是相关的。新的测试可能有一个明显的错误,但我现在看到它真的很头晕:)

1 个答案:

答案 0 :(得分:0)

嗯,我是个菜鸟:

public final class AccessPreferences {

    private static SharedPreferences prefs;

    private static SharedPreferences getPrefs(Context ctx) {
        SharedPreferences result = prefs;
        if (result == null)
            synchronized (AccessPreferences.class) {
                result = prefs;
                if (result == null) {
                    result = prefs = PreferenceManager
                        .getDefaultSharedPreferences(ctx);
                }
            }
        return result;
    }

    public static <T> void put(final Context ctx, final String key,
            final T value) {
        final Editor ed = getPrefs(ctx).edit();
        if (value == null) {
            ed.putString(key, null); // this was called in testPutNullBoolean()
            // **nulling** the boolean I put into prefs with testPutBoolean()
            //  - which run earlier (they run alphabetically (?) - not in the 
            // order they are declared anyway). So prefs.getString() found
            // null and did not throw - whereas if I deleted testPutNullBoolean()
            // it found a Boolean...
        }
        else if (value instanceof Boolean) ed.putBoolean(key, (Boolean) value); 
        //...
        ed.commit();
    }
    //...
}

所以我缺少的是,在setUp() ,返回了相同的ctx

//AccessPreferencesTest

static Context ctx;

@Override
protected void setUp() throws Exception {
    super.setUp();
    ctx = getContext(); // here
    // ...
}

所以将prefs设置为null是不够的 - 我必须明确地清除它们。

//AccessPreferencesTest

static SharedPreferences prefs; Editor e; // set up in setUp()

@Override
protected void setUp() throws Exception {
    super.setUp();
    ctx = getContext();
    prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
    e = prefs.edit();
    }

@Override
protected void tearDown() throws Exception {
    // Field f = AccessPreferences.class.getDeclaredField("prefs");
    // f.setAccessible(true);
    // // f.set(null, null); // NO USE
    // final SharedPreferences sp = (SharedPreferences) f.get(null);
    // if (sp != null) sp.edit().clear().commit();
    // I only have to clear the preferences via an editor - the
    // SharedPreferences are a singleton in the context of a single Context
    // so no need to access them via AccessPreferences and no need to
    // nullify the field in AccessPreferences [ie call f.set(null, null)] -
    // as the reference to the singleton stays valid - apparently
    if (ed != null) ed.clear().commit();
    super.tearDown();
}

如果getContext()清楚地说明返回了相同的ctx会很有帮助 - 不幸的是there are no docs at all