0

我有一个扩展 AbstractService 的 TaskService。

在 abstractService 中,我有一个方法 getCurrentUserRoleEntity 由服务中的不同方法使用。此方法使用存储库来查找当前角色。

在我的任务服务中,我有一个方法来查找当前用户角色和类别的任务,这个方法有 2 个参数,caegoryId 和方法 getCurrentUserRoleEntity。

public List<Task> findTaskByCategoryIdForCurrentUserRole(Long categoryId) {
    return vTacheAffaireRepository.findTaskByCategoryIdForCurrentUserRoleOrderByEcheanceAscTacheIdAsc(categoryId, getCurrentUserRoleEntity().getId())
        .stream().map(vTaskMapper::toDto).collect(Collectors.toList());
}

当我测试调用服务的其余资源时,我想模拟 getCurrentUserRoleEntity 以使用我的测试实体而不是使用真实方法。

我尝试了不同的方法,但没有一个能满足我的需求。您知道在其余测试期间如何模拟此方法吗?

谢谢,

4

2 回答 2

0

非常简单的方法是,您可以为 getCurrentUserRoleEntity() 方法创建一个 setter 和 getter,您可以通过它插入您的模拟对象实例而不是真实的实例。

于 2021-01-05T14:40:15.910 回答
-1

尝试在测试中覆盖该方法,因为您的方法是公共的,您可以定义一个名为的新类 TestTaskService

TestTaskService extends TaskService {
@Override
public List<Task> findTaskByCategoryIdForCurrentUserRole(Long categoryId) {
//provide mock implementation 
}

} 
于 2021-01-05T14:44:42.943 回答