users@glassfish.java.net

help getting started unit testing Java EE JAX-RS app using embedded GlassFish

From: <forums_at_java.net>
Date: Mon, 5 Mar 2012 09:06:12 -0600 (CST)

Hi Folks. I'm hoping you can help me. I'm relatively new to JAX-RS, JPA,
GlassFish, and Java DB, 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 bunch 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 apologize in advance for not forming my question more
clearly; this is the best I could do. So far I've found the following
possible pieces (more like hints at this point):

o Ideally I could set this up without Maven, which the few times I've looked
at it is a nightmare and didn't work for me. 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
http://stackoverflow.com/questions/8459284/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
(http://stackoverflow.com/questions/6739987/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.,
    @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 ahold of this?
I would /really/ appreciate your help. Thank you -- matt
Versions:
o GlassFish Server Open Source Edition 3.1.2 (build 23)
o Java DB/Derby: 10.8.1.2 - (1095077)
o NetBeans IDE 7.1 (Build 201112071828)


--
[Message sent by forum member 'matthewcornell']
View Post: http://forums.java.net/node/884020