[英]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
. 代码的设置方式将不起作用,因为
PreparedStatement
的实例是在您要测试的方法中创建的: 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: 第一步是将
PreparedStatement
的创建移到一个单独的类中,然后可以在测试中对其进行模拟:
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
: 接下来,您更新
PersonalDataDao
以使用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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.