简体   繁体   中英

unit test for insertion method using mockito

I have a problem in the unit test of the insertion method,although i use a mock object ,the data is inserted to the database ,what's wrong in this please ?

here's the source code of the method

public void InsertPersonalData(PersonalData personaldata) throws SQLException, DAOException, ClassNotFoundException {
        try {
            PreparedStatement psmt = MysqlConnection.getPreparedStatement("INSERT INTO personal_data values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
            psmt.setString(1, personaldata.getiD());
            psmt.setString(2, personaldata.getPatientName());
            psmt.setString(3, personaldata.getBirthDate());
            psmt.setString(4, personaldata.getPlaceOfBirth());
            psmt.setString(5, personaldata.getGender());
            psmt.setString(6, personaldata.getNationality());
            psmt.setString(7, personaldata.getCurrentAddress());
            psmt.setString(8, personaldata.getCityAddress());
            psmt.setString(9, personaldata.getMaritalStatus());
            psmt.setString(10, personaldata.getOffspring());
            psmt.setString(11, personaldata.getAgeOfYoungestOffspring());
            psmt.setString(12, personaldata.getWorkNature());
            psmt.setString(13, personaldata.getPhoneNumber());
            psmt.setString(14, personaldata.geteMail());

            psmt.executeUpdate();

        } catch (SQLException e) {
            throw new DAOException(e);

        }

here's how i used mocking

@RunWith(MockitoJUnitRunner.class)
public class PersonalDataDaoTest {

    public PersonalData newpd;

    @Mock
    Connection conn;

    @Mock
    PreparedStatement psmt;

    @InjectMocks
    PersonalDataDao newDAO = new PersonalDataDao();

the test code:

@Test
    public void HappyTestForInsert() throws SQLException, DAOException, ClassNotFoundException {
        when(conn.prepareStatement(anyString())).thenReturn(psmt);
        //ArgumentCaptor<Integer> intcaptor = ArgumentCaptor.forClass(int.class);
        ArgumentCaptor<String> stringcaptor = ArgumentCaptor.forClass(String.class);
        String id = "11";
        String patientName = "yara";
        String birthDate = "2001-12-5";
        String placeOfBirth = "cairo";
        String gender = "female";
        String nationality = "egyptian";
        String currentAddress = "cairo";
        String cityAddress = "cairo";
        String maritalStatus = "married";
        String offspring = "1";
        String ageOfYoungestOffspring = "2";
        String workNature = "teacher";
        String phoneNumber = "0324324234";
        String eMail = "yara@hotmail.com";

        PersonalData p = new PersonalData(id, patientName, birthDate, placeOfBirth, gender, nationality, currentAddress, cityAddress, maritalStatus, offspring, ageOfYoungestOffspring, workNature, phoneNumber, eMail);
        newDAO.InsertPersonalData(p);
        //verify(psmt, times(1)).setInt(anyInt(), intcaptor.capture());
        verify(psmt, times(14)).setString(anyInt(), stringcaptor.capture());
        Assert.assertTrue(stringcaptor.getAllValues().get(0).equals(id));
        Assert.assertTrue(stringcaptor.getAllValues().get(1).equals(patientName));
        Assert.assertTrue(stringcaptor.getAllValues().get(2).equals(birthDate));
        Assert.assertTrue(stringcaptor.getAllValues().get(3).equals(placeOfBirth));
        Assert.assertTrue(stringcaptor.getAllValues().get(4).equals(gender));
        Assert.assertTrue(stringcaptor.getAllValues().get(5).equals(nationality));
        Assert.assertTrue(stringcaptor.getAllValues().get(6).equals(currentAddress));
        Assert.assertTrue(stringcaptor.getAllValues().get(7).equals(cityAddress));
        Assert.assertTrue(stringcaptor.getAllValues().get(8).equals(maritalStatus));
        Assert.assertTrue(stringcaptor.getAllValues().get(9).equals(offspring));
        Assert.assertTrue(stringcaptor.getAllValues().get(10).equals(ageOfYoungestOffspring));
        Assert.assertTrue(stringcaptor.getAllValues().get(11).equals(workNature));
        Assert.assertTrue(stringcaptor.getAllValues().get(12).equals(phoneNumber));
        Assert.assertTrue(stringcaptor.getAllValues().get(13).equals(eMail));

    }

You are trying to verify the usage of the prepared statement in your test. The way your code is set up this will not work, as the instance of the PreparedStatement is created within the method you are testing: InsertPersonalData .

But you can modify your code so that it becomes testable. The first step is to move the creation of the PreparedStatement into a separate class, which then can be mocked in your test:

public class StatementProvider {
    public PreparedStatement createInsertStatementForPersonalData() {
        return PreparedStatement psmt = MysqlConnection.getPreparedStatement("INSERT INTO personal_data values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
    }
}

Next you update your PersonalDataDao to use the StatementProvider :

public class PersonalDataDao {
    private StatementProvider statementProvider;
    // Constructor and other methods ommitted
    public void InsertPersonalData(PersonalData personaldata) throws SQLException, DAOException, ClassNotFoundException {
        try {
            PreparedStatement psmt = statementProvider.createInsertStatementForPersonalData(); 
            psmt.setString(1, personaldata.getiD());
            psmt.setString(2, personaldata.getPatientName());
            psmt.setString(3, personaldata.getBirthDate());
            psmt.setString(4, personaldata.getPlaceOfBirth());
            psmt.setString(5, personaldata.getGender());
            psmt.setString(6, personaldata.getNationality());
            psmt.setString(7, personaldata.getCurrentAddress());
            psmt.setString(8, personaldata.getCityAddress());
            psmt.setString(9, personaldata.getMaritalStatus());
            psmt.setString(10, personaldata.getOffspring());
            psmt.setString(11, personaldata.getAgeOfYoungestOffspring());
            psmt.setString(12, personaldata.getWorkNature());
            psmt.setString(13, personaldata.getPhoneNumber());
            psmt.setString(14, personaldata.geteMail());

            psmt.executeUpdate();

        } catch (SQLException e) {
            throw new DAOException(e);

        }
    } 
}

And then your test looks like this:

@RunWith(MockitoJUnitRunner.class)
public class PersonalDataDaoTest {

    @Mock
    private PreparedStatement psmt;
    @Mock
    private StatmentProvider statementProvider; 

    @InjectMocks
    private PersonalDataDao newDAO; // This is the class under test
    @Test
    public void HappyTestForInsert() throws SQLException, DAOException, ClassNotFoundException {
        when(statementProvider.createInsertStatementForPersonalData()).thenReturn(psmt);
        //ArgumentCaptor<Integer> intcaptor = ArgumentCaptor.forClass(int.class);
        ArgumentCaptor<String> stringcaptor = ArgumentCaptor.forClass(String.class);
        String id = "11";
        String patientName = "yara";
        String birthDate = "2001-12-5";
        String placeOfBirth = "cairo";
        String gender = "female";
        String nationality = "egyptian";
        String currentAddress = "cairo";
        String cityAddress = "cairo";
        String maritalStatus = "married";
        String offspring = "1";
        String ageOfYoungestOffspring = "2";
        String workNature = "teacher";
        String phoneNumber = "0324324234";
        String eMail = "yara@hotmail.com";

        PersonalData p = new PersonalData(id, patientName, birthDate, placeOfBirth, gender, nationality, currentAddress, cityAddress, maritalStatus, offspring, ageOfYoungestOffspring, workNature, phoneNumber, eMail);
        newDAO.InsertPersonalData(p);
        //verify(psmt, times(1)).setInt(anyInt(), intcaptor.capture());
        verify(psmt, times(14)).setString(anyInt(), stringcaptor.capture());
        Assert.assertTrue(stringcaptor.getAllValues().get(0).equals(id));
        Assert.assertTrue(stringcaptor.getAllValues().get(1).equals(patientName));
        Assert.assertTrue(stringcaptor.getAllValues().get(2).equals(birthDate));
        Assert.assertTrue(stringcaptor.getAllValues().get(3).equals(placeOfBirth));
        Assert.assertTrue(stringcaptor.getAllValues().get(4).equals(gender));
        Assert.assertTrue(stringcaptor.getAllValues().get(5).equals(nationality));
        Assert.assertTrue(stringcaptor.getAllValues().get(6).equals(currentAddress));
        Assert.assertTrue(stringcaptor.getAllValues().get(7).equals(cityAddress));
        Assert.assertTrue(stringcaptor.getAllValues().get(8).equals(maritalStatus));
        Assert.assertTrue(stringcaptor.getAllValues().get(9).equals(offspring));
        Assert.assertTrue(stringcaptor.getAllValues().get(10).equals(ageOfYoungestOffspring));
        Assert.assertTrue(stringcaptor.getAllValues().get(11).equals(workNature));
        Assert.assertTrue(stringcaptor.getAllValues().get(12).equals(phoneNumber));
        Assert.assertTrue(stringcaptor.getAllValues().get(13).equals(eMail));

    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM