1

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.

Ahmed A
  • 2,702
  • 5
  • 31
  • 50
  • why you want to create a Test Runner, instead of just running the tests directly – gagan singh Jun 06 '18 at 22:50
  • I am just following a tutorial and trying to following the tutorial. The tutorial does everything on command line (even javac). I prefer to use build system, and choose intellij. – Ahmed A Jun 06 '18 at 22:53
  • In that case do not bother with test runner. just write your test classes in src/test/java.../TestSomething.java with @Test annotation and then simply run ./gradlew test – gagan singh Jun 06 '18 at 22:57
  • gradle junit test runner will pickup all the tests and execute for you – gagan singh Jun 06 '18 at 22:58
  • and in intellij you can simple code the test and execute the same. You will see a green arrow button next to every test method in intellij idea, just click that to execute one test. – gagan singh Jun 06 '18 at 22:59
  • @gagansingh Thank you for the info. Makes sense. – Ahmed A Jun 06 '18 at 23:07

0 Answers0