I want to be able to junit test the following:
private ExecutorService executor = Executors.newSingleThreadExecutor();
public void foo() {
final EntityManagerFactory entityManagerFactory =
entityManager.getEntityManagerFactory();
executor.execute(new Runnable() {
@Override
public void run() {
EntityManager entityManager =
entityManagerFactory.createEntityManager();
Session session = (Session) entityManager.getDelegate();
try {
SQLQuery query =
session.createSQLQuery("SELECT * from foo_function()");
} catch (HibernateException exception) {
LOGGER.error("Exception: " + exception);
}
}
});
}
I am using Mockito. I tried to get it to throw an exception, for example:
Mockito.doThrow(SQLException.class).when(mockSession).createSQLQuery(any(String.class));
It would throw an exception in the spawned thread, but it doesn't fail when I run it as a Junit test.