Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm trying to do the same thing. I have a solution that works for me - yours may vary (and you might not love the solution... it's a bit low-level).</p> <p>I came across an article on the net where they were using a custom class loader to do something similar which served as inspiration. If anyone can see how to improve then suggestions would be welcome btw. For deployment I rely on container injection of the EntityManager but for testing I create it myself using this code:</p> <pre><code>final Thread currentThread = Thread.currentThread(); final ClassLoader saveClassLoader = currentThread.getContextClassLoader(); currentThread.setContextClassLoader(new ClassLoaderProxy(saveClassLoader)); EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("test"); em = emFactory.createEntityManager(); </code></pre> <p>Then the ClassLoaderProxy is about as minimal as you can get and just redirects requests for META-INF/persistence.xml to META-INF/test-persist.xml:</p> <pre><code>public class ClassLoaderProxy extends ClassLoader { public ClassLoaderProxy(final ClassLoader parent) { super(); } @Override public Enumeration&lt;URL&gt; getResources(final String name) throws IOException { if (!"META-INF/persistence.xml".equals(name)) { return super.getResources(name); } else { System.out.println("Redirecting persistence.xml to test-persist.xml"); return super.getResources("META-INF/test-persist.xml"); } } } </code></pre> <p>Just to explain this a bit more:</p> <ol> <li>There are two persistence.xml files (one named persistence.xml that is used outside testing and one named test-persist.xml that is used for tests).</li> <li>The custom class loader is <em>only</em> active for unit tests (for deployment everything is normal)</li> <li>The custom class loader redirects requests for "META-INF/persistence.xml" to the test version ("META-INF/test-persist.xml"). </li> </ol> <p>I was originally hitting some problems because Hibernate will revert back (somehow) to the classloader that was used to load Hibernate (at least I think that is what was going on). I've found that putting the ClassLoader switching code (the first block) as a static block in your Test case it will get loaded before Hibernate but that, depending on your unit test structure you may also need to put the same code in other places (yuck).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload