@BeforeClass 没有按预期工作
@BeforeClass not working as expected
正在使用 Room 持久性库进行一些测试,但从@BeforeClass 得到了一些意想不到的结果。该文档指出,这将在所有测试之前仅调用该函数一次,并用于初始化该过程。事实证明,测试函数 createcurrencies 有效,但虚拟 createcurrencypairs 无效。只有当我(从设置中)删除 clearcurrencies 时,它才会按预期工作。从我的测试来看,似乎 BeforeClass 是在每次测试之前执行的。有什么想法吗?
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private static myDb dbh = null;
private static Context context = null;
private static Integer n = 100;
@BeforeClass
public static void setUp() throws Exception {
context = InstrumentationRegistry.getTargetContext();
dbh = myDb.getDatabase(context);
dbh.daoCurrency().clearCurrencies();
}
@Test
public void createCurrencies() {
Integer n=100;
for (Integer i=1; i<=n; i++)
{
Currency c = new Currency();
c.setCode("CUR"+i);
dbh.daoCurrency().addCurrencies(c);
}
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
assertEquals((Integer) n, (Integer) currencyList.size());
}
@Test
public void createCurrencyPairs() {
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
assertEquals((Integer) n, (Integer) currencyList.size());
}
@AfterClass
public static void tearDown() throws Exception {
dbh.close();
}
}
根据我收到的反馈,我重新设计了它,现在它可以正常工作了。
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private static myDb dbh = null;
private static Context context = null;
private static Integer n = 100;
@BeforeClass
public static void setUp() throws Exception {
context = InstrumentationRegistry.getTargetContext();
dbh = myDb.getDatabase(context);
}
@Before
public void clear() throws Exception {
dbh.daoCurrency().clearCurrencies();
dbh.daoCurrencyPair().clearCurrencyPairs();
}
public void createCurrencies() {
Integer n=100;
for (Integer i=1; i<=n; i++)
{
Currency c = new Currency();
c.setCode("CUR"+i);
dbh.daoCurrency().addCurrencies(c);
}
}
public void createCurrencyPairs() {
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
for (Currency c1 : currencyList)
{
for (Currency c2 : currencyList)
{
CurrencyPair cp = new CurrencyPair();
cp.setBaseId(c1.getId());
cp.setCounterId(c2.getId());
dbh.daoCurrencyPair().addCurrencyPairs(cp);
}
}
}
@Test
public void testCurrencies() {
createCurrencies();
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
assertEquals((Integer) n, (Integer) currencyList.size());
}
@Test
public void testCurrencyPairs() {
createCurrencies();
createCurrencyPairs();
List<CurrencyPair> currencypairList = dbh.daoCurrencyPair().getCurrencyPairs();
assertEquals((Integer) (n*n), (Integer) currencypairList.size());
}
@AfterClass
public static void tearDown() throws Exception {
dbh.close();
}
}
正在使用 Room 持久性库进行一些测试,但从@BeforeClass 得到了一些意想不到的结果。该文档指出,这将在所有测试之前仅调用该函数一次,并用于初始化该过程。事实证明,测试函数 createcurrencies 有效,但虚拟 createcurrencypairs 无效。只有当我(从设置中)删除 clearcurrencies 时,它才会按预期工作。从我的测试来看,似乎 BeforeClass 是在每次测试之前执行的。有什么想法吗?
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private static myDb dbh = null;
private static Context context = null;
private static Integer n = 100;
@BeforeClass
public static void setUp() throws Exception {
context = InstrumentationRegistry.getTargetContext();
dbh = myDb.getDatabase(context);
dbh.daoCurrency().clearCurrencies();
}
@Test
public void createCurrencies() {
Integer n=100;
for (Integer i=1; i<=n; i++)
{
Currency c = new Currency();
c.setCode("CUR"+i);
dbh.daoCurrency().addCurrencies(c);
}
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
assertEquals((Integer) n, (Integer) currencyList.size());
}
@Test
public void createCurrencyPairs() {
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
assertEquals((Integer) n, (Integer) currencyList.size());
}
@AfterClass
public static void tearDown() throws Exception {
dbh.close();
}
}
根据我收到的反馈,我重新设计了它,现在它可以正常工作了。
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
private static myDb dbh = null;
private static Context context = null;
private static Integer n = 100;
@BeforeClass
public static void setUp() throws Exception {
context = InstrumentationRegistry.getTargetContext();
dbh = myDb.getDatabase(context);
}
@Before
public void clear() throws Exception {
dbh.daoCurrency().clearCurrencies();
dbh.daoCurrencyPair().clearCurrencyPairs();
}
public void createCurrencies() {
Integer n=100;
for (Integer i=1; i<=n; i++)
{
Currency c = new Currency();
c.setCode("CUR"+i);
dbh.daoCurrency().addCurrencies(c);
}
}
public void createCurrencyPairs() {
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
for (Currency c1 : currencyList)
{
for (Currency c2 : currencyList)
{
CurrencyPair cp = new CurrencyPair();
cp.setBaseId(c1.getId());
cp.setCounterId(c2.getId());
dbh.daoCurrencyPair().addCurrencyPairs(cp);
}
}
}
@Test
public void testCurrencies() {
createCurrencies();
List<Currency> currencyList = dbh.daoCurrency().getCurrencies();
assertEquals((Integer) n, (Integer) currencyList.size());
}
@Test
public void testCurrencyPairs() {
createCurrencies();
createCurrencyPairs();
List<CurrencyPair> currencypairList = dbh.daoCurrencyPair().getCurrencyPairs();
assertEquals((Integer) (n*n), (Integer) currencypairList.size());
}
@AfterClass
public static void tearDown() throws Exception {
dbh.close();
}
}