Using Glassfish Embedded API in unit tests of Maven project - unit-testing

I have a Maven Project running on Glassfish 3.0.1 with these dependencies in the pom.xml:
...
<dependency>
<groupId>org.glassfish.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
...
I've been trying to run unit tests using the Glassfish Embedded API, as mentioned in the first dependency, but it gives me an error every time I try to create de EJBContainer.
test class:
...
#BeforeClass
public static void setUpClass() throws Exception {
EJBContainer ejbC = javax.ejb.embeddable.EJBContainer.createContainer();
}
...
error log:
javax.ejb.EJBException: No EJBContainer provider available
The following providers:
org.glassfish.ejb.embedded.EJBContainerProviderImpl
Returned null from createEJBContainer call.
at javax.ejb.embeddable.EJBContainer.reportError(EJBContainer.java:186)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:121)
at br.com.code.seuticket.sms.bean.GatewayBeanClickatellImplTest.setUpClass(GatewayBeanClickatellImplTest.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:62)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.executeTestSet(AbstractDirectoryTestSuite.java:140)
at org.apache.maven.surefire.suite.AbstractDirectoryTestSuite.execute(AbstractDirectoryTestSuite.java:127)
at org.apache.maven.surefire.Surefire.run(Surefire.java:177)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:345)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1009)
Does anyone has experience with unit testing Maven projects with Glassfish Embedded API?

Check out the Arquillian project by JBoss.
The mission of the Arquillian project
is to provide a simple test harness
that abstracts away all container
lifecycle and deployment from the test
logic so developers can easily produce
a broad range of integration tests for
their enterprise Java applications.
Specific instructions for Glassfish. I use it for running my integration test with JUnit (and maven). Works great. The details of managing the embedded container are abstracted away, except for a little bit of initial configuration that they walk you through in the docs.

you can try this code it works for me :
i adjust propreties according to my glassfish standard installation floder
Map<String, Object> properties = new HashMap<>();
properties.put(EJBContainer.MODULES, new File("target/classes/cd/espoirmur/ejb"));
properties.put("installation.root", "C:\\Program Files\\glassfish-4.1");
properties.put("instance.root", "C:\\Program Files\\glassfish-4.1\\glassfish\\domains\\domain1");
properties.put("configuration.file", "C:\\Program Files\\glassfish-4.1\\glassfish\\domains\\domain1\\config\\domainEmbeded.xml");
EJBContainer ec = EJBContainer.createEJBContainer(properties);
System.out.println("--------------ejb container sucessfully created----------");
Context ctx = ec.getContext();
System.out.println("--------------ejb context successfull sucessfully created----------");
enter code here

Related

java.lang.NoClassDefFoundError: javax/activation/DataSource on wsimport Intellij java 9

i'm trying to generate class stubs for a wsdl with Intellij-Idea 2017.2.5 (Webservices -> Generate code from wsdl...) using JDK-9
I'm getting this exception and i wonder how to tell intellij to pass "--add-modules java.activation" to complete the operation.
(i guess i should run wsimport from the command line...)
Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource
at com.sun.xml.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl.<clinit>(RuntimeBuiltinLeafInfoImpl.java:461)
at com.sun.xml.bind.v2.model.impl.RuntimeTypeInfoSetImpl.<init>(RuntimeTypeInfoSetImpl.java:65)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:133)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.createTypeInfoSet(RuntimeModelBuilder.java:85)
at com.sun.xml.bind.v2.model.impl.ModelBuilder.<init>(ModelBuilder.java:156)
at com.sun.xml.bind.v2.model.impl.RuntimeModelBuilder.<init>(RuntimeModelBuilder.java:93)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(JAXBContextImpl.java:455)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:303)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:142)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1174)
at com.sun.tools.xjc.reader.xmlschema.bindinfo.BindInfo.getJAXBContext(BindInfo.java:335)
at com.sun.tools.xjc.reader.internalizer.SCDBasedBindingSet.apply(SCDBasedBindingSet.java:235)
at com.sun.tools.xjc.ModelLoader.createXSOM(ModelLoader.java:541)
at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:269)
at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:95)
at com.sun.tools.ws.processor.modeler.wsdl.JAXBModelBuilder.bind(JAXBModelBuilder.java:142)
at com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler.buildJAXBModel(WSDLModeler.java:2244)
at com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler.internalBuildModel(WSDLModeler.java:191)
at com.sun.tools.ws.processor.modeler.wsdl.WSDLModeler.buildModel(WSDLModeler.java:137)
at com.sun.tools.ws.wscompile.WsimportTool.buildWsdlModel(WsimportTool.java:391)
at com.sun.tools.ws.wscompile.WsimportTool.run(WsimportTool.java:204)
at com.sun.tools.ws.wscompile.WsimportTool.run(WsimportTool.java:179)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at com.sun.tools.ws.Invoker.invoke(Invoker.java:135)
at com.sun.tools.ws.WsImport.main(WsImport.java:57)
Caused by: java.lang.ClassNotFoundException: javax.activation.DataSource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
... 28 more
Based on your error message, you need to add the following dependency in your pom.xml
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
From SDK 9, for JAXB to work for web services you need to also have the following dependencies if you do not already have them as they are not part of the SDK.
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
I guess it can be useful. I have these additional packages in my soap project when switch from Java 8 to 10. Gradle:
compile "javax.xml.bind:jaxb-api:2.3.0"
compile "javax.activation:activation:1.1"
compile "com.sun.xml.bind:jaxb-impl:2.3.0"
compile "com.sun.xml.bind:jaxb-core:2.3.0"
compile "com.sun.xml.ws:rt:2.3.0"
compile "com.sun.xml.ws:jaxws-rt:2.3.0"
Just for other people with the same exception coming here:
This problem can also occur if you use a web server such as tomcat and if you need the activation jar to be present there as well. One possible solution is to put it in the lib folder of tomcat (or to use the common.loader functionality).
I had the same problem. After changing the project jdk, it works for me.
Change project jdk to 8.

