我有問題斷言由LocalDateTime.now()方法產生的時間,我想斷言整個查詢,而忽略它產生的時間。 下面是代碼:如何斷言查詢,同時忽略由LocalDateTime.now()產生的時間差異?
@Test
public void getLastDrawResult_lotteryIdPresent() {
Optional<Long> lotteryId = Optional.of(3L);
Optional<String> name = Optional.empty();
Optional<String> byName = Optional.empty();
Optional<String> byDate = Optional.empty();
Optional<Boolean> jackpotOnly = Optional.empty();
String query = QUERY_MAIN_BLOCK.replace("/*statement0*/", LocalDateTime.now().plusMinutes(buyingLock).toString()) + CONDITION_SEARCH_BY_LAST_DRAW_RESULT;
String updatedQuery = query.replace("/*statement1*/", " WHERE d.lottery_info_id = 3") + " AND d.lottery_info_id = 3";
DrawResultDto drawResultDto = DrawResultDto.builder().id(3L).build();
List<DrawResultDto> expectedDrawResultDto = singletonList(drawResultDto);
when(jdbcTemplate.query(updatedQuery, drawResultDtoQueryBuilder.queryMapper)).thenReturn(expectedDrawResultDto);
List<DrawResultDto> actualDrawResultDto = drawResultDtoQueryBuilder.getLastDrawResult(lotteryId, name, byName, byDate, jackpotOnly);
verify(jdbcTemplate).query(updatedQuery, drawResultDtoQueryBuilder.queryMapper);
assertEquals(expectedDrawResultDto, actualDrawResultDto);
這裏是比較失敗的細節,這樣你就可以有更多的想法是什麼,我需要忽略:Comparison Failure Window Screenshot.
這裏是我的測試方法:
public List<DrawResultDto> getLastDrawResult(Optional<Long> lotteryId, Optional<String> name, Optional<String> byName, Optional<String> byDate, Optional<Boolean> jackpotOnly) {
String query = QUERY_MAIN_BLOCK.replace("/*statement0*/", LocalDateTime.now().plusMinutes(buyingLock).toString()) +
CONDITION_SEARCH_BY_LAST_DRAW_RESULT;
StringBuilder builder = new StringBuilder();
if (lotteryId.isPresent()) {
builder.append(query.replace("/*statement1*/", " WHERE d.lottery_info_id = " + lotteryId.get()))
.append(String.format(" AND d.lottery_info_id = %d", lotteryId.get()));
} else {
builder.append(query);
name.ifPresent(nameValue -> builder.append(" AND lower(li.name) LIKE '%").append(SqlUtils.escapeLike(nameValue).toLowerCase()).append("%' "));
jackpotOnly.ifPresent(jackpotOnlyValue -> {
if (jackpotOnlyValue) {
builder.append(" AND ").append(CONDITION_SEARCH_BY_JACKPOT_WIN);
}
});
byName.ifPresent(s -> builder.append(" ORDER BY li.name ").append(SqlUtils.escapeLike(s)));
byDate.ifPresent(s -> builder.append(" ORDER BY nfd.nearestDrawDate ").append(SqlUtils.escapeLike(s)));
if (!byName.isPresent() && !byDate.isPresent()){
builder.append(" ORDER BY li.name desc ");
}
}
return jdbcTemplate.query(builder.toString(), queryMapper);
}
我試圖做匹配()從Mockito,沒有運氣。我似乎無法得到正則表達式的工作。喜歡匹配一切,但以下正則表達式:(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3})
。選擇我需要忽略的部分。
最好不要對我正在測試的類/方法做任何更改。 – Amiko