尝试测试我的数据访问类时出错:
原因:org.apache.ibatis.builder.BuilderException:解析SQL映射器配置时出错。原因:org.apache.ibatis.datasource.DataSourceException
测试类:
@Tested
DataAccess dataAccess;
@Mock
Mapper mapper;
DataAccess类:
private Logger logger;
private final Mapper mapper;
public DataAccess() {
String loggerCategory = new properties().getLoggerCategory();
logger = LoggerFactory.getLogger(loggerCategory);
mapper = DBControl.getAutoClosingMapper(
Mapper.class, DataSource.source, logger);
}
DBControl(错误的来源):
private static final SqlSessionFactory sqlMapper;
static {
Reader reader = null;
String resource = "configuration.database.xml";
reader = Resources.getResourceAsReader(resource);
//the exception is getting thrown from this line
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
}
我在测试类中尝试了几种不同的mockito和jmockit注释,但每次都会出现相同的错误。
我只需要嘲笑一下映射器。
我在这里注意到的一件事是,您正在使用@Mock
注释来尝试模拟最终的类变量,除非您在某个地方有构造函数,否则这通常不会起作用。
例如
public class DataAccess{
private final Mapper mapper;
DataAccess(mapper){
this.mapper = mapper
// anything else
}
}
使用mockito你可以做这样的事情:
public class TestClass{
private DataAccess dataAccess = new DataAccess(Mockito.mock(Mapper.class));
}
此外,我通常会在您声明Logger时实例化它;
private final Logger LOGGER = LoggerFactory.getLogger(getClass());