mercredi 14 décembre 2016

How to use 2 different argument captor classes in the same test

My main class contains 1 int type and 4 strings. I want to test an insert function of a database.Trying the code below it keeps on failing "junit.framework.AssertionFailedError". Switching my data types in the original class into 5 strings and testing it, works great.So I figured it may be due to the presence of 2 different ArgumentCaptor classes.

@Test
public void HappyTest2() throws SQLException, DAOException{
    when(conn.prepareStatement(anyString())).thenReturn(psmt);
    ArgumentCaptor<Integer> integercaptor = ArgumentCaptor.forClass(Integer.class);
    ArgumentCaptor<String> Stringcaptor = ArgumentCaptor.forClass(String.class);

    Product p = new Product(1);
    p.setType("myType");
    p.setManufacturer("myManufacturer");
    p.setProductionDate("myProductionDate");
    p.setExpiryDate("myExpiryDate");

    testingDAO.insertProduct(p);
    verify(psmt, times(1)).setInt(anyInt(), integercaptor.capture());
    Assert.assertTrue(integercaptor.getAllValues().get(0).equals(1));
    verify(psmt, times(4)).setString(anyInt(), Stringcaptor.capture());
    Assert.assertTrue(Stringcaptor.getAllValues().get(1).equals("myType"));
    Assert.assertTrue(Stringcaptor.getAllValues().get(2).equals("myManufacturer"));
    Assert.assertTrue(Stringcaptor.getAllValues().get(3).equals("myProductionDate"));
    Assert.assertTrue(Stringcaptor.getAllValues().get(4).equals("myExpiryDate"));
}

Aucun commentaire:

Enregistrer un commentaire