A way to run junit from unexploded jar

Hello gang,

I’m looking for instructions on how to run/execute junit tests from within the jar file w/out exploding it first. Is it possible to do in the latest version of gradle??Thanks!

Hi,
See [JUnitCore API][1]

Using a JavaExec task, you can achieve what you want

e.g.

javaexec {
  classpath files('junit.jar', 'your jar') (*)
  main org.junit.runner.JUnitCore
  args 'Test1','Test2'
}

(*) Note that the classpath needs a FileCollection, so you can use a Dependency as well
e.g.

configurations {
  runJunit
}
dependencies {
  runJunit 'junit:junit:4.11'
  runJunit sourceSets.test.output //to get your compiled test jar
}

and ‘classpath runJunit’
[1]: http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html

Thanks for the info!

Im getting the following error:

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘C:\projects\gralde\gradle-standalone-test\build.gradle’ line: 12

  • What went wrong:
    A problem occurred evaluating root project ‘gradle-standalone-test’.

Could not find method runJunit() for arguments [junit:junit:4.12] on root project ‘gradle-standalone-test’.

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 6.069 secs

/*********************************************/

Here is the build.gradle

apply plugin: ‘java’

repositories {
mavenCentral()
}

dependencies {
//testCompile files(“fatjar.jar”)
//testRuntime “junit:junit:4.12”

runJunit 'junit:junit:4.12'
runJunit sourceSets.test.output

}

/*
task unpack(type: Copy) {
from zipTree(‘fatjar.jar’)
into file("${buildDir}/unpacked")
}
*/

test {

dependsOn javaexec
}

javaexec {
classpath files(‘junit.jar’, ‘fatjar.jar’)
main org.junit.runner.JUnitCore
//args ‘Test1’,'Test2’
args ‘testSetLiquibaseLogLevel’

}

task wrapper(type: Wrapper) {
gradleVersion = “2.4”
}

Ok so I was able to resovle above by adding

configurations {
runJunit
}

but Im still getting an error:

