I tried to test if SessionFactory was valid through:
assertFalse(sessionFactory.isClosed());
But my test passes even if the underlaying datasource could not get connections:
java.sql.SQLException: Connections could not be acquired from the underlying database!
How can I test if the SessionFactory is valid?
Seems like you really want to test whether you have valid database connectivity.
SessionFactoryis just a wrapper, once configured it will work as long as underlyingDataSourceis fine.So how do you test
DataSource? First of all various connection pools allow you to define so-called test query. Typically"SELECT 1". If your database connection pool does not expose such a functionality, just pick any connection from your pool and run similar query.See also