我在 Android 测试包中有一个测试类。有一个类,我在其中创建了一个对象。但是这个测试类的其他方法不能使用这个对象,也不能识别那个方法的结果。为什么以及我应该做什么?我也用了 static 但不能...
@RunWith(AndroidJUnit4.class)
public class PatientDaoTest {
private static int newRowId;
public static PatientRecordEntity newPatient1;
public void generationRecord(){
newRowId = 0;
PatientRecordEntity newPatient1 = new PatientRecordEntity();
newPatient1.setPatient_db_ID("23456");
newPatient1.setPatient_race("Chines");
newRowId = (int) patientDao.addNewPatient(newPatient1);
newPatient1.setPid(newRowId);
}
@Test
public void addNewPatient() throws Exception {
boolean pin = false;
if (0 != newRowId) {
pin = true;
}
assertTrue("addNewPatient is not true", pin);
}
最佳答案
使用注解@Before
。
喜欢:
public class HTest {
public static Integer i;
@Before
public void before(){
i = 10;
}
@Test
public void print() {
System.out.println(i);
}
}
before
方法将在 print
和 i
被初始化之前执行。
关于java - 在测试类的其他方法中使用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56624225/