我有DAO类和测试方法。但它有断言错误,我认为测试方法addGroup
中的原因,但不确定。
对象返回:1个名称!!! 2015年7月16日星期四00:00:00策展人!!!
但是当我尝试获取值时它有null
预期:1
实际:0
@RunWith(MockitoJUnitRunner.Silent.class)
public class GroupDAOTest {
@Mock
private DataSource mockDataSource;
@Mock
private Connection connectionMock;
@Mock
private PreparedStatement mockPreparedStatement;
@Mock
private ResultSet mockResultSet;
@Mock
private Group groupMock;
@Mock
private DataSource dataSourceMock;
private GroupDao groupDao;
public GroupDAOTest() {
}
@Test
public void addGroup() {
// GIVEN:
try {
when(dataSourceMock.getConnection()).thenReturn(connectionMock);
groupDao = new GroupDao(dataSourceMock);
SimpleDateFormat format = new SimpleDateFormat();
format.applyPattern("dd-MM-yyyy");
Date date = format.parse("16-07-2015");
Timestamp timestamp = new Timestamp(date.getTime());
int id = 1;
String name = "name!!!";
String curator = "curator!!!";
when(groupMock.getId()).thenReturn(id);
when(groupMock.getName()).thenReturn(name);
when(groupMock.getStartDate()).thenReturn(date);
when(groupMock.getCurator()).thenReturn(curator);
when(connectionMock.prepareStatement(anyString())).thenReturn(mockPreparedStatement);
// WHEN:
groupDao.add(groupMock);
// THEN
// Assert.assertEquals(connectionMock, verify(dataSourceMock, times(2)).getConnection());
verify(connectionMock, times(1)).prepareStatement(anyString());
verify(connectionMock, times(1)).close();
Assert.assertEquals(id, verify(groupMock, times(1)).getId());
Assert.assertEquals(name, verify(groupMock, times(1)).getName());
Assert.assertEquals(date, verify(groupMock, times(1)).getStartDate());
Assert.assertEquals(curator, verify(groupMock, times(1)).getCurator());
verify(mockPreparedStatement, times(1)).setInt(1, id);
verify(mockPreparedStatement, times(1)).setString(2, name);
verify(mockPreparedStatement, times(1)).setTimestamp(3, timestamp);
verify(mockPreparedStatement, times(1)).setString(4, curator);
verify(mockPreparedStatement, times(1)).executeUpdate();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
}