GATE embedded Pronominal conference no class def found junit.framework.Assert

While executing a Gate embedded app containing annie coreference pr I got the following error.
Gate lib was loaded using maven dependancy.
Exception in thread "main" java.lang.NoClassDefFoundError: junit/framework/Assert
at gate.creole.coref.PronominalCoref.init(PronominalCoref.java:110)
at gate.creole.coref.Coreferencer.init(Coreferencer.java:74)
at gate.Factory.createResource(Factory.java:432)
at gate.Factory.createResource(Factory.java:139)
at gate.Factory.createResource(Factory.java:121)
at com.sigmacr.OBIEPipelineBuilder.createPipeline(OBIEPipelineBuilder.java:150)
Issue solved. The junit dependency was scoped "test". While the GATE embedded coref PR used it in runtime.
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test-->should be runtime </scope>
</dependency>

Issue with neethi and WSO2 DStudio client stub

I need to connect to a webservice that have a mutual authentication security policy.
So, I create a stub client in WSO2 Developer Studio 3.1.0 inside my maven java project.
My project use maven to manage my dependencies, so I include the needed libraries to make the client work but I found a funny issue.
If I put this neethi dependencies in this order all work fine:
<dependency>
<groupId>org.apache.neethi.wso2</groupId>
<artifactId>neethi</artifactId>
<version>2.0.4.wso2v4</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>3.0.2</version>
</dependency>
if I change the order I found this error:
Exception in thread "main" java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType
at org.apache.neethi.AssertionBuilderFactoryImpl.findAssertionBuilderTarget(AssertionBuilderFactoryImpl.java:145)
at org.apache.neethi.AssertionBuilderFactoryImpl.invokeBuilder(AssertionBuilderFactoryImpl.java:134)
at org.apache.neethi.AssertionBuilderFactoryImpl.build(AssertionBuilderFactoryImpl.java:117)
at org.apache.neethi.PolicyBuilder.processOperationElement(PolicyBuilder.java:224)
at org.apache.neethi.PolicyBuilder.getAllOperator(PolicyBuilder.java:184)
at org.apache.neethi.PolicyBuilder.processOperationElement(PolicyBuilder.java:217)
at org.apache.neethi.PolicyBuilder.getExactlyOneOperator(PolicyBuilder.java:180)
at org.apache.neethi.PolicyBuilder.processOperationElement(PolicyBuilder.java:215)
at org.apache.neethi.PolicyBuilder.getPolicyOperator(PolicyBuilder.java:174)
at org.apache.neethi.PolicyBuilder.getPolicy(PolicyBuilder.java:113)
at org.apache.neethi.PolicyBuilder.getPolicy(PolicyBuilder.java:99)
at org.apache.neethi.PolicyEngine.getPolicy(PolicyEngine.java:78)
at org.test.ws.recibews.RecibeWSServiceStub.getPolicy(RecibeWSServiceStub.java:292)
at org.test.ws.recibews.RecibeWSServiceStub.populateAxisService(RecibeWSServiceStub.java:57)
at org.test.ws.recibews.RecibeWSServiceStub.<init>(RecibeWSServiceStub.java:96)
at org.test.ws.recibews.RecibeWSServiceStub.<init>(RecibeWSServiceStub.java:85)
at org.test.ws.recibews.RecibeWSServiceStub.<init>(RecibeWSServiceStub.java:133)
at org.test.webservicesTest.recibews_Test.recibews(recibews_Test.java:329)
at org.test.webservicesTest.recibews_Test.obtienews(recibews_Test.java:120)
at org.test.webservicesTest.recibews_Test.main(recibews_Test.java:88)
My problem is that I need to package this implementation in a jar file so I create a runnable jar file in Eclipse STS but when I run the jar I see this error:
NOTE: If i remove the 3.0.2 neethi dependency, my eclipse project work fine, but the jar not, with the same error below.
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoSuchMethodError: org.apache.neethi.Constants.isPolicyElement(Ljavax/xml/namespace/QName;)Z
at org.apache.axis2.util.PolicyUtil.getPolicyChildren(PolicyUtil.java:287)
at org.apache.axis2.deployment.AxisConfigBuilder.populateConfig(AxisConfigBuilder.java:157)
at org.apache.axis2.deployment.DeploymentEngine.populateAxisConfiguration(DeploymentEngine.java:857)
at org.apache.axis2.deployment.FileSystemConfigurator.getAxisConfiguration(FileSystemConfigurator.java:116)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContext(ConfigurationContextFactory.java:64)
at org.apache.axis2.context.ConfigurationContextFactory.createConfigurationContextFromFileSystem(ConfigurationContextFactory.java:210)
at org.apache.axis2.client.ServiceClient.configureServiceClient(ServiceClient.java:151)
at org.apache.axis2.client.ServiceClient.<init>(ServiceClient.java:144)
at org.test.ws.recibews.RecibeWSServiceStub.<init>(RecibeWSServiceStub.java:99)
at org.test.ws.recibews.RecibeWSServiceStub.<init>(RecibeWSServiceStub.java:85)
at org.test.ws.recibews.RecibeWSServiceStub.<init>(RecibeWSServiceStub.java:133)
at org.test.webservicesTest.recibews_Test.recibews(recibews_Test.java:329)
at org.test.webservicesTest.recibews_Test.obtienews(recibews_Test.java:120)
at org.test.webservicesTest.recibews_Test.main(recibews_Test.java:88)
... 5 more
Any idea about the solution to this problem??
Thanks.

