在测试用例中访问会议室数据库时出错-Android

时间:2019-06-16 13:36:10

标签: testing integration-testing android-room android-instrumentation

我正在我的应用程序中使用会议室数据库。我的应用程序中具有登录功能,在获取用户名和密码后,单击Login button后,我正在从成功获得回调响应后调用API并将响应数据存储在会议室数据库表中 API。

现在,我想编写数据库数据的集成测试用例,在这里我使用 mockWebServer 模拟API响应并将其存储在会议室数据库表中。

然后我获取数据库值并测试这些值是否正确存储,但是我遇到了错误

java.lang.IllegalStateException:无法访问主线程上的数据库,因为它可能会长时间锁定UI。

在这一行上 authentication = authenticationDao.getAuthInformation();

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

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestLogin {

@Rule
public InstantTaskExecutorRule mInstantTaskExecutorRule = new InstantTaskExecutorRule();
@Rule
public ActivityTestRule<LoginActivity> activityTestRule = new ActivityTestRule<>(LoginActivity.class, true, false);
@Rule
public MockWebServerTestRule mockWebServerTestRule = new MockWebServerTestRule();
@Mock
Application application;
LoginViewModel loginViewModel;
AppDatabase appDatabase;
AuthenticationDao authenticationDao;
Authentication authentication;

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    loginViewModel = new LoginViewModel(application);
    ApiUrls.TOKEN = mockWebServerTestRule.mockWebServer.url("/").toString();

    appDatabase = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(),
            AppDatabase.class).build();
    authenticationDao = appDatabase.authenticationDao();

    activityTestRule = new ActivityTestRule<>(LoginActivity.class, true, true);
    String fileName = "valid_login_response.json";
    mockWebServerTestRule.mockWebServer.enqueue(new MockResponse()
            .setBody(RestServiceTestHelper.getStringFromFile(getContext(), fileName))
            .setResponseCode(HttpURLConnection.HTTP_OK));

    Intent intent = new Intent();
    activityTestRule.launchActivity(intent);

    loginViewModel.userName.postValue("Elon");
    loginViewModel.password.postValue("Musk123");
    loginViewModel.getAuthenticateTokenData();
    mockWebServerTestRule.mockWebServer.takeRequest();


}

@Test
public void a_testDbEntryOnValidResponse() {
    authentication = authenticationDao.getAuthInformation();

    String issueTime = authentication.getIssueDateTime();
    String expirationTime = authentication.getExpireDateTime();
    String refreshToken = authentication.getRefreshToken();

    Assert.assertEquals("Tue, 16 Apr 2019 10:39:20 GMT", issueTime);
    Assert.assertEquals("Tue, 16 Apr 2019 10:54:20 GMT", expirationTime);
    Assert.assertEquals("e2b4dfd7205587745aa3100af9a0b", refreshToken);

}
}

下面是我的AppDatabase班:

@Database(entities = {Authentication.class, UserProfile.class}, version = 1, exportSchema = false)

public abstract class AppDatabase extends RoomDatabase {

private static AppDatabase INSTANCE;

public static AppDatabase getAppDatabase(Context context) {
    if (INSTANCE == null) {
        INSTANCE =
                Room.databaseBuilder(context,
                        AppDatabase.class,
                        "myapp-database")
                        .allowMainThreadQueries()
                        .build();
    }
    return INSTANCE;
}

public abstract AuthenticationDao authenticationDao();

public abstract UserProfileDao userProfileDao();
}

可能是什么问题? 我的测试用例正确吗?

谢谢。

0 个答案:

没有答案