I use spring boot 2.3.2 with mapstruct.
In a service class I have a mapper who have an autowired annotation.
@Service
public BillingService{
private BillingRepository billingRepository;
@Autowired
private BillingMapper billingMapper;
@Autowired
public BillingService (BillingRepository billingRepository){
this.billingRepository=billingRepository;
}
public void getBilling(Long billingId){
....
billingMapper.convertTo(...);
}
}
@RunWith(MockitoJUnitRunner.class)
public class BillingServiceTest{
@Mock
BillingRepository billingRepository;
private BillingService bilingService;
@Spy
private BillingMapper billingMapper = Mappers.getMapper(BillingMapper.class);
@BeforeEach
public void setup(){
MockitoAnnotations.initMocks(this);
billingService = new BillingService();
}
@Test
public void testGetBilling(){
List<Billing> billings = new ArrayList<>();
...
List<BillingPayload> payloads = new ArrayList<>();
when(billingMapper.convertTo(billings)).thenReturn(payloads);
bilingService.getBilling(1l);
}
}
@Mapper(componentModel="spring")
public interface BillingMapper{
...
}
When I debug and I'm stuck in getBilling method in BillingService Class, billingMapper is alway null;