有什么方法可以用嵌套的 Mapper 对 Mapstruct 进行单元测试吗?
Is there any way to unit test a Mapstruct with nested Mapper?
我正在尝试对 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);
}
但它总是失败:
Error creating bean with name 'eventMapper': Instantiation of bean
failed; nested exception is
org.springframework.beans.BeanInstantiationException: Failed to
instantiate
[com.mycompany.cna.projects.fishmarket.back.events.repositories.mappers.event.EventMapper]:
Specified class is an interface
我已经尝试使用 Mapper.getMapper(EventMapper.class) 实例化映射器,但它返回了 NullPointerException。
我应该怎么做才能对这些类型的映射器进行单元测试?
我已经解决了这个问题。问题是我没有实例化我的 Mocks 对象。我还在之前的方法中为 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);
}
我正在尝试对 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);
}
但它总是失败:
Error creating bean with name 'eventMapper': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.mycompany.cna.projects.fishmarket.back.events.repositories.mappers.event.EventMapper]: Specified class is an interface
我已经尝试使用 Mapper.getMapper(EventMapper.class) 实例化映射器,但它返回了 NullPointerException。
我应该怎么做才能对这些类型的映射器进行单元测试?
我已经解决了这个问题。问题是我没有实例化我的 Mocks 对象。我还在之前的方法中为 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);
}