You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring服务中使用Dozer,如何通过JUnit和Mockito向被测服务注入DozerBeanMapper?

这问题我熟,用JUnit 4 + Mockito来注入DozerBeanMapper其实很简单,主要有两种常用方式,我给你一步步讲清楚:

方式一:使用Mockito注解自动注入

这是最简洁的方式,利用Mockito的@Mock和@InjectMocks注解自动完成依赖注入,非常适合常规测试场景。

首先要给测试类加上@RunWith(MockitoJUnitRunner.class),告诉JUnit用Mockito的运行器来处理注解逻辑。完整代码示例如下:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.mockito.Mockito.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

@RunWith(MockitoJUnitRunner.class)
public class UnicornServiceTest {

    // 生成DozerBeanMapper的Mock实例,替代真实的Dozer对象
    @Mock
    private DozerBeanMapper mockDozer;

    // 自动将标注了@Mock的依赖注入到UnicornService实例中
    @InjectMocks
    private UnicornService unicornService;

    @Test
    public void convert_ShouldCallDozerMapMethod() {
        // 1. 准备测试数据
        Unicorn testUnicorn = new Unicorn();
        testUnicorn.setId(1L);
        testUnicorn.setName("Rainbow");

        UnicornDto expectedDto = new UnicornDto();
        expectedDto.setId(1L);
        expectedDto.setName("Rainbow");

        // 2. Stub Mock对象的方法:指定调用map时返回预期的Dto
        when(mockDozer.map(testUnicorn, UnicornDto.class)).thenReturn(expectedDto);

        // 3. 调用被测服务的方法
        UnicornDto actualDto = unicornService.convert(testUnicorn);

        // 4. 验证结果和行为
        assertThat(actualDto, is(expectedDto));
        verify(mockDozer, times(1)).map(testUnicorn, UnicornDto.class);
    }
}

关键注解说明:

  • @Mock:创建一个Mock版的DozerBeanMapper,完全隔离真实Dozer的配置和逻辑,让测试只关注服务本身的代码。
  • @InjectMocks:自动创建UnicornService的实例,并把所有@Mock标记的依赖注入进去(这里就是通过构造函数注入mockDozer)。

方式二:手动构造注入(适合新手理解原理)

如果你不想依赖Mockito的注解,也可以完全手动控制依赖注入的过程,逻辑更直观:

import org.junit.Test;
import static org.mockito.Mockito.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class UnicornServiceManualTest {

    @Test
    public void convert_ShouldReturnMappedDto() {
        // 1. 手动创建Dozer的Mock实例
        DozerBeanMapper mockDozer = mock(DozerBeanMapper.class);

        // 2. 通过构造函数手动注入到被测服务
        UnicornService unicornService = new UnicornService(mockDozer);

        // 3. 准备测试数据并Stub Mock方法
        Unicorn testUnicorn = new Unicorn();
        UnicornDto expectedDto = new UnicornDto();
        when(mockDozer.map(testUnicorn, UnicornDto.class)).thenReturn(expectedDto);

        // 4. 调用方法并验证结果
        UnicornDto actualDto = unicornService.convert(testUnicorn);
        assertThat(actualDto, is(expectedDto));
        verify(mockDozer).map(testUnicorn, UnicornDto.class);
    }
}

这种方式没有任何注解依赖,能清晰看到依赖注入的整个流程,适合刚接触Mockito的开发者理解原理。

小提示

  • 记得导入正确的静态方法(Mockito和Hamcrest的),这样才能直接使用when、verify、assertThat这些简洁的语法。
  • 用Mock替代真实Dozer后,测试不需要加载任何Dozer配置文件,速度更快且不受外部环境影响。

内容的提问来源于stack exchange,提问作者naXa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:23:22