如何为通过Spring注入的mapstruct抽象映射器编写Junit测试 [英] How to write Junit test for mapstruct abstract mapper injected via Spring

查看:999
本文介绍了如何为通过Spring注入的mapstruct抽象映射器编写Junit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是MapStruct,mapstruct-jdk8版本1.1.0.最后定义了我通过Spring注入的抽象类.

I'm using MapStruct, mapstruct-jdk8 version 1.1.0.Final and defining abstract class that I inject via Spring.

我正在研究如何通过Junit Test对其进行测试? 我基本上已经有了一个主映射器,它将使用2个子映射器

I'm looking at how to be able to test them via Junit Test ? I've basicaly a main mapper that will use 2 sub mappers

@Mapper(componentModel = "spring", uses = {SubMapper1.class, SubMapper2.class})
public abstract class MainMapper {

  @Mapping(target = "field1", qualifiedByName = {"MyMapper2Name", "toEntity"})
  public abstract MyEntity toEntity(MyDto pDto);

  public MyDto fromEntity(MyEntity pEntity) {
     // Specific code, hence why I use Abstract class instead of interface. 
  }
}

我已经尝试了几种方法,但是无法正确实例化该映射器以对其进行测试.

I've tried several things but can't get the mapper to be instancied correctly to test it.

@RunWith(SpringRunner.class)
public class MainMapperTest {

    private MainMapper service = Mappers.getMapper(MainMapper.class);

    @Test
    public void testToEntity() throws Exception {
.....

java.lang.RuntimeException:java.lang.ClassNotFoundException:找不到com.mappers.MainMapper的实现

java.lang.RuntimeException: java.lang.ClassNotFoundException: Cannot find implementation for com.mappers.MainMapper

我也尝试通过@InjectMock尝试,但也没有骰子.

I've also tried via @InjectMock but no dice either.

无法实例化名为服务"的@InjectMocks字段.你还没有 在字段声明中提供了实例,所以我尝试构造 实例.但是,我失败了,因为:类型'MainMapper是 一个抽象类.

Cannot instantiate @InjectMocks field named 'service'. You haven't provided the instance at field declaration so I tried to construct the instance. However, I failed because: the type 'MainMapper is an abstract class.

并通过Spring @Autowired

And via Spring @Autowired

起因: org.springframework.beans.factory.NoSuchBeanDefinitionException:否 可用类型为"com.mappers.MainMapper"的合格bean:预期 至少1个符合自动装配候选条件的bean.相依性 注释: {@ org.springframework.beans.factory.annotation.Autowired(required = true)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.mappers.MainMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

我猜想这可能与注释处理器有关,并且在我启动测试时未生成映射器. 我发现

I'm guessing this might have to do with annotation processor, and mapper not being generated when I launch test. I found this class as example.

但是在1.2之前,该类AnnotationProcessorTestRunner似乎不可用,而1.2尚未发布最终版本.

However the class AnnotationProcessorTestRunner doesn't seems to be available before 1.2 which has no final release yet.

所以我的问题是如何编写Junit测试来测试我的代码中通过Spring注入使用的mapstruct抽象类映射器.

So my question is how do I write Junit tests to test my mapstruct abstract class mapper that I use via Spring injection in my code.

推荐答案

在回应@Richard Lewan的评论时,这是我如何使用2个subMappers声明抽象类ConfigurationMapper的测试类

In response to @Richard Lewan comment here is how I declared my test class for the abstract class ConfigurationMapper using 2 subMappers

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {ConfigurationMapperImpl.class, SubMapper1Impl.class, SubMapper2Impl.class})
public class ConfigurationMapperTest {

您可以在SpringBootTest批注中使用Impl生成的类,然后注入要测试的类:

You use the Impl generated classes in the SpringBootTest annotation and then inject the class you want to test:

@Autowired
private ConfigurationMapper configurationMapper;

如果您需要更多信息,请告诉我,但是从那里开始很简单. 我没有嘲笑subMapper,因为最好一次测试所有映射过程.

Let me know if you need more info, but from there it's straightforward. I didn't mock the subMapper, as it was better for me to test all the mapping process at once.

这篇关于如何为通过Spring注入的mapstruct抽象映射器编写Junit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