Junit对Spring Boot应用程序进行测试。
有人对我如何使用JUnit和Mockito实现这一目标有任何建议吗?
@Autowired
JdbcTemplate jdbcTemplate;
public List<Student> getStudentDetails(String department) {
List<Student> results = new LinkedList<String>();
results = jdbcTemplate.query("SELECT * FROM STUDENT WHERE DEPARTMENT = ?", new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement preparedStatement) throws SQLException {
preparedStatement.setString(1, department);
preparedStatement.setFetchSize(10);
}
}, new ResultSetExtractor<List<Student>>() {
@Override
public List<Student> extractData(ResultSet rs) throws SQLException {
List<Student> students = new ArrayList<>();
while (rs.next()) {
Student student = new Student<>();
student.setDepartment(rs.getString("NAME"));
student.setName(rs.getString("DEPARTMENT"));
students.add(student);
}
return students;
}
});
return results
}
答案 0 :(得分:0)
您拥有的代码与数据库有关,我认为应该使用某些数据库进行测试。通常的做法是使用嵌入式数据库(例如h2)。人们会这样做,因为使用单元测试无法检查查询是否真的有效,因为您实际上并未运行它。因此,我将结合集成和单元测试来测试此类。
单元测试将是这样的:
data <- data.frame(a = c("2018-06-01 09:00:17",
"2018-06-01 20:31:54",
"2018-06-01 20:32:49",
"2018-06-01 20:34:45",
"2018-06-02 12:28:27",
"2018-06-04 22:01:58",
"2018-06-05 01:31:34",
"2018-06-05 01:31:46",
"2018-06-05 01:33:03",
"2018-06-05 01:33:03",
"2018-06-05 01:33:12"),
b = c(0, 0, 91, 0, 522, 0, 0, 501, 0, 501, 501),
c = c(511, 250, 250, 250, 501, 511, 501, 501, 501, 501, 501),
d = c(0, 522, 0, 559, 200, 0, 501, 0, 501, 501,0),
DateShift = c(NA,
"2018-06-01 09:00:17",
"2018-06-01 20:31:54",
"2018-06-01 20:32:49",
"2018-06-01 20:34:45",
"2018-06-02 12:28:27",
"2018-06-04 22:01:58",
"2018-06-05 01:31:34",
"2018-06-05 01:31:46",
"2018-06-05 01:33:03",
"2018-06-05 01:33:03"),
Wait = c(1, 1, 1, 1, 1, 1, 0, 12, 0, 0, 9),
stringsAsFactors = FALSE)
data
这里,我只是使用我们发送到@ExtendWith(MockitoExtension.class)
class StubTest {
@Mock
JdbcTemplate jdbcTemplate;
@InjectMocks
Stub stub;
@Test
void whenExecuteQuery_thenExtractDataCorrectly() throws SQLException {
//GIVEN
ArgumentCaptor<PreparedStatementSetter> setterCaptor = ArgumentCaptor.forClass(PreparedStatementSetter.class);
ArgumentCaptor<ResultSetExtractor> extractorCaptor = ArgumentCaptor.forClass(ResultSetExtractor.class);
//WHEN
stub.getStudentDetails("TEST");
//THEN
verify(jdbcTemplate).query(anyString(), setterCaptor.capture(), extractorCaptor.capture());
//AND
PreparedStatementSetter setter = setterCaptor.getValue();
PreparedStatement preparedStatement = Mockito.mock(PreparedStatement.class);
setter.setValues(preparedStatement);
verify(preparedStatement).setString(1, "TEST");
verify(preparedStatement).setFetchSize(10);
verifyNoMoreInteractions(preparedStatement);
//AND
ResultSetExtractor extractor = extractorCaptor.getValue();
ResultSet rs = Mockito.mock(ResultSet.class);
when(rs.next()).thenReturn(true).thenReturn(false);
when(rs.getString(anyString())).thenReturn("TEST","name");
verifyNoMoreInteractions(rs);
List<Student> students = (List<Student>) extractor.extractData(rs);
assertThat(students.get(0).getName()).isEqualTo("name");
assertThat(students.get(0).getDepartment()).isEqualTo("TEST");
}
}
方法的业务逻辑来捕获参数。然后,我运行参数的重写方法并验证它们是否按预期工作。