Running all tests from a #Category using Maven - junit

I want to run only a subset of my unit tests, the ones defined by a specific #Category.
So I read several SO questions, such as this one (which is exactly what I am looking for), and also this one.
The solution of my problem seems to be provided by the ClasspathSuite project. So I started to write the NewTest and OldTest interfaces that will define my test categories. Then, I created the AllTests suite:
#RunWith(ClasspathSuite.class)
public class AllTests { }
After that, I created a AllNewTests suite:
#RunWith(Categories.class)
#IncludeCategory(NewTest.class)
#SuiteClasses( { AllTests.class })
public class AllNewTests { }
Finally, I create two JUnit classes, one per category:
#Category(NewTest.class)
public class SomeNewTests {
// some tests...
}
#Category(OldTest.class)
public class SomeOldTests {
// some tests...
}
Now, if I run AllTests, Eclipse launches all the tests of my project, while Maven fails as no test are found:
mvn test -Dtest=AllTests
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running my.company.AllTests
Tests run: 0, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.093 sec
There are no tests to run.
If I run AllNewTests (which is the correct thing to do, right?), in Eclipse everything is fine (i.e. it only run the tests annoted with #Category(NewTest.class)) but Maven returns an error:
mvn test -Dtest=AllNewTests
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running my.company.AllNewTests
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.125 sec <<< FAILURE!
Results :
Tests in error:
initializationError(my.company.AllNewTests)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
The exception thrown is the following:
org.junit.runner.manipulation.NoTestsRemainException
at org.junit.runners.ParentRunner.filter(ParentRunner.java:256)
at org.junit.experimental.categories.Categories.<init>(Categories.java:142)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:35)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
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.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:33)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:146)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:97)
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.ProviderFactory$ClassLoaderProxy.invoke(ProviderFactory.java:103)
at $Proxy0.invoke(Unknown Source)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:145)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcess(SurefireStarter.java:70)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:69)
My question is what I did wrong?
Technical details: Java 6, Maven 3.0.2, JUnit 4.8.1, Surefire plugin 2.7.1, cpsuite-1.2.5

As an update: as of Surefire plugin v2.11, JUnit 4.8+ style categories are now supported.
The release notes for Surefire v2.11 mention the new feature. The surefire:test goal can is configured using groups.

