I am getting an error while performing the database operation.
In my project when I try to access deleteAll()
from Dao class ie.e,AuthenticationDao
of Room Database
from my test cases I am getting below error
Refer to the below code:
@Dao
public interface AuthenticationDao {
@Insert
void insert(Authentication authentication);
@Query("delete from authentication")
void deleteAll();
@Query("select * from authentication")
Authentication getAuthInformation();
}
AppDatabase.java
@Database(entities = {Authentication.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,
"my-database")
.allowMainThreadQueries()
.build();
}
return INSTANCE;
}
public abstract AuthenticationDao authenticationDao();
}
What is the issue?
Is it because I am using AppDatabase dbInstance = Room.databaseBuilder(context,AppDatabase.class,"my-database")
.allowMainThreadQueries()
.build();
to initialize my database object?
If I use AppDatabase dbInstance = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDatabase.class).allowMainThreadQueries().build();
then it works fine.
What could be the reason?