14:14:33.120 [ERROR] [org.gradle.BuildExceptionReporter]
14:14:33.130 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
14:14:33.145 [ERROR] [org.gradle.BuildExceptionReporter]
14:14:33.154 [ERROR] [org.gradle.BuildExceptionReporter] * Where:
14:14:33.164 [ERROR] [org.gradle.BuildExceptionReporter] Build file ‘C:\projects\gralde\gradle-standalone-test\build.gradle’ line: 34
14:14:33.175 [ERROR] [org.gradle.BuildExceptionReporter]
14:14:33.185 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
14:14:33.196 [ERROR] [org.gradle.BuildExceptionReporter] A problem occurred evaluating root project ‘gradle-standalone-test’.
14:14:33.207 [ERROR] [org.gradle.BuildExceptionReporter] > No such property: org for class: org.gradle.process.internal.DefaultJavaExecAction_Decorated
Possible solutions: args
14:14:33.219 [ERROR] [org.gradle.BuildExceptionReporter]
14:14:33.230 [ERROR] [org.gradle.BuildExceptionReporter] * Try:
14:14:33.239 [ERROR] [org.gradle.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.
14:14:33.252 [LIFECYCLE] [org.gradle.BuildResultLogger]
14:14:33.264 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD FAILED
14:14:33.275 [LIFECYCLE] [org.gradle.BuildResultLogger]

It’s
main=‘org.junit.runner.JUnitCore’

Main is a string property of the javaexec task.
Sorry about the inaccuracy.

Still not working:

14:28:12.504 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTING
14:28:12.520 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Waiting until process started: command ‘C:\Program Files\Java\jdk1.7.0_45\bin\java.exe’.
14:28:12.534 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: STARTED
14:28:12.547 [DEBUG] [org.gradle.process.internal.ExecHandleRunner] waiting until streams are handled…
14:28:12.548 [INFO] [org.gradle.process.internal.DefaultExecHandle] Successfully started process 'command ‘C:\Program Files\Java\jdk1.7.0_45\bin\java.exe’'
14:28:12.644 [ERROR] [system.err] Error: Could not find or load main class org.junit.runner.JUnitCore
14:28:12.670 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: FAILED
14:28:12.681 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Process ‘command ‘C:\Program Files\Java\jdk1.7.0_45\bin\java.exe’’ finished with exit value 1 (state: FAILED)
14:28:12.695 [DEBUG] [org.gradle.configuration.project.BuildScriptProcessor] Timing: Running the build script took 3.677 secs
14:28:12.730 [ERROR] [org.gradle.BuildExceptionReporter]
14:28:12.740 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
14:28:12.754 [ERROR] [org.gradle.BuildExceptionReporter]
14:28:12.764 [ERROR] [org.gradle.BuildExceptionReporter] * Where:
14:28:12.772 [ERROR] [org.gradle.BuildExceptionReporter] Build file ‘C:\projects\gralde\gradle-standalone-test\build.gradle’ line: 33
14:28:12.782 [ERROR] [org.gradle.BuildExceptionReporter]
14:28:12.792 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
14:28:12.802 [ERROR] [org.gradle.BuildExceptionReporter] A problem occurred evaluating root project ‘gradle-standalone-test’.
14:28:12.810 [ERROR] [org.gradle.BuildExceptionReporter] > Process ‘command ‘C:\Program Files\Java\jdk1.7.0_45\bin\java.exe’’ finished with non-zero exit value 1
14:28:12.823 [ERROR] [org.gradle.BuildExceptionReporter]
14:28:12.833 [ERROR] [org.gradle.BuildExceptionReporter] * Try:
14:28:12.843 [ERROR] [org.gradle.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.
14:28:12.857 [LIFECYCLE] [org.gradle.BuildResultLogger]
14:28:12.875 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD FAILED
14:28:12.886 [LIFECYCLE] [org.gradle.BuildResultLogger]
14:28:12.896 [LIFECYCLE] [org.gradle.BuildResultLogger] Total time: 6.755 secs

Here is my build.gradle
apply plugin: ‘java’

repositories {
mavenCentral()
}

configurations {
runJunit
}
dependencies {
//testCompile files(“fatjar.jar”)
//testRuntime “junit:junit:4.12”

runJunit 'junit:junit:4.12'
runJunit sourceSets.test.output

}

test {

javaexec {
	classpath files('junit.jar', 'fatjar.jar')  
	main='org.junit.runner.JUnitCore'
	//args 'Test1','Test2'
	args 'TestExecution'

 }

}

task wrapper(type: Wrapper) {
gradleVersion = “2.4”
}

FAILURE: Build failed with an exception.

  • Where:
    Build file ‘C:\projects\gralde\gradle-standalone-test\build.gradle’ line: 25

  • What went wrong:
    A problem occurred evaluating root project ‘gradle-standalone-test’.

Process ‘command ‘C:\Program Files\Java\jdk1.7.0_45\bin\java.exe’’ finished with non-zero exit value 1

  • Try:
    Run with --info or --debug option to get more log output.

  • Exception is:
    org.gradle.api.GradleScriptException: A problem occurred evaluating root project ‘gradle-standalone-test’.
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:76)
    at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$1.run(DefaultScriptPluginFactory.java:148)
    at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:156)
    at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:39)
    at org.gradle.configuration.project.BuildScriptProcessor.execute(BuildScriptProcessor.java:26)
    at org.gradle.configuration.project.ConfigureActionsProjectEvaluator.evaluate(ConfigureActionsProjectEvaluator.java:34)
    at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:55)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:487)
    at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:85)
    at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:42)
    at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:129)
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:106)
    at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:86)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:90)
    at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
    at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41)
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:28)
    at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:50)
    at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:27)
    at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:40)
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:169)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:237)
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:210)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:206)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
    at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
    at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:127)
    at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
    Caused by: org.gradle.process.internal.ExecException: Process ‘command ‘C:\Program Files\Java\jdk1.7.0_45\bin\java.exe’’ finished with non-zero exit value 1
    at org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:365)
    at org.gradle.process.internal.DefaultJavaExecAction.execute(DefaultJavaExecAction.java:31)
    at org.gradle.api.internal.file.DefaultFileOperations.javaexec(DefaultFileOperations.java:148)
    at org.gradle.api.internal.project.AbstractProject.javaexec(AbstractProject.java:779)
    at org.gradle.groovy.scripts.DefaultScript.javaexec(DefaultScript.java:192)
    at build_teo3f1bi2tanok0arhzm4pi0$_run_closure4.doCall(C:\projects\gralde\gradle-standalone-test\build.gradle:25)
    at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:67)
    at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:130)
    at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:110)
    at org.gradle.api.internal.AbstractTask.configure(AbstractTask.java:492)
    at org.gradle.api.internal.AbstractTask.configure(AbstractTask.java:58)
    at org.gradle.api.internal.ClosureBackedAction.execute(ClosureBackedAction.java:59)
    at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:130)
    at org.gradle.util.ConfigureUtil.configure(ConfigureUtil.java:91)
    at org.gradle.api.internal.DefaultNamedDomainObjectCollection$ContainerElementsDynamicObject.invokeMethod(DefaultNamedDomainObjectCollection.java:341)
    at org.gradle.api.internal.CompositeDynamicObject.invokeMethod(CompositeDynamicObject.java:150)
    at org.gradle.groovy.scripts.BasicScript.methodMissing(BasicScript.java:79)
    at build_teo3f1bi2tanok0arhzm4pi0.run(C:\projects\gralde\gradle-standalone-test\build.gradle:22)
    at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:74)
    … 38 more

