Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中的junit-删除测试会更改其他测试的结果_Android_Junit_Android Testing_Junit3 - Fatal编程技术网

android中的junit-删除测试会更改其他测试的结果

android中的junit-删除测试会更改其他测试的结果,android,junit,android-testing,junit3,Android,Junit,Android Testing,Junit3,我只需右键单击该项目,并在android框架下作为Junit测试运行—该项目有3个文件 基类 import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.preference.PreferenceManager; import android.test.AndroidTestCase

我只需右键单击该项目,并在android框架下作为Junit测试运行—该项目有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);
    }
}
完全是阿尔法,所以不要开枪

我的

不用说,它花了一整天的时间才把它减少到这3个文件

如果我有testPutNullBoolean:

如果我删除它:

其中,故障跟踪显示:

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-我不明白的是为什么测试是相关的。测试新手,所以可能有一个明显的错误,但我现在看到它真的很头晕:

嗯,我是一个noob:

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();
    }
    //...
}
所以仅仅将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会有所帮助-很遗憾

@ρаσѕρєK:正如我在问题中所说的那样,我不介意投掷——我不明白为什么测试是相关的
//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();
}