I'm doing my project using Tomcat 7
, JSP
, Servlets
, Log4j
and MySQL
.
I've googled this question for hours with no proper answer.
How can I test my DAO's using DataSource
and JUnit
?
I found this article recently, but don't know how to configure it for my purposes and don't sure if it's a good way of doing it.
I'm having the following DAO hierarchy:
And I'm obtaining the DataSource
in AbstractRepository
in the following way:
...
protected final DataSource ds;
...
public AbstractRepository() {
DataSource dataSource = null;
try {
Context initContext = new InitialContext();
dataSource = (DataSource) initContext
.lookup("java:/comp/env/jdbc/mydb");
} catch (NamingException ex) {
LOG.error("Cannot obtain a connection from the pool", ex);
}
ds = dataSource;
}
...
Would you produce some code sample of the way to solve this problem for example for Entrant Repository Test
?