单元测试的问题是相同的集合在流和for循环中被不同的处理。这两种情况下的集合都是空的 (data.size() = 0) ,但在例1中,集合是以某种方式处理的,换句话说,它将执行for-循环。在第2种情况下,该集合被跳过(因为它是空的)。
(data.size() = 0)
测试使用的是Mockito,而 Result<Record> 正在开始使用JOOQ。
Result<Record>
测试是旧的,不变的,唯一的改变是从一个循环到另一个流。
案例1
private SearchResult iterateData( Result<Record> data, ...) { for (Record record : data) { doSomething(record); }
案例2
private SearchResult iterateData( Result<Record> data, ...) { data.stream().forEach(record -> doSomething(record));
案例1 循环示例 的屏幕截图
模拟结果对象
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)))