MapStruct Junit - Injecting Inner Mapper with CDI

Solution 1:

Without creating a CDI context one option is to define the inner class using Mockito for example:

@Spy
private B uses = Mappers.getMapper(B.class);

@InjectMocks
private A mapper = Mappers.getMapper(A.class);

@Test
public void test() {
   ADto = mapper.toDto(AEntity);
   // asserts...
}

This will set the inner mapper for A when it uses B.

Solution 2:

I would suggest not mocking out Mapper and setup a test with CDI that can actually create all Mappers properly (not experienced with CDI to suggest a solution).

Having said that you can use the Mapper#injectionStrategy from 1.3. You can use a constructor injection and inject your mocks in it.