Getting errors for trying to test my data access class:
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.datasource.DataSourceException
Test class:
@Tested
DataAccess dataAccess;
@Mock
Mapper mapper;
DataAccess class:
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 (where the error is coming from):
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);
}
I've tried several different mockito and jmockit annotations in my test class but I am left with the same error each time.
I simply just need to mock the mapper.
One thing that I noticed here, is that you are using the @Mock
annotation to try to mock a final class variable, that typically is not going to work unless you have a constructor someplace.
e.g.
public class DataAccess{
private final Mapper mapper;
DataAccess(mapper){
this.mapper = mapper
// anything else
}
}
Using mockito you could do something like this:
public class TestClass{
private DataAccess dataAccess = new DataAccess(Mockito.mock(Mapper.class));
}
Also, I would typically instantiate the Logger when you declare it;
private final Logger LOGGER = LoggerFactory.getLogger(getClass());