Doing some tests with the Room persistance library, but getting some unexpected results from @BeforeClass. The doc states that this will invoke the function only once before all test and is used to init the process. As it turns out, the test function createcurrencies works, but the dummy createcurrencypairs doesn't. Only if i remove the clearcurrencies (from the setup) it works as expected. from my testing it seems that BeforeClass is executed before every test. Any ideas?
@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();
}
}
Based on the feedback I have received, I reworked it and now it works as expected now.
@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();
}
}