我正在尝试将 mapstruct 与 Gradle 一起使用,但取得的成功有限。当我在应用程序中使用它时,一切似乎都工作正常,但是当我尝试编写一些测试时,Spring 无法正确自动装配 MapStruct (它只返回 NullPointer 异常)。我正在使用 Gradle 5.4.1、Junit5 和 IntelliJ 2019.1.2。
这是构建文件夹,没有为测试类生成映射器。

包含代码的存储库位于: https://github.com/MirkoManojlovic/mapstruct-example
@Mapper(unmappedTargetPolicy = ReportingPolicy.WARN,
componentModel = "spring",
injectionStrategy = InjectionStrategy.CONSTRUCTOR)
public interface ItemMapper {
ItemDto toDto(Item item);
Item toItem(ItemDto itemDto);
}
Run Code Online (Sandbox Code Playgroud)
public class ItemRepository {
public ItemDto getItemDto() {
return new ItemDto("item 1");
}
public Item getItem() {
return new Item(1, "item 1", 20);
}
}
Run Code Online (Sandbox Code Playgroud)
@Service
@RequiredArgsConstructor
@Log4j2
public class ItemService {
private final ItemRepository itemRepository;
private final ItemMapper …Run Code Online (Sandbox Code Playgroud)