BUILD FAILED

Total time: 5.897 secs

C:\projects\gralde\gradle-standalone-test>

You specify your javaexec classpath with classpath files(‘junit.jar’, ‘fatjar.jar’)

This means that you manually have junit.jar in the project’s directory.

The point of using “configurations” and “dependencies” is to avoid that.
That’s why in my example I wrote
"classpath runJunit"
This allows to let gradle decide where the retrieved junit.jar is located (it’s actually somewhere in its cache but we don’t care )

Can you give me an exmple of what my “classpath runJunit” should look like? Im on windows.

It should exactly look like that :wink:
we defined a runJunit configuration
This can be used in the classpath method that is present on javaExec tasks (see its javadoc)
javaexec {
classpath configurations.runJunit
main=‘org.junit.runner.JUnitCore’
//args ‘Test1’,'Test2’
args ‘TestExecution’
}

I’m currently not in front of my computer so I might forget something but that should work.

Thanks for all your help!!! Switched to “classpath configurations.runJunit”

but still

14:59:02.668 [ERROR] [org.gradle.BuildExceptionReporter]
14:59:02.677 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
14:59:02.690 [ERROR] [org.gradle.BuildExceptionReporter]
14:59:02.699 [ERROR] [org.gradle.BuildExceptionReporter] * Where:
14:59:02.708 [ERROR] [org.gradle.BuildExceptionReporter] Build file ‘C:\projects\gralde\gradle-standalone-test\build.gradle’ line: 27
14:59:02.718 [ERROR] [org.gradle.BuildExceptionReporter]
14:59:02.727 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
14:59:02.735 [ERROR] [org.gradle.BuildExceptionReporter] A problem occurred evaluating root project ‘gradle-standalone-test’.
14:59:02.745 [ERROR] [org.gradle.BuildExceptionReporter] > No such property: runJunit for class: org.gradle.process.internal.DefaultJavaExecAction_Decorated
14:59:02.756 [ERROR] [org.gradle.BuildExceptionReporter]
14:59:02.764 [ERROR] [org.gradle.BuildExceptionReporter] * Try:
14:59:02.772 [ERROR] [org.gradle.BuildExceptionReporter] Run with --stacktrace option to get the stack trace.
14:59:02.784 [LIFECYCLE] [org.gradle.BuildResultLogger]
14:59:02.796 [LIFECYCLE] [org.gradle.BuildResultLogger] BUILD FAILED
14:59:02.806 [LIFECYCLE] [org.gradle.BuildResultLogger]

Yes it’s definitely this.

Any idea on why Im still getting an error above? Thanks!

I’m starting to lose track on your actual build.gradle file content.
Could you post it here or create a gist on github (might be clearer)