samedi 7 février 2015

How to test DAO with DataSource in Java Web Application?

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 my DAO hierachy 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: 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 ?


Aucun commentaire:

Enregistrer un commentaire