Jetty 9.0 embedded and RestEasy 3.0 keeps throwing NoSuchMethodError

Today I had the idea to build a very simple web application, which would be powered by a REST backend. Since I wanted a very lightweight server I started looking at Jetty. And since I wanted to try another JAX-RS implementation than Jersey I looked at RestEasy. I thought those 2 would be easy to implement. I was wrong...
I imported the basic Jetty server and servlet dependencies since I thought that were the only server requirements for a basic (REST only) Jetty server (I alto tried to use the webapp dependency; this gave the same errors).
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlets</artifactId>
<version>9.0.0.RC0</version>
<scope>compile</scope>
</dependency>
Then I imported the basic RestEasy dependencies.
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.1.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.1.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>async-http-servlet-3.0</artifactId>
<version>3.0.1.Final</version>
<scope>compile</scope>
</dependency>
Main method:
public class ExampleActivator {
public static void main(String args[]) throws Exception {
Server server = new Server(8080);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
context.setContextPath("/");
ServletHolder h = new ServletHolder(new HttpServlet30Dispatcher());
h.setInitParameter("javax.ws.rs.Application", "packages.ExampleResources");
context.addServlet(h, "/*");
server.setHandler(context);
server.start();
server.join();
}
}
ExampleResources:
public class ExampleResources extends Application {
private static final ImmutableSet services = ImmutableSet.of(
ExampleResourceImpl.class
);
#Override
public Set<Class<?>> getClasses() {
return services;
}
}
ExampleResourceImpl:
#Path("activities")
public class ExampleResourceImpl {
#GET
#Produces(MediaType.TEXT_PLAIN)
public String getAll() {
return "Hello World";
}
}
When I go to the webapge I get the following trace:
250 [main] INFO org.eclipse.jetty.server.Server - jetty-9.0.0.RC0
911 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Deploying javax.ws.rs.core.Application: class packages.ExampleResources
939 [main] INFO org.jboss.resteasy.spi.ResteasyDeployment - Adding class resource packages.ExampleResourceImpl from Application class packages.ExampleResources
1005 [main] INFO org.eclipse.jetty.server.handler.ContextHandler - started o.e.j.s.ServletContextHandler#702ab48{/,null,AVAILABLE}
1037 [main] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector#465ed596{HTTP/1.1}{0.0.0.0:8080}
6315 [qtp84346444-13] WARN org.eclipse.jetty.servlet.ServletHandler - Error for /activities
java.lang.NoSuchMethodError: org.jboss.resteasy.specimpl.BuiltResponse.getHeaders()Ljavax/ws/rs/core/MultivaluedMap;
at org.jboss.resteasy.core.ServerResponseWriter.setDefaultContentType(ServerResponseWriter.java:195)
at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:46)
at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:411)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:376)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:848)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:671)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:448)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1070)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:375)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1004)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:136)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
at org.eclipse.jetty.server.Server.handle(Server.java:449)
at org.eclipse.jetty.server.HttpChannel.run(HttpChannel.java:246)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:265)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.run(AbstractConnection.java:240)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:589)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:520)
at java.lang.Thread.run(Thread.java:722)
Normally this would mean that I forgot a dependency, however I really have no clue what's going on.
I just stumbled upon the same issue. The solution was to add the jaxrs-api explicitly as the first dependency in the list of dependencies.
<dependencies>
<!-- jaxrs-api is the very first dependency -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.4.Final</version>
</dependency>
<!-- here come the other depdendencies -->
</dependencies>
Jackson uses the jsr311-api that provides the same Response class that is used by the BuiltResponse class. Unfortunately, this Response does not contain the getHeaders() method. So even though the dependency is resolved, it is using a wrong class. If somehow the Jackson dependency gets before the Resteasy one, then the exception above will be raised.
I had the very same problem. First of all it was working using JAX-RS 2 and then it was migrated to JAX-RS 3, so I thought it had something to do with that. After failing in trying to get JBoss use JAX-RS 3 from app via Maven and jboss-deployment-structure.xml, I tried upgrading JBoss modules, which can be done using the following procedure:
3.1. Upgrading Resteasy Within JBoss AS 7
Resteasy is bundled with JBoss AS 7. You will likely have the need to upgrade Resteasy in AS7.
The Resteasy distribution comes with a zip file called resteasy-jboss-modules-3.0.1.Final.zip.
Unzip this file while with the modules/ directory of the JBoss AS7 distribution. This will overwrite
some of the existing files there.
Source: http://docs.jboss.org/resteasy/docs/3.0.1.Final/userguide/pdf/resteasy-reference-guide-en-US.pdf
Hope it helps!
Had the very same problem when trying to deploy a jax-rs (with resteasy) to glassfish 3.1.2.2. Tried to resolve what caused the inconsistency, took for hours and couldn't find it in the end - glassfish behavior became more and more strange.
My recommendation for everyone trying to run resteasy on gf: stop it and use jersey.
I ran into this issue as well and it took me forever to figure out how to resolve it. Kevin Day was right. The issue was that one of the dependencies in my POM indirectly pulled in Jackson and that dependency was placed in the POM file before the jaxrs dependency. A fix was to simply move the jaxrs dependency to the top of the POM file so it gets picked up before Jackson.

