question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Run JUnit4 commandline against a fat jar.

See original GitHub issue

I am new to the JVM world and hence don’t know much about it. Though it’s not stackoverflow.com, I hope you guys can point me to the right direction.

Here is an issue I am trying to tackle:

I have a fat jar that has a JUnit4 test class. I can run it in gradle and it works just fine. However what I would like to do is have a run it from a commandline. I read the documentation and can see that one can run tests per class. But I need the to be discovered automatically from the jar file, same way as gradle does it.

What I tried so far:

> java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore ./build/libs/fatjar.jar
JUnit version 4.12
.E
Time: 0.003
There was 1 failure:
1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
java.lang.IllegalArgumentException: Could not find class [./build/libs/fatjar.jar]
        at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
        at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
        at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
        at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
        at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
Caused by: java.lang.ClassNotFoundException: ./build/libs/fatjar.jar
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at org.junit.internal.Classes.getClass(Classes.java:16)
        at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
        ... 4 more

FAILURES!!!
Tests run: 1,  Failures: 1

Obviously this didn’t work, as the runner is expecting a class name and not a jar file.

Is it actually possible? May it be possible with JUnit5 runner?

Thanks in advance.

P.S.

In case it helps, this is the project: https://github.com/albumprinter/junit-synnefo-demo

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kcooneycommented, Apr 24, 2019

JUnit 4 doesn’t have built-in support for this, but there are third-party options. One of them is https://github.com/takari/takari-cpsuite (that’s apparently the new home for https://blog.johanneslink.net/2006/11/14/classpathsuite-for-junit4/)

0reactions
derwaspcommented, Apr 24, 2019

If you want to run just one test and know the full class name you could specify it on the command line

yeah, I figured that out, too. thanks!

The thing is that I have a few classes I want to run. I guess ClasspathSuite might be an option.

I am also looking at using gradle for it: run the tests from the compiled jar. I have a working prototype already.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to run JUnit tests for Java from the command line [duplicate]
I've run my JUnit tests using maven before. Now I'm packaging all my source code into a JAR file, and want to run...
Read more >
How To Setup JUnit Environment For Your First Test?
For running JUnit tests in IntelliJ IDEA, right-click on the project and click on 'Run All Tests'. Run all junit test. You can...
Read more >
Setup build.gradle to run cucumber / junit tests via. command ...
I was tasked with setting up the build.gradle to create a fatjar file which can be ran via. command line to execute the...
Read more >
Karate | Test Automation Made Simple.
Karate Core “Fat JAR” ... But to be able to run JUnit 5 tests from the command-line, you need to ensure that the...
Read more >
Gradle tutorial for complete beginners - Tom Gregory
This tutorial answers all of these questions and helps you take your ... as running a single command on the command line or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found