vendredi 12 janvier 2018

Inject private field to tested method

Simple question, I have a class with have a List field with Items, I have a method operating on that List, checking if there is a Item with a name given in parameter and returning true or false. I would like to test that method, creating an ArrayList in test class, mocking JdbcItemDao class, and calling a method. Method in test cannot reach an array, how to fix it? Code:

     public class JdbcItemDao {
     private List<Item> tempStockList;

    public JdbcItemDao() {

    this.tempStockList = getAllStockItems();

   //getting items from my sql, returning them as ArrayList

}

  public boolean checkStockItems(String itemName) {
    for (Item item : tempStockList) {
        if (item.getItemName().equalsIgnoreCase(itemName)) {
            return true;
        }
    }
    return false;
}}

Test class:

public class JdbcTest {


JdbcItemDao jdbcItemDao;
List<Item> tempStockList;

@Before
public void setup() {

    jdbcItemDao = mock(JdbcItemDao.class);
    tempStockList = new ArrayList<>();
    tempStockList.add(new Item(1, "LEDTv", new BigInteger("40"),
            new Integer("3"), new BigInteger("70")));

}


@Test
public void checkStockItemsName() throws Exception {

   assertTrue(jdbcItemDao.checkStockItems("LEDTv"));

}}

Aucun commentaire:

Enregistrer un commentaire