AttachNotSupportedException while running jMockit tests on IBM JRE

I am getting the below exception when I try to run a simple jMockit/JUnit test using IBM JDK. Has anyone faced this issue? I tried given -Dcom.ibm.tools.attach.enable=yes as VM argument but no luck. The same code is working fine in Sun JDK.
java.lang.RuntimeException: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:89)
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:98)
at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:112)
at org.junit.runner.Runner.<clinit>(Runner.java:22)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
at sun.tools.attach.WindowsVirtualMachine.<init>(WindowsVirtualMachine.java:64)
at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:73)
... 22 more
java.lang.NoClassDefFoundError: org.junit.internal.runners.ErrorReportingRunner (initialization failure)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NoClassDefFoundError: org.junit.runner.Runner (initialization failure)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:140)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
... 9 more
Caused by: java.lang.RuntimeException: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:89)
at mockit.internal.startup.JDK6AgentLoader.loadAgent(JDK6AgentLoader.java:54)
at mockit.internal.startup.AgentInitialization.initializeAccordingToJDKVersion(AgentInitialization.java:21)
at mockit.internal.startup.Startup.initializeIfNeeded(Startup.java:98)
at mockit.internal.startup.Startup.initializeIfPossible(Startup.java:112)
at org.junit.runner.Runner.<clinit>(Runner.java:22)
at java.lang.J9VMInternals.initializeImpl(Native Method)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:167)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
... 11 more
Caused by: com.sun.tools.attach.AttachNotSupportedException: Unable to enqueue operation: the target VM does not support attach mechanism
at sun.tools.attach.WindowsVirtualMachine.<init>(WindowsVirtualMachine.java:64)
at mockit.internal.startup.JDK6AgentLoader.getVirtualMachineImplementationFromEmbeddedOnes(JDK6AgentLoader.java:73)
... 22 more
I tried giving the VM argument -javaagent:jmockit.jar as suggested by Kevin Welker and I got the below exception.
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at sun.instrument.InstrumentationImpl.loadClassAndStartAgent(InstrumentationImpl.java:335)
at sun.instrument.InstrumentationImpl.loadClassAndCallPremain(InstrumentationImpl.java:350)
Caused by: java.lang.NullPointerException
at mockit.internal.annotations.MockClassSetup.validateRealClass(MockClassSetup.java:59)
at mockit.internal.annotations.MockClassSetup.<init>(MockClassSetup.java:38)
at mockit.internal.annotations.MockClassSetup.<init>(MockClassSetup.java:77)
at mockit.internal.annotations.MockClassSetup.<init>(MockClassSetup.java:99)
at mockit.internal.startup.JMockitInitialization.setUpInternalStartupMock(JMockitInitialization.java:56)
at mockit.internal.startup.JMockitInitialization.initialize(JMockitInitialization.java:29)
at mockit.internal.startup.Startup.initialize(Startup.java:68)
at mockit.internal.startup.Startup.premain(Startup.java:56)
... 6 more
Fatal error: processing of -javaagent failed
The Attach API simply doesn't work in the IBM JDK 6.0, at least on Windows. Therefore, it's necessary to use the -javaagent:jmockit.jar parameter.
The NullPointerException that occurs at MockClassSetup.java:59, when using -javaagent, is caused by a bug in the IBM JDK. When a type referenced in an annotation attribute (#MockClass, in this case) is not present in the classpath, the JDK should throw a TypeNotPresentException. The Oracle JDKs do so as expected, but the IBM JDK returns null instead for the attribute value.
I just implemented a workaround for this in class MockClassSetup, which will be available in the next JMockit release, by the end of september. For now, you can avoid the problem by adding TestNG to the classpath (since the "type not found" is the org.testng.TestNG class).
Above answer to use -javaagent is correct. If you're using maven it's a little tricky, so here is how I did it:
Add the maven-dependency-plugin so you can generate absolute paths to the dependencies:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
2. Add -javaagent to surefire plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<configuration>
<argLine>-javaagent:${com.googlecode.jmockit:jmockit:jar} -XX:-UseSplitVerifier</argLine>
</configuration>
</plugin>
3. Also, you don't have to, but I'd recommend using a relatively newer version of jmockit. This issues was detected in 1.1 (prior to September 2012 fix by #Rogério, but adding -javaagent fixes it regardless. For reference I am using the latest version available in maven central (2.5) as of this comment:
<dependency>
<groupId>com.googlecode.jmockit</groupId>
<artifactId>jmockit</artifactId>
<!-- Use latest version. 1.1 gives AttachNotSupportedException -->
<version>1.5</version>
<scope>test</scope>
</dependency>
Certain versions of the IBM JDK do not properly support the attach mechanism. You might try running with -javaagent:jmockit.jar parameter. The following info is a bit old, but it may still apply as other JDKs do not necessarily fully support the attach mechanism.
See this: http://code.google.com/p/jmockit/issues/detail?id=18
I also tried using JMockit to override final methods in a class in a 3rd party library. I got the same problem discussed above, and again adding the VM arg -javaagent:C:/<Path-to-jar>/jmockit.jar only gave me a different type of error. I am also using the IBM JDK 6.0 that comes with Websphere. It's a shame but it looks like it is an actual impossibility to override final methods: I can't find any other Testing framework that can do it.
You just need update the JDK to 1.8. I tested this version JDK, and this problem has been resolved.
The setup for javagent for surefire using maven looks like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
....
<argLine>-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/1.11/jmockit-1.11.jar</argLine>
</configuration>
</plugin>
The above assumes you are using the following dependency:
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>1.11</version>
<scope>test</scope>
</dependency>
If you are using a different version then modify the argLine value appropriately.
Source: http://jmockit.googlecode.com/svn-history/r1166/trunk/www/installation.html