问题:为什么mapper返回null?
谁能解释我为什么我的mapper(mapstruct)返回null?当我实现自己的映射器时,就可以了。
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest
class CouponServiceTestSuite {
@InjectMocks
private CouponService couponService;
@Mock
private CouponRepository couponRepository;
@Spy
private CouponDto couponDto;
@Spy
private CouponMapper couponMapper;
@Test
public void testMapper() {
Coupon coupon = createCoupon();
CouponDto couponDto = couponMapper.mapToCouponDto(coupon);
System.out.println(couponDto); // RETURN NULL
}
@Component
@Mapper
public interface CouponMapper {
Coupon mapToCoupon(CouponDto couponDto);
CouponDto mapToCouponDto(Coupon coupon);
default List<CouponDto> mapToCouponDtoList(List<Coupon> couponList) {
if (couponList == null) {
return new ArrayList<>();
}
return new ArrayList<>(couponList).stream()
.map(this::mapToCouponDto)
.collect(Collectors.toList());
}
}
谢谢 :)
由于您有个,因此返回null @Spy
。这意味着Mockito会将实现包装在自己的实例中,然后调用方法(如果为null,则将为方法返回null)。
如果希望对应用程序上下文中可用的bean进行监视或模拟,则应该使用Spring Boot @SpyBean
并@MockBean
从Spring Boot中使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句