I have solved my problem by creating my own JUnit Runner, that extends the Suite.
The idea is close to the principle by the Classpath Suite project, i.e. looking for the classes existing in the classpath, and keep only the ones that are annotated with a given annotation (#NewTest for example).
If you are interested, you can read the full story on my blog.

After reading some blog posts and stackoverflow questions, I finally was able to make this work with the surefire plugin, as user1034382 answered. In my case with version 2.17 of maven-surefire-plugin.
Just to add my two cents, the more up-to-date explanation can be found here:
Using JUnit Categories to group tests
But you may run with the following surefire plugin problem:
[ERROR] java.lang.RuntimeException: Unable to load category:
That can be fixed with this other stackoverflow question/answer:
Where should I put interface class for Junit #Category?
My answer is just to gather all these info here and avoid googling/reading to many diferent solutions. Al least, this worked to me.

Related

junit-vintage-engine is compatible with which versions of junit?

Is junit-vintage engine meant to be compatible with versions of junit older than 4.12.x?
In my case there seems to be an issue during test discovery when using junit 4.7 and running within intellij idea.
I am currently running version of idea 2017.3.3, junit-jupiter '5.0.3', junit-vintage-engine '4.12.3' and junit 4.7
When running all tests in a module i see the following exception.
Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/experimental/categories/Category
at org.junit.vintage.engine.descriptor.VintageTestDescriptor.addCategoriesAsTags(VintageTestDescriptor.java:86)
at org.junit.vintage.engine.descriptor.VintageTestDescriptor.getTags(VintageTestDescriptor.java:77)
at org.junit.platform.launcher.TestIdentifier.from(TestIdentifier.java:60)
at org.junit.platform.launcher.TestPlan.lambda$from$0(TestPlan.java:80)
at org.junit.platform.engine.TestDescriptor.accept(TestDescriptor.java:239)
at org.junit.platform.engine.TestDescriptor.lambda$accept$0(TestDescriptor.java:241)
at java.lang.Iterable.forEach(Iterable.java:75)
at org.junit.platform.engine.TestDescriptor.accept(TestDescriptor.java:241)
at org.junit.platform.launcher.TestPlan.lambda$from$1(TestPlan.java:81)
at java.util.LinkedHashMap$LinkedValues.forEach(LinkedHashMap.java:608)
at org.junit.platform.launcher.TestPlan.from(TestPlan.java:81)
at org.junit.platform.launcher.core.DefaultLauncher.discover(DefaultLauncher.java:82)
at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:48)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.ClassNotFoundException: org.junit.experimental.categories.Category
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more
I narrowed down the source of the exception to a few lines in VintageTestDescriptor.java (staring at line 86)
private void addCategoriesAsTags(Set<TestTag> tags) {
Category annotation = (Category)this.description.getAnnotation(Category.class);
Category is in the experimental package of junit 4.12 and seems to be missing from junit 4.7. I was not able to find doc on which versions of junit the vintage engine is compatible with.
The junit-vintage-engine is compatible with JUnit 4.12.
Is there any reason not to update to 4.12?

Gradle build / test failed - kryo.KryoException: Buffer overflow

While running a Gradle build, tests are failing.
PS:
1. Gradle is using the correct JDK (1.6) to build.
2. I tried this with JDK 1.7, same error comes there as well.
3. I don't see this error when I build it locally (with JDK 1.6) on a linux/windows
4. machine but one of the machine is giving me this error.
My ?s
1. What can be done to fix the com.esotericsoftware.kryo.KryoException: Buffer overflow error.
2. Why Gradle process failed, even when test section in build.gradle says:
test {
ignoreFailures=true
//more code here for test section...
//..
}
Any hints/help appreciated. Error snapshot is shown below:
:test
Unexpected exception thrown.
org.gradle.messaging.remote.internal.MessageIOException: Could not read message from '/0:0:0:0:0:0:0:1:53371'.
at org.gradle.messaging.remote.internal.inet.SocketConnection.receive(SocketConnection.java:88)
at org.gradle.messaging.remote.internal.hub.MessageHub$ConnectionReceive.run(MessageHub.java:230)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.esotericsoftware.kryo.KryoException: Buffer underflow.
at com.esotericsoftware.kryo.io.Input.require(Input.java:162)
at com.esotericsoftware.kryo.io.Input.readByte(Input.java:255)
at org.gradle.messaging.remote.internal.hub.InterHubMessageSerializer$MessageReader.read(InterHubMessageSerializer.java:64)
at org.gradle.messaging.remote.internal.hub.InterHubMessageSerializer$MessageReader.read(InterHubMessageSerializer.java:53)
at org.gradle.messaging.remote.internal.inet.SocketConnection.receive(SocketConnection.java:83)
... 5 more
> Building > :test > 84 tests completed
:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> Process 'Gradle Worker 6' finished with non-zero exit value 139
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
It's an internal error. Best chance is to try with the latest Gradle version.

Glassfish Embedded does not start

My glassfish 3.2 embedded was starting with Arquillian and Junit perfectly and now I've getting this Exception in eclipse.
I've replace .m2 repository and it doesn't start.
Anybody could help me:
java.lang.RuntimeException: Could not setup GlassFish Embedded Runtime
Caused by: org.glassfish.embeddable.GlassFishException: A MultiException has 2 exceptions.
1. java.lang.IllegalArgumentException: The scope name given in the descriptor (org.glassfish.hk2.api.PerLookup) did not match the scope annotation on the class (javax.inject.Singleton) in class CommandExecutorImpl
2. java.lang.IllegalArgumentException: Errors were discovered while reifying SystemDescriptor(
implementation=com.sun.enterprise.admin.cli.embeddable.CommandExecutorImpl
contracts={com.sun.enterprise.admin.cli.embeddable.CommandExecutorImpl,org.glassfish.embeddable.CommandRunner
at com.sun.enterprise.glassfish.bootstrap.StaticGlassFishRuntime.newGlassFish(StaticGlassFishRuntime.java:138)
at org.jboss.arquillian.container.glassfish.embedded_3_1.GlassFishContainer.setup(GlassFishContainer.java:138)
... 62 more
I've solved this question deleting all workspace project and eclipse .metadata and RemoteSystemsTempFiles folders, after that I've dowloaded all project with "Checkout Project" and the glassfish-embedded using Junit 4 have started normally.

ClassNoDefFoundError while running a junit test which is instrumented using TPTP probekit agent

I've been dealing with this error message for a while now and nobody has been able to help me...
I am trying to probe a java application using the standalone TPTP probekit agent with the following linux command:
java '-agentlib:JPIBootLoader=JPIAgent:server=standalone;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript=ome/anis/qf-t/core/probe/testProbe.probescript' junit.textui.TestRunner quickfix.test.acceptance.AcceptanceTestSuite
My application is actually a junit testcase from the Quickfixj package (quickfixj.org)...
Running the above command, the probing starts and I am able to see the runtime traces. However, at some point during execution I am getting the runtime exception:
java.lang.NoClassDefFoundError: testProbe_probe$Probe_0
25-Jan-2012 10:25:11 AM quickfix.test.acceptance.ATServer run
SEVERE: error in AT server
java.lang.NoClassDefFoundError: testProbe_probe$Probe_0
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl.<init>(DocumentBuilderFactoryImpl.java)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at javax.xml.parsers.FactoryFinder.newInstance(FactoryFinder.java:147)
at javax.xml.parsers.FactoryFinder.find(FactoryFinder.java:233)
at javax.xml.parsers.DocumentBuilderFactory.newInstance(DocumentBuilderFactory.java:123)
at quickfix.DataDictionary.load(DataDictionary.java:906)
at quickfix.DataDictionary.read(DataDictionary.java:893)
at quickfix.DataDictionary.<init>(DataDictionary.java:109)
at quickfix.DefaultSessionFactory.getDataDictionary(DefaultSessionFactory.java:325)
at quickfix.DefaultSessionFactory.createDataDictionary(DefaultSessionFactory.java:219)
at quickfix.DefaultSessionFactory.processFixtDataDictionaries(DefaultSessionFactory.java:258)
at quickfix.DefaultSessionFactory.create(DefaultSessionFactory.java:113)
at quickfix.mina.acceptor.AbstractSocketAcceptor.createSessions(AbstractSocketAcceptor.java:242)
at quickfix.mina.acceptor.AbstractSocketAcceptor.startAcceptingConnections(AbstractSocketAcceptor.java:99)
at quickfix.SocketAcceptor.initialize(SocketAcceptor.java:66)
at quickfix.SocketAcceptor.start(SocketAcceptor.java:59)
at quickfix.test.acceptance.ATServer.run(ATServer.java:193)
at java.lang.Thread.run(Thread.java:662)**
which states that my probe file is missing. But this file has been needed in the first place to print the traces!!???.....
Well, here are some facts:
I am able to probe other applications, including a simple junit test:
java '-agentlib:JPIBootLoader=JPIAgent:server=standalone;ProbekitAgent:ext-pk-BCILibraryName=BCIEngProbe,ext-pk-probescript=ome/anis/qf-t/core/probe/testProbe.probescript' junit.textui.TestRunner quickfix.test.acceptance.AnisJUnitTestExample
I am able to run the quickfix.test.acceptance.AcceptanceTestSuite using junit (no probing):
java junit.textui.TestRunner quickfix.test.acceptance.AcceptanceTestSuite
I have set the path to testProbe_probe$Probe_0 in CLASSPATH
I have called the System.getenv("CLASSPATH") from within my testcase and the CLASSPATH seems to be fine
My testcase deals with threads, sockets, etc
And my system configuration is:
-os: ubuntu 10.04-64bit (on VirtualBox!)
-java version "1.6.0_24"
-junit: junit-4.10.jar
-TPTP agent controller: agntctrl.linux_em64t-TPTP-4.7.2
Any ideas??
Thanks,
Anis

Monitoring External Jobs in Hudson

I have started to look at Hudson as a replacement for CruiseControl. I would like to use it to monitor external jobs as well. I have tried to follow the advice on this page: Monitoring External Jobs
When I run this command:
java -jar hudson-core-1.309.jar
I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/http/HttpServletRequest
at hudson.EnvVars.initMaster(EnvVars.java:197)
at hudson.EnvVars.(EnvVars.java:192)
at hudson.Main.getHudsonHome(Main.java:71)
at hudson.Main.run(Main.java:61)
at hudson.Main.main(Main.java:53)
Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest
Add servlet.jar to the executing classpath. hudson-core-1.309.jar is built to run in a web container and thus doesn't have that included.