Closed Johnzhuawa closed 5 months ago
public class MyServiceImpl extends ServiceImpl<MyDao, MyEntity> implements MyService {}
目前是用@BeforeEach void setUp() { try { Field baseMapperField = ServiceImpl.class.getDeclaredField("baseMapper"); baseMapperField.setAccessible(true); baseMapperField.set(manufactureCategoryCommissionService, manufactureCategoryCommissionDao); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } }来解决的,有更优雅的方式嘛
当前使用版本 3.5.5 当前环境信息 例如: Java8 + Mysql5.7 java17 描述bug现象 使用mockito框架mock ServiceImpl的BaseMapper,出现空指针 提供问题复现步骤 @ExtendWith(MockitoExtension.class) class MyServiceImplTest { @Mock private MyDao baseMapper; @InjectMocks private MyServiceImpl myServiceImpl; @Test void updateCategoryCommissionRate() { when(baseMapper.selectList(any())).thenReturn(null); myServiceImpl.update("1",123); } } 提供完整堆栈日志(可选) java.lang.NullPointerException: Cannot invoke "com.intbee.cloud.modules.product.dao.mysql.ManufactureCategoryCommissionDao.selectList(com.baomidou.mybatisplus.core.conditions.Wrapper)" because "this.baseMapper" is null 提供问题复现工程(可选) 请尽量提供复现工程,减少大家排错的时间.