“我有一个会议室数据库,它可以正常工作,我现在想对其进行测试,但是我的测试无法正常工作,运行时也看不到任何断言。 在这段代码中,我添加了一条新记录并对其进行了初始化,然后断言是否添加了记录”
@RunWith(AndroidJUnit4.class)
public class PatientDaoTest {
private PatientDao patientDao;
private PatientDatabase db;
public int patient_db_ID;
public int bed_number;
@Before
public void createDb(){
Context context = ApplicationProvider.getApplicationContext();
db = Room.inMemoryDatabaseBuilder(context, PatientDatabase.class).buil();
patientDao = db.patientDao();}
@After
public void closeDb() throws IOException {
db.close();
}
@Test
public void InsertNewPatient() throws Exception {
PatientRecordEntity newPatient = new PatientRecordEntity();
newPatient.setPatient_db_ID("123456");
newPatient.setPatient_race("chines");
newPatient.setBed_number("123");
newPatient.setReal_bed_number("1234");
newPatient.setRegister_date("5/28/2019");
newPatient.setRegister_time("3:35:02");
newPatient.setPatient_age(12);
newPatient.setPatient_gender(1);
boolean pin = false;
int newRowId = 0;
newRowId = (int) db.patientDao().addNewPatient(newPatient);
if(newRowId == 0){
pin= true;
}
assertTrue("test is true",pin);
}}
“这是我的道查询:”
@Insert
long addNewPatient(PatientRecordEntity newPatient);
”“这是使用dao的查询的函数。如果我的@test为true,则我不反对”
public static int AddPatientToDB() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String formatedDate = sdf.format(new Date());
sdf = new SimpleDateFormat("HH-mm-ssZ", Locale.getDefault());
String formatedTime = sdf.format(new Date());
PatientRecordEntity newPatient = new PatientRecordEntity();
newPatient.setBed_number(bed_no);
newPatient.setPatient_db_ID(patient_id);
newPatient.setRegister_time(formatedTime);
newPatient.setRegister_date(formatedDate);
int newRowId = 0;
newRowId = (int) pDatabase.patientDao().addNewPatient(newPatient);
current_patient_record_entity = newPatient;
current_patient_record_entity.pid = newRowId;
current_patient_record_entity.sessionID = Repo.currentSessionID;
current_wound_record_entity = new WoundRecordEntity();
current_wound_record_entity.patient_key = (int) newRowId;
current_sample_no = newRowId;
return newRowId;
}
答案 0 :(得分:0)
您有3种选择进行测试
1)在菜单中->运行->选择测试而不是应用
2)Alt + Shift + F10。它将打开运行菜单
3)在此菜单中单击您的测试
如果要运行your_project的所有测试,请右键单击“ test”或“ androidTest”文件夹,然后选择“在your_project”中运行测试
答案 1 :(得分:0)
“感谢...根据您的指南,测试是正确的,我只是修改了断言:”
boolean pin = false;
int newRowId = 0;
newRowId = (int) patientDao.addNewPatient(newPatient);
newPatient.setPid(newRowId);
if(0 != newRowId){
pin= true;}
assertTrue("addNewPatient is not true",pin);
“并且测试通过了:))”