I using my implementation ServletContextListener for writing in servlet context my object for work with database. In unit test my database handler work but same code in ServletContextListener throw java.sql.SQLException.
This is my database handler test which create Connection to DB and it's work. No exception no problem. All ok:
@Test
public void whenCreateDBJoinInstanceThenResultOfMethodGetDBExecutorNotNull() throws SQLException {
final DBJoint db = new DBJointHandler("database_scripts", "authentication_database");
Assert.assertNotNull(db);
db.closeConnection();
}
This is use same DBJoint which create Connection to database:
@WebListener
public class ContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
final ServletContext servletContext = servletContextEvent.getServletContext();
servletContext.setAttribute("db", new DBJointHandler("database_scripts", "authentication_database"));
}
...
}
And this my result:
java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/user_storage
And place which throw this Exception:
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(
properties.get("url"),
properties.get("username"),
properties.get("password")
);
}
This is exactly the same code which in test. When i run test then it's ok. When i use ServletContextListener it's Exception.
What wrong? How fix this issue? Thank you.