Android : 如何确保在 运行 Instrumentation 测试中的大量测试之前清除 sharedPreference

Android : How to make sure that sharedPreference get clear before running a large nos of test in Instrumentation testing

我有一个包含登录页面的应用程序。在此登录页面中,我将用户凭据存储在共享首选项中,如果用户已经登录,它将直接进入初始屏幕,然后进入仪表板。但是在 android 仪器测试的情况下。我的测试用例 运行s 仅一次,因为第一次用户凭据未存储在共享首选项中。一旦凭据存储在共享首选项中,它将在测试开始时自动将测试用例重定向到启动画面。因此我的测试用例失败了。

我想在每次 运行 登录测试用例之前清除我的应用共享偏好。

我尝试使用 getInstrumentation().getContext() 清除我的共享偏好以获取上下文。但它似乎不起作用。

我必须编写多个测试用例,其中我必须 运行 在同一个 class 中至少有 15 个测试用例。所以对于每个@test,我都需要首先清除共享首选项。

下面是我的登录测试用例的代码:

import android.app.Instrumentation;
import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Button;
import android.widget.EditText;

import androidx.test.espresso.action.ViewActions;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import salesken.app.R;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
import static junit.framework.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)

public class LoginActivityTest {

    @Rule
    public ActivityTestRule < LoginActivity > loginActivityTestRule = new ActivityTestRule < LoginActivity > (LoginActivity.class);

    private LoginActivity loginActivity = null;

    private String email_input = "dummy@user.com";
    private String password_input = "password1";
    private int timeout = 5000;
    private SharedPreferences sharedPreferences;

    Instrumentation.ActivityMonitor monitor = getInstrumentation().addMonitor(SplashScreenActivity.class.getName(), null, false);

    @Before
    public void setUp() throws Exception {
        loginActivity = loginActivityTestRule.getActivity();
        clearsharedPref();

    }

    @After
    public void tearDown() throws Exception {
        loginActivity = null;
    }

    @Test
    public void autenticationcheck() {
        clearsharedPref();

        EditText email = loginActivity.findViewById(R.id.email);
        EditText password = loginActivity.findViewById(R.id.password);
        Button login = loginActivity.findViewById(R.id.login);
        email.setText(email_input);
        password.setText(password_input);
        onView(withId(R.id.login)).perform(ViewActions.click());
        SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
        assertNotNull(splashScreenActivity);
        splashScreenActivity.finish();
    }

    @Test
    public void emptyEmail() {
        clearsharedPref();
        EditText email = loginActivity.findViewById(R.id.email);
        EditText password = loginActivity.findViewById(R.id.password);
        Button login = loginActivity.findViewById(R.id.login);
        email.setText("");
        password.setText(password_input);
        onView(withId(R.id.login)).perform(ViewActions.click());
        SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
        assertNull(splashScreenActivity);
    }

    @Test
    public void emptyPassword() {
        clearsharedPref();
        EditText email = loginActivity.findViewById(R.id.email);
        EditText password = loginActivity.findViewById(R.id.password);
        Button login = loginActivity.findViewById(R.id.login);
        email.setText("");
        password.setText(password_input);
        onView(withId(R.id.login)).perform(ViewActions.click());
        SplashScreenActivity splashScreenActivity = (SplashScreenActivity) getInstrumentation().waitForMonitorWithTimeout(monitor, timeout);
        assertNull(splashScreenActivity);
    }

    public void clearsharedPref() {
        Context context = getInstrumentation().getTargetContext().getApplicationContext();
        sharedPreferences = context.getSharedPreferences("SaleskenProComplexKey", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.clear();
        editor.commit();
    }

}

您可以通过

全部清除
Context context = getInstrumentation().getContext();
SharedPreferences preferences = context.getSharedPreferences("mysharedpref", 
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear(); 
editor.commit();

清除SharedPrferences应该就够了,如果不够,可以删除它而不是清除:

String filePath = getApplicationContext().getFilesDir().getParent()+"/shared_prefs/prefs_name.xml";
    File deletePrefFile = new File(filePath);
     deletePrefFile.delete();

如果我无法通过简单的方式获取应用程序的上下文,我会使用:

public class ApplicationContext extends Application {

    private static ApplicationContext INSTANCE;

    @Override
    public void onCreate() {
        super.onCreate();
        INSTANCE = this;
    }

    public static ApplicationContext getInstance() {
        return INSTANCE;
    }
}