alibaba / testable-mock

换种思路写Mock,让单元测试更简单
https://alibaba.github.io/testable-mock/
MIT License
1.83k stars 310 forks source link

mybatisplus常用方法mock失败 #300

Open itxiaobai123 opened 2 years ago

itxiaobai123 commented 2 years ago

mybatisplus的BaseMapper中selectOne方法mock失败而delete却mock成功?有大佬知道这是为什么吗

xqzw123 commented 1 year ago

mock同样遇到byId失败,但byIds成功。 `public interface Repository<AGGREGATE, ID extends Serializable>{

/**
 * 按id查找
 *
 * @param id
 * @return
 */
AGGREGATE byId(ID id);

/**
 * 按id查找集合
 *
 * @param ids
 * @return
 */
List<AGGREGATE> byIds(List<ID> ids);

} public interface TaskRepository extends Repository<TaskDto,Long>{ }

@Repository public class TaskRepositoryImpl implements TaskRepository { @Override public TaskDto byId(Long id) { return taskMapping.targetToSource(taskInfoDao.selectById(id)); }

@Override
public List<TaskDto> byIds(List<Long> ids) {
    final List<TaskEntity> list = new LambdaQueryChainWrapper<>(taskInfoDao).in(TaskEntity::getId, ids).list();
    return taskMapping.targetToSource(list);
}

}`

xqzw123 commented 1 year ago

解决了,Mock方法写的不对导致。 正确写法如下 @MockInvoke(targetClass = TaskRepository.class,targetMethod = "byId") public AGGREGATE byId(Serializable id){ TaskDto taskDto = easyRandom.nextObject(TaskDto.class); return (AGGREGATE)taskDto; }