【问题标题】:Is there any way to unit test a Mapstruct with nested Mapper?有没有办法用嵌套的 Mapper 对 Mapstruct 进行单元测试?
【发布时间】:2022-01-24 10:27:32
【问题描述】:

我正在尝试对 Mapstruct 嵌套映射器进行单元测试,如下所示:

@Mapper(componentModel = "spring", uses = EventCategoryMapper.class, injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface EventMapper {

    
    Event fromEventMO(EventMO eventMO);

    EventMO toEventMO(Event event);

    default Optional<Event> fromOptionalEventMO(Optional<EventMO> optionalEventMO) {
        
        return (optionalEventMO.isEmpty()) ? Optional.empty() : Optional.of(fromEventMO(optionalEventMO.get()));
        
    }
    
}
@Mapper(componentModel = "spring", injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface EventCategoryMapper {

    
    EventCategory fromEventCategoryMO(EventCategoryMO eventCategoryMO);

    EventCategoryMO toEventCategoryMO(EventCategory eventCategory);

    default String fromPriorityMO(PriorityMO priority) {
        return (priority.getPriority()==null) ? null : priority.getPriority();
    }

我正在尝试测试 EventMapper:

@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {EventMapper.class, EventCategoryMapper.class, EventMapperImpl.class, EventCategoryMapperImpl.class})
public class EventMapperTest {

    private Mocks mocks; //This object contains the mocked objects that should be mapped.

    @Autowired
    private EventMapper eventMapper;

    @Test
    @DisplayName("Should return an Event from an EventMO")
    void shouldReturnEventfromEventMO() {
        
        var event = eventMapper.fromEventMO(mocks.getEventMO());

        assertEquals(event.getId(), 123L);


    }

但它总是失败:

创建名为“eventMapper”的 bean 时出错:bean 的实例化 失败的;嵌套异常是 org.springframework.beans.BeanInstantiationException:失败 实例化 [com.mycompany.cna.projects.fishmarket.back.events.repositories.mappers.event.EventMapper]: 指定的类是一个接口

我已经尝试使用 Mapper.getMapper(EventMapper.class) 实例化映射器,它返回了 NullPointerException。

我应该怎么做才能对这类映射器进行单元测试?

【问题讨论】:

    标签: java spring spring-boot unit-testing mapstruct


    【解决方案1】:

    我已经解决了这个问题。问题是我没有实例化我的 Mocks 对象。我还在 before 方法中为 EventMapperImpl 提供了嵌套映射器的模拟:

        @ExtendWith(SpringExtension.class)
    public class EventMapperTest {
    
        private Mocks mocks;
    
        private EventMapper eventMapper;
    
        @Mock
        private EventCategoryMapper eventCategoryMapper;
    
        @BeforeEach
        void before() {
            eventMapper = new EventMapperImpl(eventCategoryMapper);
            mocks = new Mocks();
        }
        
        @Test
        @DisplayName("Should return an Event from an EventMO")
        void shouldReturnEventfromEventMO() {
    
            when(eventCategoryMapper.fromEventCategoryMO(any(EventCategoryMO.class))).thenReturn(mocks.getEventCategory());
            var event = eventMapper.fromEventMO(mocks.getEventMO());
    
            assertEquals(event.getId(), 123L);
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-23
      • 2010-11-13
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多