Issue with unit test is that the same collection is processed differently in a stream and in a for loop.
The collection in both cases is empty (data.size() = 0)
, but in the Case 1 that collection is somehow processed, in other words it will step in the for-loop.
In Case 2, that collection is just skipped (which is expected since it's empty).
Tests are using Mockito, and Result<Record>
is comming for JOOQ.
The tests are old and unchanged, the only change is going from for-loop to stream.
Case 1
private SearchResult iterateData(
Result<Record> data, ...) {
for (Record record : data) {
doSomething(record);
}
Case 2
private SearchResult iterateData(
Result<Record> data, ...) {
data.stream().forEach(record -> doSomething(record));
Screenshot of Case 1 for loop example
Mocked Result object
private DefaultSearchRequestModel rowSpecificValuesTestSetup(
parameters...) {
DefaultSearchRequestModel searchRequest = new DefaultSearchRequestModel(
Arrays.asList(....),
Collections.singletonList(
new SearchFilter(
"test",
Collections.singletonList(...)));
List<Column> columns =
this.searchService.filterUserAllowedColumns(...);
Condition searchCondition =
this.searchRepositoryMock.getSearchConditions(...);
List<TableJoinMapping> joinMappings = ColumnHelper.getColumnTranslateDeviceJoinMappings(
columns,
searchRequest.getFilters());
Result<Record> deviceDataResultMock = Mockito.mock(Result.class);
Iterator<Record> resultIterator = Mockito.mock(Iterator.class);
final Table fromTableMock = Mockito.mock(Table.class);
when(resultIterator.hasNext()).thenReturn(true, false);
Record recordMock = Mockito.mock(Record.class);
when(resultIterator.next()).thenReturn(recordMock);
when(deviceDataResultMock.iterator()).thenReturn(resultIterator);
when(recordMock.get(CONTRACTID)).thenReturn(contractId);
...
when(this.userPermissions.getAccessPermissions()).thenReturn(searchRequest.getColumns().stream().map
(name -> Column.findByName(name).getId()).collect(
Collectors.toList()));
when(this.searchRepositoryMock.getCurrentTable(companyId))
.thenReturn(fromTableMock);
when(recordMock.get(TYPEID)).thenReturn(financialTypeId);
when(this.searchRepositoryMock.getDeviceData(
ArgumentMatchers.anyList(),
ArgumentMatchers.anyList(),
any(),
any(),
eq(searchRequest.getPageSize()),
eq(searchRequest.getPage()),
eq(searchRequest.getSortCriterias()),
eq(fromTableMock),
ArgumentMatchers.anyList(),
eq(Optional.empty()),
eq(this.dslContextMock)))
.thenReturn(deviceDataResultMock);
return searchRequest;
}```