SpringBoot Mocktio 单测 Mock Mapper

import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import org.apache.ibatis.builder.MapperBuilderAssistant;
....

@ExtendWith(MockitoExtension.class)
class XxxxTeset {
  @InjectMocks
  private XxxService service = new XxxService();
  @Mock
  private XxxMapper xxxMapper;
  @Mock
  private XxxDao xxxDao;

  @BeforeEach
  public void initTable() {
    TableInfoHelper.initTableInfo(new MapperBuilderAssistent(new MybatisConfiguration(), ""),  XxxBean.class); 
    // 若是 MPJLambdaWrapper 连接多少张表就需要写多少个Bean对象的
    // 否则报错 can not find lambda cache
  }
  
  @Test 
  void test() {
    // 若是模拟 MybatisPlus中的ServiceImpl中getBaseMapper需要
    Mockito.when(xxxDao.getBaseMapper()).thenReturn(xxx);
  }
}
posted @ 2026-01-16 20:12  Joeyin  阅读(3)  评论(0)    收藏  举报