1

I've used embedded glassfish in the following way:

public static void createContainer() throws IOException {        
    File target = new File("target/classes");       
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(EJBContainer.MODULES, target);
    properties.put("org.glassfish.ejb.embedded.glassfish.installation.root",
            "/opt/glassfish3/glassfish");
    container = EJBContainer.createEJBContainer(properties);
    context = container.getContext();
}

@AfterSuite(alwaysRun = true)
public static void closeContainer() throws NamingException {
    // close container
}

// I use this method to lookup 
public static <T> T lookupBy(Class<T> type) {
    try {
        return (T) context.lookup("java:global/classes/" + type.getSimpleName());
    } catch (NamingException ex) {
        throw new RuntimeException(ex);
    }
}

The problem is that embedded glassfish uses the classes in "target/classes" and maven cobertura uses "target/generated-classes/cobertura". Then, the first time the tests run it's ok, but in the second time, when cobertura run, I receive a java.lang.RuntimeException: javax.naming.NamingException (probably because cobertura is working on "target/generated-classes/cobertura" while glassfish is working on "target/classes").

Any ideas to solve this problem???

joaosavio
  • 1,481
  • 3
  • 17
  • 20

1 Answers1

0

I experimented the same trouble with cobertura and glassfish-embedded. Here is my setup to solve it.

I just included glassfish-embedded-all and cobertura in maven dependencies without specific options. I don't use the EJBContainer property EJBContainer.MODULES, glassfish-embedded finds by itself the ejb classes in normal or cobertura phase.

However the JNDI portable names change between normal and cobertura case. So I adapted your lookupBy method to manage these two cases.

Finally here is my code :

public static void createContainer() throws IOException {        
    container = EJBContainer.createEJBContainer();
    context = container.getContext();
    MyServiceLocal ejb = lookupBy(MyServiceLocal.class,MyServiceImpl.class);
}

public static <T> T lookupBy(Class<T> type, Class service) {
    try {
        return (T) context.lookup("java:global/classes/" + service.getSimpleName());
    } catch (NamingException ex) {
        // lookup with cobertura
        return (T) context.lookup("java:global/cobertura/" + service.getSimpleName() + "!" + type.getName());
    }
}
GagouKun
  • 1
  • 1