I'm relatively new to JAX-RS, JPA, GlassFish, Java DB, and NetBeans, and I'd like to write unit tests for my code. [Version numbers are at the bottom.] However, I'm stuck on where to begin. I've done a good bit of searching, but I don't yet have a clear understanding of how to set up an embedded test of my code. I'm using NetBeans, but my question is general. I wish I could form my question more clearly, but this is the best I could do. So far I've found the following possible pieces (more like hints at this point).
o I want to set this up without Maven, but this means I must install the embedded jars manually. Q: Where do I find them?
o Create versions of my config xml files (glassfish-resources.xml and persistence.xml) that specify embedded versions of GlassFish and Java DB. Q: But how do you tell NetBeans to use those for testing rather than production ones that rely on the installed version of them?
I think persistence.xml would look something like this (from using hibernate with embedded derby):
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:test"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
o Create a custom Glassfish domain configuration file ( Embedded GlassFish ignores Maven test resources ). Q: What should this look like? I have domain.xml from the default domain1 that was created with my NetBeans install, but there's a lot to it.
o Once my project has access to the embedded files, and it is configured to use them, what should my JUnit harness look like? http://jersey.java.net/nonav/documentation/latest/client-api.html#d4e759 says:
protected void setUp() throws Exception {
...
glassfish = new GlassFish(BASE_URI.getPort());
ScatteredWar war = new ScatteredWar(...);
glassfish.deploy(war);
...
However, I've also seen EJBContainer mentioned, e.g., (from http://docs.oracle.com/javaee/6/tutorial/doc/gkcqz.html ):
@BeforeClass
public static void initContainer() throws Exception {
ec = EJBContainer.createEJBContainer(); ctx = ec.getContext();
}
o I'm using JPA, so I need access to the PersistenceContext/EntityManager. Currently I look it up via:
new InitialContext().lookup("java:module/<jax-rs resource name>");
But I've also seen:
emf = Persistence.createEntityManagerFactory("chapter02PU");
Q: What's the right way to get a hold of this?
I would really appreciate your help.
- Versions:
- GlassFish Server Open Source Edition 3.1.2 (build 23)
- Java DB/Derby: 10.8.1.2 - (1095077)
- NetBeans IDE 7.1 (Build 201112071828)