我通过以下方式使用了嵌入式 glassfish:
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);
}
}
问题是嵌入式 glassfish 使用“target/classes”中的类,而 maven cobertura 使用“target/generated-classes/cobertura”。然后,第一次运行测试没问题,但是在第二次运行 cobertura 时,我收到 java.lang.RuntimeException: javax.naming.NamingException (可能是因为 cobertura 正在处理“目标/生成的类/cobertura ”而 glassfish 正在研究“目标/类”)。
有什么想法可以解决这个问题???