我一直在使用一个名为Diff的工具为某些类生成测试用例。现在,Diff是一个AI工具,可以为某些功能生成多个测试用例。我有一个使用Diff生成测试用例的特定类,但是有一个测试用例失败了。我的主要目标是让所有的测试用例都通过。我正在处理的测试用例是用于具有函数getDashboardById的下面类。
@RequiredArgsConstructor
@Transactional
@Service
@Slf4j
public class DashboardServiceImpl implements DashboardService {
private final DashboardRepository dashboardRepository;
private final PlatformDataExtractHistoryService platformDataExtractHistoryService;
private final ShopifyService shopifyService;
private final ModelMapper modelMapper;
/**
* Get Dashboard by its id
*
* @param id dashboard id
* @return DashboardGetDto
*/
@Override
public DashboardGetDto getDashboardById(String id) {
Dashboard dashboard = dashboardRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException(GlobalErrorUtils.DASHBOARD_NOT_FOUND_404));
log.info("Get dashboard details by id: {}", id);
// convert entity to DTO
return modelMapper.map(dashboard, DashboardGetDto.class);
}Diffblue为函数getDashboardById生成了3个测试用例。两个测试用例通过,另一个失败。我的目标是让它也通过。下面的代码是失败测试用例的代码。
void testGetDashboardById3() {
DashboardGetDto dashboardGetDto = new DashboardGetDto();
when(dashboardRepository.findById((String) any())).thenReturn(Optional.empty());
when(modelMapper.map((Object) any(), any())).thenReturn("Map");
when(modelMapper.map((Object) any(), (Class<DashboardGetDto>) any())).thenReturn(dashboardGetDto);
assertThrows(ResourceNotFoundException.class, () -> dashboardServiceImpl.getDashboardById("42"));
verify(dashboardRepository).findById((String) any());
verify(modelMapper).map((Object) any(), (Class<Object>) any());
}我遇到的问题是,我得到了一个类强制转换异常。
java.lang.ClassCastException: class java.lang.String cannot be cast to class com.unicorn.dashboard.dto.dashboard.DashboardGetDto (java.lang.String is in module java.base of loader 'bootstrap'; com.unicorn.dashboard.dto.dashboard.DashboardGetDto is in unnamed module of loader 'app')
at com.unicorn.dashboard.serviceImpl.DashboardServiceImplTest.testGetDashboardById3(DashboardServiceImplTest.java:127)现在我明白了,当您尝试将一种类型的类转换到另一种类型时,就会发生这种情况。但是,我很难理解的是,错误是在哪里生成的,以及如何解决它,因为这是一个测试用例。在上述测试函数testGetDashboardById3中的第127行的错误点。作为参考,我会给出下面这一行
when(modelMapper.map((Object) any(), (Class<DashboardGetDto>) any())).thenReturn(dashboardGetDto);现在我不明白的是,在上面的行中,到底在哪里发生了转换,为了使测试用例正常工作,必须更改什么?
编辑
我已经按照答案编辑了我的代码,并显示在下面。但是,代码中有一个错误。要修复错误,必须做哪些必要的更改?

当我悬停在突出显示的区域上时,我会收到以下消息

发布于 2022-10-24 11:40:11
when(modelMapper.map((Object) any(), any())).thenReturn("Map");匹配任何第二个参数。您希望模拟方法 D map(Object source, Class destinationType),在您的生产代码中将其称为map(input, String.class)和map(input, DashboardGetDto.class)。因此,您希望将此方法设置为:
when(modelMapper.map(any(), eq(String.class)))
.thenReturn("Map");
when(modelMapper.map(any(), eq(DashboardGetDto.class)))
.thenReturn(dashboardGetDto);若要选择正确的重载,请执行以下操作。您的测试是匹配调用,比如map(input, new DashboardGetDto()),它返回void。
请注意,ArgumentMatchers#any()已经是通用的,因此不必强制转换返回值。您可以使用ArgumentMatchers.<Object>any()或any(Object.class)。
需要注意的是,Java运行时不能区分Class<X>和Class<Y> (或List<A>和List<B>)。对于JVM,两者都是Class (或List )类型。您的第一个when调用已经匹配了对模拟的所有调用。
https://stackoverflow.com/questions/74180454
复制相似问题