This is my test class:
public class RoutingResponseRegressionOneByOne {
#BeforeClass
public static void oneTimeSetUp() {
routingClient = injector.getInstance(IRoutingClient.class);
System.out.println("in RoutingResponseRegressionOneByOne:: oneTimeSetUp");
}
I added a task to my build.gradle
task testRegression(type: Test) {
systemProperties['test.single'] = '\'RoutingResponseRegressionOneByOne\''
//systemProperty 'test.single', System.getProperty('test.single', '\'RoutingResponseRegressionOneByOne\'')
}
When I run "test" from the intellij this is the output
however when I "testRegression" from the intellij this is the output:
what should I fix in order for "testRegression" to run only this test class ?
You can pass an argument to your task with P and use the TestFilter.
In this link you will find a complete example for the filter usage. Below I attached a code of a custom automationTest task that runs tests from a specific folder.
task automationTest(type: Test) {
//search for an argument called 'single'. if exists, use it as a test filter
if (project.hasProperty('single')) {
filter {
includeTestsMatching "*${project.property('single')}*"
}
}
useTestNG()
description = "Runs Automation Tests"
testClassesDir = file("$buildDir/classes/main/automation")
classpath += sourceSets.main.runtimeClasspath
}
run it as: gradle -Psingle=MyTest automationTest
Related
I'm testing an app with JUnit5 and using Jacoco for coverage report.
Tests are executed Ok and test reports are present.
However, Jacoco report has the following logs if service contains methods, annotated with #Transactional
[ant:jacocoReport] Classes in bundle 'my-service' do no match with execution data. For report generation the same class files must be used as at runtime.
[ant:jacocoReport] Execution data for class mypackage/SampleService does not match.
This error occurres for all #Service classes methods, annotated with #Transactional, plain classes coverage is calculated ok.
Here's a sample test:
#SpringBootTest
#ExtendWith(SpringExtension.class)
public class MyServiceTest {
#Autowired
private SampleService sampleService;
#Test
public void doWork(){
sampleService.doWork();
}
}
Works fine. Coverage is non-zero:
public class SampleService {
public void doWork(){
System.out.println("HEY");
}
}
0% coverage:
public class SampleService {
#Transactional
public void doWork(){
System.out.println("HEY");
}
}
Transactional creates a proxy around actuall class. But, isn't there an out-of-box way for Jacoco to handle such a common situation?
I've tried #EnableAspectJAutoProxy annotaion with different flag variations, checked that up-to-date Jupiter engine and Jacoco plugin are used
Here's gradle config:
subprojects {
test {
useJUnitPlatform()
}
jacocoTestReport {
afterEvaluate {
classDirectories.from = files(classDirectories.files.collect {
fileTree(dir: it, exclude: '*Test.java')
})
}
reports {
html.enabled = true
xml.enabled = true
csv.enabled = false
}
}
}
Any help appreciated
I tried it with a test project, similar to what you've described, however I couldn't reproduce the issue. The only difference that I see between your project and mine, is that I've used maven instead of gradle.
Here is the test project:
https://github.com/gybandi/jacocotest
And here is the jacoco result for it (using org.springframework.transaction.annotation.Transactional annotation):
If this doesn't help you, could you upload your test project to github or some other place?
Edit:
#MikaelF posted a link to another answer, which shows how to add offline instrumentation for jacoco.
The solution that was described there worked for me, after I added the following block to build.gradle:
task instrument(dependsOn: [classes, project.configurations.jacocoAnt]) {
inputs.files classes.outputs.files
File outputDir = new File(project.buildDir, 'instrumentedClasses')
outputs.dir outputDir
doFirst {
project.delete(outputDir)
ant.taskdef(
resource: 'org/jacoco/ant/antlib.xml',
classpath: project.configurations.jacocoAnt.asPath,
uri: 'jacoco'
)
def instrumented = false
if (file(sourceSets.main.java.outputDir).exists()) {
def instrumentedClassedDir = "${outputDir}/${sourceSets.main.java}"
ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
fileset(dir: sourceSets.main.java.outputDir, includes: '**/*.class')
}
//Replace the classes dir in the test classpath with the instrumented one
sourceSets.test.runtimeClasspath -= files(sourceSets.main.java.outputDir)
sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
instrumented = true
}
if (instrumented) {
//Disable class verification based on https://github.com/jayway/powermock/issues/375
test.jvmArgs += '-noverify'
}
}
}
test.dependsOn instrument
There seems to be an open ticket on the jacoco plugin's github about this as well:
https://github.com/gradle/gradle/issues/2429
Based on single module instrumentation example https://stackoverflow.com/a/31916686/7096763 (and updated version for gradle 5+ by #MikaelF) here's example for multimodule instrumentation:
subprojects { subproject ->
subproject.ext.jacocoOfflineSourceSets = [ 'main' ]
task doJacocoOfflineInstrumentation(dependsOn: [ classes, subproject.configurations.jacocoAnt ]) {
inputs.files classes.outputs.files
File outputDir = new File(subproject.buildDir, 'instrumentedClasses')
outputs.dir outputDir
doFirst {
project.delete(outputDir)
ant.taskdef(
resource: 'org/jacoco/ant/antlib.xml',
classpath: subproject.configurations.jacocoAnt.asPath,
uri: 'jacoco'
)
def instrumented = false
jacocoOfflineSourceSets.each { sourceSetName ->
if (file(sourceSets[sourceSetName].output.classesDirs[0]).exists()) {
def instrumentedClassedDir = "${outputDir}/${sourceSetName}"
ant.'jacoco:instrument'(destdir: instrumentedClassedDir) {
fileset(dir: sourceSets[sourceSetName].output.classesDirs[0], includes: '**/*.class')
}
//Replace the classes dir in the test classpath with the instrumented one
sourceSets.test.runtimeClasspath -= files(sourceSets[sourceSetName].output.classesDirs[0])
sourceSets.test.runtimeClasspath += files(instrumentedClassedDir)
instrumented = true
}
}
if (instrumented) {
//Disable class verification based on https://github.com/jayway/powermock/issues/375
test.jvmArgs += '-noverify'
}
}
}
test.dependsOn doJacocoOfflineInstrumentation
}
full example here:
https://github.com/lizardeye/jacocomultimodulesample
Still, I think this is a durty hack, which can be easily broken with gradle or jacoco updates
I have a very basic gradle project, and have a test file with main(). The file - "src/test/javaTestRunner.java" is as follows:
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class TestRunner {
public static void main(String[] args) {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful());
}
}
The code compiles ("./gradlew classes" and "./gradlew testclasses" works fine). However, I am not able to execute TestRunner, not able to find the correct way to invoke the above class. I have tried the following:
./gradlew test --tests TestRunner
./gradlew test TestRunner
./gradlew TestRunner
"build.gradle" contains the following:
apply plugin: 'java'
apply plugin: 'application'
repositories {
jcenter()
}
dependencies {
compile 'com.google.guava:guava:21.0'
testCompile 'junit:junit:4.12'
}
// Define the main class for the application
mainClassName = 'MyFirstJavaProgram'
Any suggestions.
Thank you,
Ahmed.
A alternate way of running the main method as a gradle test:
Change the main method to a non-static method with no parameters and annotated with #Test:
public class TestRunner {
#Test
public void runTests() {
Result result = JUnitCore.runClasses(TestJunit.class);
for (Failure failure : result.getFailures()) { // consider using assertAll (JUnit5)
System.out.println(failure.toString());
}
System.out.println(result.wasSuccessful()); // consider using assertTrue (JUnit5)
}
}
Then ./gradlew test --tests TestRunner should now work.
Nice JUnit 5 tutorial Writing Tests with JUnit 5 by Trisha Gee of JetBrains.
I am using Cucumber testing framing with spring-rest web service. I am using standard cucumber JUnit framework and Gradle build tool.
Here are the dependencies
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('info.cukes:cucumber-java:1.2.4')
testCompile('info.cukes:cucumber-junit:1.2.4')
testCompile ("info.cukes:cucumber-spring:1.2.4")
testCompile('org.springframework.security:spring-security-test')
Here is my feature file, location of feature file is src/main/resources/feature/addition.feature
Feature: Testing a REST API
Users should be able to add two numbers
Scenario: number addition service
When baseUri is /messages/add
Then result should be 3
Here is my step definition code. it resides in test/java/somepackage/
public class StepDefinition {
#When("^baseUri is (.*)$")
public void baseUri(String uri) {
}
#Then("result should be (\\d+)")
public void checkResult(int result){
Assert.assertEquals(result, this.result);
}
}
My test runner class is, test runner and StepDefinition both are in the same package in /test/java
#RunWith(Cucumber.class)
#CucumberOptions(features = "classpath:feature/", glue = {"com.example.restdemo.web.StepDefinition"}, format = {"pretty", "html:target/cucumber"})
public class CucumberIntegrationTest {
}
Now when I am running my test runner class i.e CucumberIntegrationTest.java, I am getting following O/P.
"C:\Program Files\Java\jdk1.8.0_144\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\lib\idea_rt.jar=52114:C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\bin -Dfile.encoding=UTF-8 -classpath "C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\lib\idea_rt.jar;C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\plugins\junit\lib\junit-rt.jar;C:\Users\govindas\Downloads\ideaIU-2017.2.5.win\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_144\jre\lib\rt.jar;C:\Users\govindas\IdeaProjects\restdemo\out\test\classes;C:\Users\govindas\IdeaProjects\restdemo\out\production\classes;C:\Users\govindas\IdeaProjects\restdemo\out\production\resources;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-cache\1.5.7.RELEASE\98480d5e70a79ff279ea0bb156f2d18222ba8211\spring-boot-starter-cache-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-data-rest\1.5.7.RELEASE\cc092ea664a736c5fdf6077dd86f3459bcdd8caa\spring-boot-starter-data-rest-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-hateoas\1.5.7.RELEASE\be7eb70b2ac27fb270278530238323e441afd1cf\spring-boot-starter-hateoas-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-jdbc\1.5.7.RELEASE\98c652a5461ccbec70b55fd0f7815d9c9a37a082\spring-boot-starter-jdbc-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-jersey\1.5.7.RELEASE\dc608d6e96755fdad671da5f9a11f86b345fe30d\spring-boot-starter-jersey-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-security\1.5.7.RELEASE\e680aa1a8990a6e98e7a843c88f0a8bc378bb4d\spring-boot-starter-security-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-web\1.5.7.RELEASE\a862305be8b7512a41f6768d825ed37251ccbbe0\spring-boot-starter-web-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-test\1.5.7.RELEASE\d23f37a72c2d4e46aba66bc69a649213f9cadeee\spring-boot-starter-test-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-spring\1.2.4\f3cff393c13087ac43e11936a90ec0c8792c534\cucumber-spring-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-java\1.2.4\57cca534b7abe43f6dd7624b90d3d97d33d3023d\cucumber-java-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.derby\derby\10.13.1.1\29c42babe5c5463c6eea45487d3cfbfe8348ccd1\derby-10.13.1.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-junit\1.2.4\8a57b68486cff4da4f10019cb9c62c597a6d2861\cucumber-junit-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-test\4.2.3.RELEASE\f93339f597290d85a596f25a7841b9109b79ced6\spring-security-test-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-validation\1.5.7.RELEASE\a2e87bf66a1db8037e1feeba7e7063996fa72090\spring-boot-starter-validation-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter\1.5.7.RELEASE\b42fe6962dd8b3be4ac8c1d7134d0ca2d0a35c7e\spring-boot-starter-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context-support\4.3.11.RELEASE\a74bdcbce2b35804cdfdac1ca820820100b5fd9b\spring-context-support-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-rest-webmvc\2.6.7.RELEASE\9a638b76b290b52a241b59a7341a042a063bad3d\spring-data-rest-webmvc-2.6.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-rest-core\2.6.7.RELEASE\fe15f604fa9b35f16af608c05266fcba53a7d17c\spring-data-rest-core-2.6.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.hateoas\spring-hateoas\0.23.0.RELEASE\678ffa0798f417a794fea592dc8066e325611919\spring-hateoas-0.23.0.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.plugin\spring-plugin-core\1.2.0.RELEASE\f380e7760032e7d929184f8ad8a33716b75c0657\spring-plugin-core-1.2.0.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-config\4.2.3.RELEASE\9f3771903616e33521836fd999d63efbfebf90d\spring-security-config-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-web\4.2.3.RELEASE\7a03e737484ca232d7146852f06d067ac21427ac\spring-security-web-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-webmvc\4.3.11.RELEASE\814f91ec4dc324e724bbe7cbc5045b234604c539\spring-webmvc-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.security\spring-security-core\4.2.3.RELEASE\5c0e47a47036c94d6fdd02696bf52be6d1adbd4d\spring-security-core-4.2.3.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test-autoconfigure\1.5.7.RELEASE\8bead16b97c9f1b5fe7c47f3e030dd9351a6974d\spring-boot-test-autoconfigure-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-test\1.5.7.RELEASE\b8d77445d48f4320f404d8f13f4e5cb95bc36b30\spring-boot-test-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-autoconfigure\1.5.7.RELEASE\e83f1407ff991e9e7eb156f49fa19d868bb73289\spring-boot-autoconfigure-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot\1.5.7.RELEASE\1006870df7b3dc203df7ce84463c5dd7049fdaa4\spring-boot-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.ext\jersey-spring3\2.25.1\a31bfcd2fcae5beb979d3f41079b6f4020d6fbc9\jersey-spring3-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\spring-bridge\2.5.0-b32\f38ecef23edc769942a95c062efd63541044de42\spring-bridge-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-web\4.3.11.RELEASE\50a3dfce550bdf9459dfb2c6282aa104b040258e\spring-web-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-context\4.3.11.RELEASE\3efec2d7c7469d49108a2b21c3f15831f7297569\spring-context-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.media\jersey-media-json-jackson\2.25.1\19d1e4276eb7b6386640c344d9e5c01eba7eae5d\jersey-media-json-jackson-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.jaxrs\jackson-jaxrs-json-provider\2.8.10\f8837cf03871569eb02a491dea64929e5da14b4\jackson-jaxrs-json-provider-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.jaxrs\jackson-jaxrs-base\2.8.10\50a3d02a31cb427272a96fe696b5ac9442c7cfa1\jackson-jaxrs-base-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.module\jackson-module-jaxb-annotations\2.8.10\22df8efd0cff7661bf136148c73e2651748f7600\jackson-module-jaxb-annotations-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-databind\2.8.10\f7b83cb2bc4b88d53961e749e1ad32f49ef017b7\jackson-databind-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-annotations\2.8.0\45b426f7796b741035581a176744d91090e2e6fb\jackson-annotations-2.8.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-jdbc\8.5.20\10d0d288d837ddfc92096920e4571054a3934160\tomcat-jdbc-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-jdbc\4.3.11.RELEASE\642ed7497d327f299caccb930af6b65e229864c4\spring-jdbc-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-tomcat\1.5.7.RELEASE\eae533e2a0111a51d812912acb3d3e2368736d1b\spring-boot-starter-tomcat-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.containers\jersey-container-servlet\2.25.1\cf5f7a76fcea38158b890ab7a0142d4db709a882\jersey-container-servlet-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.containers\jersey-container-servlet-core\2.25.1\400e30bb035a0cdf3c554530224141ce659a0d1e\jersey-container-servlet-core-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.ext\jersey-bean-validation\2.25.1\1971927d79cad0ad2b5a3bfda24967748a2023d\jersey-bean-validation-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-server\2.25.1\276e2ee0fd1cdabf99357fce560c5baab675b1a2\jersey-server-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-aop\4.3.11.RELEASE\461d0bb58f1de30203d9331adf4b51de9554de5a\spring-aop-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2\2.5.0-b32\c3accae585955e49c771d464899e906ecc9ffb4\hk2-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\config-types\2.5.0-b32\686bbe7f80b1b879d64c06bc6606c97721a795f2\config-types-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-config\2.5.0-b32\dce05ac4225dbc0c1c382ad02e3b5bee51f0168a\hk2-config-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.hibernate\hibernate-validator\5.3.5.Final\622a9bcef2eed6d41b5b8e0662c36212009e375\hibernate-validator-5.3.5.Final.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.jayway.jsonpath\json-path\2.2.0\22290d17944bd239fabf5ac69005a60a7ecbbbcb\json-path-2.2.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\junit\junit\4.12\2973d150c0dc1fefe998f834810d68f278ea58ec\junit-4.12.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.assertj\assertj-core\2.6.0\b532c3fc4f66bcfee4989a3514f1cd56203a33ad\assertj-core-2.6.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.mockito\mockito-core\1.10.19\e8546f5bef4e061d8dd73895b4e8f40e3fe6effe\mockito-core-1.10.19.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-library\1.3\4785a3c21320980282f9f33d0d1264a69040538f\hamcrest-library-1.3.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.hamcrest\hamcrest-core\1.3\42a25dc3219429f0e5d060061f71acb49bf010a0\hamcrest-core-1.3.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.skyscreamer\jsonassert\1.4.0\9cdbb373a06f6513e51e8c545ee6a5e981463edb\jsonassert-1.4.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-test\4.3.11.RELEASE\cf765b5d6d6bdc38c67522ecae3fe9549da43e1\spring-test-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-tx\4.3.11.RELEASE\c931ee363d2269f03edfeb83d7d7fe23924ab8a1\spring-tx-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.data\spring-data-commons\1.13.7.RELEASE\cf4538abda1f0a9cf2323d6e4204df0130aeec73\spring-data-commons-1.13.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-beans\4.3.11.RELEASE\591cc35fd39292adae13f01aa13e978d0bb11936\spring-beans-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-expression\4.3.11.RELEASE\fca662a2fccdad90ec22b2aaecb021047dcbe249\spring-expression-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework\spring-core\4.3.11.RELEASE\eb30ed093f628279d3aead068fd478fa343f1dad\spring-core-4.3.11.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-core\1.2.4\72790b1da44d8d3d2764c6aef29865ee228bbeb1\cucumber-core-1.2.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.springframework.boot\spring-boot-starter-logging\1.5.7.RELEASE\c84c4ea09d7f789a0d5e936a961471e5dbd9295e\spring-boot-starter-logging-1.5.7.RELEASE.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.yaml\snakeyaml\1.17\7a27ea250c5130b2922b86dea63cbb1cc10a660c\snakeyaml-1.17.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml.jackson.core\jackson-core\2.8.10\eb21a035c66ad307e66ec8fce37f5d50fd62d039\jackson-core-2.8.10.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\jcl-over-slf4j\1.7.25\f8c32b13ff142a513eeb5b6330b1588dcb2c0461\jcl-over-slf4j-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-classic\1.1.11\ccedfbacef4a6515d2983e3f89ed753d5d4fb665\logback-classic-1.1.11.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\jul-to-slf4j\1.7.25\af5364cd6679bfffb114f0dec8a157aaa283b76\jul-to-slf4j-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\log4j-over-slf4j\1.7.25\a87bb47468f47ee7aabbd54f93e133d4215769c3\log4j-over-slf4j-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.slf4j\slf4j-api\1.7.25\da76ca59f6a57ee3102f8f9bd9cee742973efa8a\slf4j-api-1.7.25.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat\tomcat-juli\8.5.20\6c0579d329696059f377585d2a51455bf083d9ee\tomcat-juli-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-websocket\8.5.20\4df9055507926a2651f691cc9964c50493a0ab29\tomcat-embed-websocket-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-core\8.5.20\b9026ee20f1f6a2b0fc3e51dd806d800901448b0\tomcat-embed-core-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.apache.tomcat.embed\tomcat-embed-el\8.5.20\42960f481a8a80f4ffd3b865c2232820e3565bf1\tomcat-embed-el-8.5.20.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-client\2.25.1\4d563b1f93352ee9fad597e9e1daf2c6159993c6\jersey-client-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.media\jersey-media-jaxb\2.25.1\d7da0beeed5614a3bfd882662faec602699e24b\jersey-media-jaxb-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.core\jersey-common\2.25.1\2438ce68d4907046095ab54aa83a6092951b4bbb\jersey-common-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.ext\jersey-entity-filtering\2.25.1\4a5805060f796ec2c9bb1ba0ce91c1db6d889524\jersey-entity-filtering-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\javax.ws.rs\javax.ws.rs-api\2.0.1\104e9c2b5583cfcfeac0402316221648d6d8ea6b\javax.ws.rs-api-2.0.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\javax.annotation\javax.annotation-api\1.2\479c1e06db31c432330183f5cae684163f186146\javax.annotation-api-1.2.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-core\2.5.0-b32\8cb6a8a9522ec523b7740d29f555bdbe9d936af2\hk2-core-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-runlevel\2.5.0-b32\60dc979763ea885e796cba4d3e322af8d500ba74\hk2-runlevel-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-locator\2.5.0-b32\195474f8ad0a8d130e9ea949a771bcf1215fc33b\hk2-locator-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-api\2.5.0-b32\6a576c9653832ce610b80a2f389374ef19d96171\hk2-api-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\javax.inject\2.5.0-b32\b2fa50c8186a38728c35fe6a9da57ce4cc806923\javax.inject-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\javax.validation\validation-api\1.1.0.Final\8613ae82954779d518631e05daa73a6a954817d5\validation-api-1.1.0.Final.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\aopalliance\aopalliance\1.0\235ba8b489512805ac13a8f9ea77a1ca5ebe3e8\aopalliance-1.0.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.jboss.logging\jboss-logging\3.3.1.Final\c46217ab74b532568c0ed31dc599db3048bd1b67\jboss-logging-3.3.1.Final.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.fasterxml\classmate\1.3.4\3d5f48f10bbe4eb7bd862f10c0583be2e0053c6\classmate-1.3.4.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\net.minidev\json-smart\2.2.1\5b9e5df7a62d1279b70dc882b041d249c4f0b002\json-smart-2.2.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.objenesis\objenesis\2.1\87c0ea803b69252868d09308b4618f766f135a96\objenesis-2.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\com.vaadin.external.google\android-json\0.0.20131108.vaadin1\fa26d351fe62a6a17f5cda1287c1c6110dec413f\android-json-0.0.20131108.vaadin1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-html\0.2.3\624a0c986088e32910336dd77aee5191c04a8201\cucumber-html-0.2.3.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\cucumber-jvm-deps\1.0.5\69ed0efe4b81f05da3c0bdc7281cbdc43f5ceb26\cucumber-jvm-deps-1.0.5.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\info.cukes\gherkin\2.12.2\17138631fa20fd0e44a13e50d6b7be59cee1a94\gherkin-2.12.2.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.atteo\evo-inflector\1.2.2\2551aad98d65ac5464d81fe05f0e1516cfe471c9\evo-inflector-1.2.2.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.jersey.bundles.repackaged\jersey-guava\2.25.1\a2bb4f8208e134cf2cf71dfb8824e42942f7bd06\jersey-guava-2.25.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\osgi-resource-locator\1.0.1\4ed2b2d4738aed5786cfa64cba5a332779c4c708\osgi-resource-locator-1.0.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\hk2-utils\2.5.0-b32\5108a926988c4ceda7f1e681dddfe3101454a002\hk2-utils-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\aopalliance-repackaged\2.5.0-b32\6af37c3f8ec6f9e9653ec837eb508da28ce443cd\aopalliance-repackaged-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.javassist\javassist\3.21.0-GA\598244f595db5c5fb713731eddbb1c91a58d959b\javassist-3.21.0-GA.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2\class-model\2.5.0-b32\17f054f3e91898c0c0fc52163ad904b13c24e8b\class-model-2.5.0-b32.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\net.minidev\accessors-smart\1.1\a527213f2fea112a04c9bdf0ec0264e34104cd08\accessors-smart-1.1.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\ch.qos.logback\logback-core\1.1.11\88b8df40340eed549fb07e2613879bf6b006704d\logback-core-1.1.11.jar;C:\Users\govindas\.gradle\caches\modules-2\files-2.1\org.glassfish.hk2.external\asm-all-repackaged\2.5.0-b32\dc705f1d54cd5a96cbc5a473525e75ef1cb59a9e\asm-all-repackaged-2.5.0-b32.jar;C:\Users\xyz\.gradle\caches\modules-2\files-2.1\org.ow2.asm\asm\5.0.3\dcc2193db20e19e1feca8b1240dbbc4e190824fa\asm-5.0.3.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.example.restdemo.web.CucumberIntegrationTest
Feature: Testing a REST API
Users should be able to add two numbers
19:00:03.815 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Registering scope 'cucumber-glue' with implementation [cucumber.runtime.java.spring.GlueCodeScope#2a54a73f]
Test ignored.
Test ignored.
Test ignored.
Test ignored.
Scenario: number addition service # feature/addition.feature:4
When baseUri is /messages/add
Then result should be 3
1 Scenarios (1 undefined)
2 Steps (2 undefined)
0m0.000s
You can implement missing steps with the snippets below:
#When("^baseUri is /messages/add$")
public void baseuri_is_messages_add() throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
#Then("^result should be (\\d+)$")
public void result_should_be(int arg1) throws Throwable {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
Process finished with exit code 0
Remove the class name from the glue attribute
#CucumberOptions(features = "classpath:feature/", glue = {"com.example.restdemo.web"}, format = {"pretty", "html:target/cucumber"})
I've been attempting to run a java main from Gradle however Gradle keeps giving me the error
Execution failed for task ':myTask'
No main class specified
I've attached the code below
sourceSets {
main {
java {
srcDirs 'src/myPackage/downloadupdater'
srcDirs 'src/myPackage/downloadupdater/util'
srcDirs 'src/myPackage/downloadupdater/dao'
}
// output.classesDir = "/bin"
}
}
task myTask(dependsOn : compileJava, type : JavaExec){
group = "Custom"
description = "Acquires the weekly stats"
doLast{
classpath = sourceSets.main.output.classesDir
main = "myPackage.SomeClass"
args "-w"
}
}
The java class I'm calling is:
package myPackage
public class SomeClass{
private static DownloadUpdater updater;
public static void main (String [] args) {
updater = new DownloadUpdater();
updater.whichStatistics(args[0]);
updater.setCalendar();
updater.secureWorkbook();
updater.getStatistics();
}
}
After building, my directories come up with a build-classes-java-main-myPackage but gradle still can't find the main class.
Either move java source files to src/main/java instead of just src. Or set
sourceSet properly
sourceSets.main.java.srcDirs = ['src']
and use
task execute(type:JavaExec) {
main = "myPackage.SomeClass"
classpath = sourceSets.main.runtimeClasspath
}
for more refer : Gradle to execute Java class (without modifying build.gradle)
I have a gradle build script which currently works by simply executing a Java class through it's main method. What I want to know is, how can I call a static method in the same class but not have to go through the main method. The current gradle code is as follows:
import org.apache.tools.ant.taskdefs.condition.Os
apply plugin: 'java'
defaultTasks 'runSimple'
project.ext.set("artifactId", "test-java")
File rootDir = project.getProjectDir()
File targetDir = file("${rootDir}/target")
FileCollection javaClasspath = files("${targetDir}/tools.jar")
task(runSimple, dependsOn: 'classes', type: JavaExec) {
main = 'com.test.model.JavaTest'
classpath = javaClasspath
args 'arg1'
args 'arg2'
}
And my Java class as follows:
package com.test.model;
public class JavaTest {
public static void main(String[] args) throws Exception {
System.out.println("In main");
anotherMethod(args[0], args[1]);
}
public static void anotherMethod(String arg1, String arg2) {
System.out.println("In anotherMethod");
System.out.println(arg1 + " " + arg2);
}
}
This gives me the output:
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:runSimple
In main
In anotherMethod
arg1 arg2
BUILD SUCCESSFUL
Total time: 2.344 secs
My question is simply how can I skip the main method, and call the method "anotherMethod" directly from the gradle script? The output would then simply be:
In anotherMethod
arg1 arg2
Thanks
you have to add the jar or class to the classpath. here is an example with a jar file who contains the class.
Inside the file build.gradle add the dependencies.
My jar file is in the lib folder the path is lib/MQMonitor.jar.
import mypackage.MyClass
buildscript {
repositories {
flatDir name: 'localRepository', dirs: 'lib'
}
dependencies {
classpath name: 'MQMonitor'
}
}
task myTaskCallJava << {
MyClass.foo()
}
Assuming the class is on the buildscript classpath (it should be, since you're calling main from the same class)
task runSimple {
doLast {
com.test.model.JavaTest.anotherMethod("foo", "bar")
}
}
Tested on Gradle 4.6
I have been working on this too. You know I like the function of eclipse and intellij with the run with Junit options, and I want to do this using command line and gradle.
If you could accept putting your test method in the directory of 'test' directory of gradle. I actually have a fair solution.
package com.udacity.gradle;
import org.junit.Test;
public class TestClass {
#Test
public void anotherMethod() {
System.out.println("This is it, I want this!!!");
}
#Test
public void notMyWantedMethod1() {
System.out.println("not my wanted");
}
public void notMyWantedMethod2() {
System.out.println("not my wanted");
}
}
This my test class which is in src/test/java/com/udacity/gradle/TestClass.java
Then the under below is the file of my build.gradle
apply plugin: "java"
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
test {
testLogging.showStandardStreams = true
filter {
//include specific method in any of the tests
includeTestsMatching "*.TestClass.anotherMethod"
}
}
Simple idea you know this is a test class so I use the test task of gradle. And to specify which method to use, I added a Test filter which could specify down to method.
Then you could just run
gradle test
Then you could find in console that you have what you want in there. However, remember to add
testLogging.showStandardStreams = true
if you don't do this, gradle would swallow your console output. But even if you don't add this line. You could read a test log in the directory of
...../build/test-results/test/TEST-com.udacity.gradle.TestClass.xml
There are well organized test reports output in it.
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="com.udacity.gradle.TestClass" tests="1" skipped="0" failures="0" errors="0" timestamp="2018-03-31T19:26:44" hostname="hexin-PC" time="0.022">
<properties/>
<testcase name="anotherMethod" classname="com.udacity.gradle.TestClass" time="0.022"/>
<system-out><![CDATA[This is it, I want this!!!
]]></system-out>
<system-err><![CDATA[]]></system-err>
</testsuite>
If you want to execute a static method you will need to add the class to the Gradle build script's classpath.
To add the code to the build scripts classpath if your code is in a repository:
buildscript {
repositories {
maven { url "${yourRepositoryURL}" }
}
dependencies {
classpath 'com.yourgroup:yourpackagename:version'
}
}
To add the code to the build scripts classpath if you code is built locally (I didn't test this one):
buildscript {
dependencies {
classpath files("path/to/where/the/class/files/are")
}
}
Then you should be able to call that method just like any other:
task runSimple(dependsOn: 'classes') {
doFirst() {
com.test.model.JavaTest.anotherMethod('arg1', 'arg2')
}
}