如何将 modelMapper 模拟为函数?

发布于 2025-01-14 19:08:40 字数 2328 浏览 0 评论 0原文

我正在为我的服务中的一个方法编写单元测试,并且正在努力处理它的返回。

这是我的服务:

@Slf4j
@Service
public class CategoryService {

private final PaginationGenerator paginationGenerator;
private final ModelMapper modelMapper;

@Autowired
public CategoryService(final PaginationGenerator paginationGenerator, final ModelMapper modelMapper){
        this.paginationGenerator = paginationGenerator;
        this.modelMapper = modelMapper;
}

public PageableResponse<CategoryDto> getAll(final Integer page, final Integer pageSize, String idCategory, String name) {
        final Pageable pageRequest = this.paginationGenerator.pageRequest(page, pageSize);
        final Page<Category> categoryPage = getCategoriesByParams(idCategory, name, pageRequest);
        return this.paginationGenerator.paginatedResponse(categoryPage, category -> modelMapper.map(category, CategoryDto.class));
    }
}

这个categoryPage是通过getCategoriesByParams方法正确实现的。 当它尝试转换并返回 PageableResponse 时会出现问题,因为此 modelMapper 无法工作。

这是我的单元测试

@ExtendWith(MockitoExtension.class)
@ActiveProfiles("test")
class CategoryServiceTest {

    @InjectMocks
    private CategoryService categoryService;
    @Mock
    private ModelMapper mockedModelMapper;
    @Mock
    private PaginationGenerator mockedPaginationGenerator;

    @Test
    void getAll() {
        Page<Category> categoryPage = new PageImpl<>(generateListOfCategories());
        Pageable pageable = Pageable.ofSize(50);

        when(mockedPaginationGenerator.pageRequest(0, 50)).thenReturn(pageable);
        when(categoryRepository.findAll(pageable)).thenReturn(categoryPage);
        //when(mockedPaginationGenerator.paginatedResponse(categoryPage)).thenReturn(gene);
        when(mockedModelMapper.map(Category.class, CategoryDto.class)).thenReturn(generateCategoryDto());

        PageableResponse<CategoryDto> categoriesReturned = categoryService.getAll(0, 50, null, null);

        assertEquals(categoriesReturned.getContent().size(), categoryPage.getContent().size());
        verify(categoryRepository, times(1)).findAll(pageable);

    }
}

这个 modelMapper 作为一个函数很难弄清楚如何测试,我不确定问题是否是模拟 modelMappers 和 paginationGenerator 的方式,或者我只是错过了一些东西。

categoriesReturned 在我的测试中始终返回 null,因此我无法用它进行断言。

我怎样才能进行这个测试?或者还有其他方法来测试它吗?

I'm writing a unit test for a method in my service and I'm strugling to handle its return.

Here's my service:

@Slf4j
@Service
public class CategoryService {

private final PaginationGenerator paginationGenerator;
private final ModelMapper modelMapper;

@Autowired
public CategoryService(final PaginationGenerator paginationGenerator, final ModelMapper modelMapper){
        this.paginationGenerator = paginationGenerator;
        this.modelMapper = modelMapper;
}

public PageableResponse<CategoryDto> getAll(final Integer page, final Integer pageSize, String idCategory, String name) {
        final Pageable pageRequest = this.paginationGenerator.pageRequest(page, pageSize);
        final Page<Category> categoryPage = getCategoriesByParams(idCategory, name, pageRequest);
        return this.paginationGenerator.paginatedResponse(categoryPage, category -> modelMapper.map(category, CategoryDto.class));
    }
}

this categoryPage is fulfilled properly by getCategoriesByParams method.
The problem occurs when it tries to convert and return the PageableResponse, because this modelMapper just won't work.

Here's my unit test

@ExtendWith(MockitoExtension.class)
@ActiveProfiles("test")
class CategoryServiceTest {

    @InjectMocks
    private CategoryService categoryService;
    @Mock
    private ModelMapper mockedModelMapper;
    @Mock
    private PaginationGenerator mockedPaginationGenerator;

    @Test
    void getAll() {
        Page<Category> categoryPage = new PageImpl<>(generateListOfCategories());
        Pageable pageable = Pageable.ofSize(50);

        when(mockedPaginationGenerator.pageRequest(0, 50)).thenReturn(pageable);
        when(categoryRepository.findAll(pageable)).thenReturn(categoryPage);
        //when(mockedPaginationGenerator.paginatedResponse(categoryPage)).thenReturn(gene);
        when(mockedModelMapper.map(Category.class, CategoryDto.class)).thenReturn(generateCategoryDto());

        PageableResponse<CategoryDto> categoriesReturned = categoryService.getAll(0, 50, null, null);

        assertEquals(categoriesReturned.getContent().size(), categoryPage.getContent().size());
        verify(categoryRepository, times(1)).findAll(pageable);

    }
}

This modelMapper as a function is hard to figure out how to test, I'm not sure if the problem is the way of mocking modelMappers and paginationGenerator or I'm just missing something.

categoriesReturned always returns as null in my test, so I can't do the assertions with it.

How could I perform this test? or is there another way of testing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文