1

I want to execute unit tests on an embedded Jetty with CDI/Weld in parallel in the same JVM.

For every test method a new jetty instance with a clean database is created. Execution in sequence works, however, in parallel I'm running into an exception.

org.jboss.weld.exceptions.DefinitionException: 
    Exception List with 1 exceptions:|Exception 0 
    :|java.lang.RuntimeException: javax.naming.NameAlreadyBoundException: 
     com<|?at com.sun.jersey.server.impl.cdi.CDIExtension.initialize(CDIExtension.java:196)

The full stacktrace is at pastebin.

The servers and context are isolated on different jetty server instances and ports. However, Weld does not realize this, although it detects a Jetty container and seems to be using a shared state some place (maybe this is Jetty specific?).

Has anyone come across this problem or has a tip how to tell Weld that it should not register twice?

Elmar Weber
  • 2,683
  • 28
  • 28

1 Answers1

2

You could try to fork on every test, so they're all done in different JVMs. It looks like Weld is storing beans per JVM (which makes sense) and when a new server is being started its running through the bootstrap again.

LightGuard
  • 5,298
  • 19
  • 19
  • yeah, I had originally thought it was the weld/jetty integration that was doing this but looking closer I think @LightGuard is correct and his solution seems viable to boot – jesse mcconnell Jun 20 '13 at 18:14
  • That was my first idea, the problem I see there is that I would have to partition every single test method into its own test class. But maybe that's worth the saved time I have to test that. – Elmar Weber Jun 21 '13 at 10:03
  • Another point: Should a container framework like Weld isolate this? Because deploying the same bean twice in different contexts works. – Elmar Weber Jun 21 '13 at 10:10
  • When you say "contexts" what exactly are you talking about (so we're on the same page)? Weld keeps the metadata about the bean per bean descriptor archive (which in your case would be the jvm as tests aren't bundled in jars), there's no point in having the metadata around multiple times, and could also cause problems with which one you get when you ask for a new instance. – LightGuard Jun 21 '13 at 15:02