【发布时间】: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