问题
I am using room database in my app. I have Login feature in my app, where after taking userId & password, on click of Login button
I am calling API and storing the response data in room database table after getting a successful callback response from API.
Now I want to write integration test cases for database data, where I am using mockWebServer to mock the API response and storing that in the room database table.
And later I am fetching the DB values & testing whether those are stored properly or not but I am getting below error
java.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
On this line authentication = authenticationDao.getAuthInformation();
Below is my test cases code:
@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);
}
}
Below is my AppDatabase
class:
@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();
}
What could be the issue? Is my test case right?
Thank you in advance.
来源:https://stackoverflow.com/questions/56619344/getting-error-while-accessing-the-room-database-in-test-cases-android