如何测试和模拟mapstruct转换器? [英] How to tests and mock mapstruct converter?

查看:243
本文介绍了如何测试和模拟mapstruct转换器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Java gradle项目上使用mapstruct frameword,它可以正常工作,但我只想测试:

I use mapstruct frameword on my java gradle project and it works perfectly but i just want to test :

  • mapstruct生成的源(转换器)
  • 服务类(调用mapstrcut转换器)

我尝试使用其他主题,但这对我不起作用.

I have try to use an other topic to do this but it not working for me.

这是我的mapstruct界面:

This is my mapstruct interface :

@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
public interface RisqueBOConvertisseur extends BOConvertisseur<RisqueARS, RisqueBO> {
    @Override
    RisqueBO convertirDaoVersBo(RisqueARS dao);

    @Override
    RisqueARS convertirBoVersDao(RisqueBO bo);
}

这是我的服务类别:

@服务 公共类ServiceRisqueImpl实现ServiceCRUD {

@Service public class ServiceRisqueImpl implements ServiceCRUD {

@Autowired
private RisqueRepository risqueRepo;

private RisqueBOConvertisseur risqueConv = Mappers.getMapper(RisqueBOConvertisseur.class);

private final String nomObjet = "RisqueARS";

public void setRisqueConv(RisqueBOConvertisseur risqueConv) {
    this.risqueConv = risqueConv;
}

@Autowired
private DossierInternetResource dossierInternet;

@Override
public RisqueBO recupererParId(String id) {
    // Récupère le bloc de la base de données
    final RisqueARS risqueDAO = risqueRepo.findOne(id);

    // Si aucun résultat -> on déclenche une exception
    if (null == risqueDAO) {
        // Déclenche une exception
        throw new ObjectNotFoundException(construireMessageErreur(this.nomObjet, "L'objet risque correspondant à l'id %s, n'existe pas.", id));
    }

    return risqueConv.convertirDaoVersBo(risqueDAO);
}

}

当我尝试测试我的服务时:

When i try to test my service :

@RunWith(MockitoJUnitRunner.class)

@SpringBootTest(classes = {ServiceRisqueImpl.class,RisqueBOConvertisseurImpl.class,RisqueBOConvertisseur.class}) 公共类ServiceRisqueImplTest {

@SpringBootTest(classes = {ServiceRisqueImpl.class, RisqueBOConvertisseurImpl.class, RisqueBOConvertisseur.class}) public class ServiceRisqueImplTest {

@Mock
private RisqueRepository risqueRepo;

@InjectMocks
ServiceRisqueImpl serviceRisque;

@Mock
private DossierInternetResource dossierInternet;

@Mock
private RisqueBOConvertisseur risqueConv;

@Before
public void initMocks() {
    MockitoAnnotations.initMocks(this);
    serviceRisque.setRisqueConv(risqueConv);
}

@Test(expected = ObjectNotFoundException.class)
public void testRecupererParIdQuandIdInconnu() {
    // INITIALISATION
    // Mock la méthode DAO de retour des données en base
    when(risqueRepo.findOne(anyString())).thenReturn(null);

    // PROCESSUS
    serviceRisque.recupererParId("5");
}

junit还给我

However the constructor or the initialization block threw an exception : java.lang.ClassNotFoundException: Cannot find implementation for ***.convertisseur.RisqueBOConvertisseur

我的转换器测试有相同的错误:

I have the same error with my converter test :

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {RisqueBOConvertisseur.class, RisqueBOConvertisseurImpl.class})
public class RisqueBOConvertisseurTest {

    @Autowired
    private RisqueBOConvertisseur configurationMapper;

    private final RisqueBOConvertisseur risqueConv = Mappers.getMapper(RisqueBOConvertisseur.class);

    @Test
    public void test() {
        // INITIALISATION
        final RisqueBO risqueBO = new RisqueBO("950095f7-62e7-42e5-a5ae-0d7292e7ad00", "D1", ProfilEpargnant.PROFIL_EPARGNANT_SECURISE,
                ComportementFaceRisques.REACTION_BAISSE_MARCHE_PANIQUE);

        // PROCESSUS
        // final RisqueARS risqueARS =
        // RisqueBOConvertisseur.INSTANCE.convertirBoVersDao(risqueBO);

        final RisqueARS risqueARS = configurationMapper.convertirBoVersDao(risqueBO);

        // VERIFICATIONS
        assertEquals(risqueBO.getIdRisque(), risqueARS.getIdRisque());
        assertEquals(risqueBO.getIdDossierInternet(), risqueARS.getIdDossierInternet());
        assertEquals(risqueBO.getCodeComportementRisque(), risqueARS.getCodeComportementRisque());
        assertEquals(risqueBO.getCodeProfilEpargnant(), risqueARS.getCodeProfilEpargnant());
    }

}

如何使用mapStruct测试生成的源代码转换器?

How can i test my generated sources converter with mapStruct ?

坦克!

推荐答案

我的策略是

  1. 还要在业务逻辑中模拟映射器,并将其作为单独的组件进行测试. MapStruct可以生成弹簧注释.只需使用@Mapper( componentModel = "spring" )让您的DI框架注入映射器.
  1. mock the mapper as well in your business logic and test it as a separate component. MapStruct can generate spring annotations. Just use @Mapper( componentModel = "spring" ) to let your DI framework inject the mapper.

您的课程看起来像:

@Service public class ServiceRisqueImpl implements ServiceCRUD {

@Autowired
private RisqueRepository risqueRepo;

@Autowired
private RisqueBOConvertisseur risqueConv;

//...

和您对ServiceRisqueImpl

@Mock
private RisqueRepository risqueRepo;

@Mock
private DossierInternetResource dossierInternet;

@Mock
private RisqueBOConvertisseur risqueConv;

@InjectMocks
ServiceRisqueImpl serviceRisque;

  1. 您现在也需要模拟映射器,但是这样做时,您可以对调用映射器并使用其结果的业务逻辑进行更细粒度的控制.毕竟,您可以根据需要验证呼叫并模拟结果.

  1. You'll need to mock the mapper as well now, but in doing so, you have far more fine-grained control over your business logic that calls the mapper and uses its result. After all, you can verify the call and mock the result however you like.

并且您需要为映射器添加单独的测试并测试映射逻辑.我通常使用双向映射:in -> map -> reverseMap -> out并使用assertj属性声明来查看in是否与out相同.

And you need to add a separate test for your mapper and test the mapping logic. I usually to roundtrip mapping so: in -> map -> reverseMap -> out and use assertj property assertion to see if in is the same as out.

这篇关于如何测试和模拟mapstruct转换器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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