我正在尝试使用 android-room 对我的 DAO 进行单元测试。我已经编写了一个可以正常工作的插入测试。不幸的是,删除方法似乎不起作用。
我尝试了几种不同的测试设置。没有一个有效。
这是 DAO:
@Dao
public interface MonthlyDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
void saveAll(List<Monthly> goals);
@Insert(onConflict = OnConflictStrategy.REPLACE)
void save(Monthly goal);
@Update
void update(Monthly goal);
@Delete
void delete(Monthly goal);
@Query("SELECT * FROM Monthly")
LiveData<List<Monthly>> findAll();
@Query("SELECT * FROM monthly")
List<Monthly> findAllList();
}
这是每月实体:
@Entity
public class Monthly {
@PrimaryKey(autoGenerate = true)
private int monthlyId;
@TypeConverters(CalendarTypeConverter.class)
@ColumnInfo(name = "date")
private Calendar date = Calendar.getInstance();
@ColumnInfo(name = "title")
private String title;
@ColumnInfo(name = "description")
private String description;
@ColumnInfo(name = "completed")
private boolean completed;
...
public int getMonthlyId() {
return monthlyId;
}
public void setMonthlyId(int monthlyId) {
this.monthlyId = monthlyId;
}
这是我正在运行的测试:
@RunWith(AndroidJUnit4.class)
public class MonthlyTest {
private MonthlyDao monthlyDao;
private MonthlyGoalsDatabase db;
@Before
public void createDb() {
Context context = ApplicationProvider.getApplicationContext();
db = Room.inMemoryDatabaseBuilder(context, MonthlyGoalsDatabase.class).build();
monthlyDao = db.getMonthlyDao();
}
@After
public void closeDb() throws IOException {
db.close();
}
我将 UpdatedGoals 列表排除为空,但事实并非如此。仍然有我在测试时插入的目标。
眼眸繁星
相关分类