title: How to use JUnit 5 with Gradle?
body: <p>I am trying to use JUnit 5 with Gradle after I succeeded in running a JUnit 4 test.</p>
  
  <p><strong>Expected result:</strong> Tthe JUnit 4 test gave a nice 'passed' in the output and an html report in <code>build/reports/tests</code>.</p>
  
  <p><strong>Actual result:</strong> The JUnit 5 test as below does not output anything besides <code>(...) build succesful</code>, while I know the test is not actually run since there is no test log output passed/skipped/failed, and putting a <code>fail</code> in the test keeps the build successful.</p>
  
  <p>Running <code>gradle test --info</code> yields <code>Skipping task ':testClasses' as it has no actions.</code> among a lot of I think mostly unrelevant output.
  Surprisingly, it also says <code>Executing task ':test'</code> and <code>Generating HTML test report... Finished generating test html results</code> and similar for the xml in <code>build/test-results/test</code>, while the xml is not generated, the html shows no tests run and no errors, and the test is indeed not run.</p>
  
  <p>What I also think very interesting, is that <code>gradle test --debug</code> yields</p>
  
  <pre><code>[TestEventLogger] Gradle Test Run :test STARTED
  [org.gradle.api.internal.tasks.testing.junit.JUnitDetector] test-class-
  scan : failed to scan parent class java/lang/Object, could not find the class file
  [TestEventLogger]
  [TestEventLogger] Gradle Test Run :test PASSED
  </code></pre>
  
  <p>while my only test contains</p>
  
  <pre><code>fail("test fails");
  </code></pre>
  
  <p>which I think is very strange!</p>
  
  <p>My build file is</p>
  
  <pre class="lang-groovy prettyprint-override"><code>apply plugin: 'java'
  
  test {
      dependsOn 'cleanTest' // run tests every time
  
  }
  
  sourceSets {
      main {
          java {
              srcDirs 'src'
          }
      }
      test {
          java {
              srcDirs 'test'
          }
      }
  }
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
      // when using this, it worked with a junit 4 test
  //    testCompile 'junit:junit:4.10'
      // this should be needed for junit 5 (using M4 is required since IJ 2017.1.2
      testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M4")
  }
  
  test {
      testLogging {
          events "passed", "skipped", "failed"
      }
  }
  </code></pre>
  
  <p>My test is</p>
  
  
  
  <pre><code>package mypackage;
  
  import org.junit.jupiter.api.Test;
  import static org.junit.jupiter.api.Assertions.assertEquals;
  
  public class HelloWorldTest {
      @Test
      public void testHelloWorld(){
          assertEquals(2, 1+1, "message");
      }
  }
  </code></pre>
  
  <p>My folder structure is, using package <code>mypackage</code>,</p>
  
  <pre><code>java-template-project
  --- src
      --- mypackage
          --- HelloWorld.java
  --- test
      --- mypackage
          --- HelloWorldTest.java
  </code></pre>
  
  <p>and in IntelliJ 2017.1.3, which I am using, the module structure looks like this</p>
  
  <pre><code>java-template-project
  --- java-template-project_main
      --- src/mypackage
          --- HelloWorld(.java)
  --- java-template-project_test
      --- test/mypackage
          --- HelloWorldTest(.java)
  </code></pre>
  
  <p>because Gradle nowadays wants the source and tests in their own package.</p>
  
  <p><strong>What I tried</strong></p>
  
  <p>Obviously this is not the first question about this topic, all the relevant questions I found are</p>
  
  <ul>
  <li><p><a href="https://stackoverflow.com/questions/38293901/gradle-project-running-junit-5-tests-in-intellij">Gradle project running jUnit 5 tests in IntelliJ</a></p>
  
  <p>But as you can see this is for older versions of IntelliJ, and I am already using the syntax for IJ 2016.3.3 and higher according to one of the answers, in  in the one JUnit dependency line, so that should be okay.</p></li>
  <li><p><a href="https://stackoverflow.com/questions/38576011/upgrade-from-junit-4-to-junit-5-in-intellij-with-gradle">Upgrade from JUnit 4 to JUnit 5 in intellij with gradle</a></p>
  
  <p>Links back to above question, and links to this <a href="https://blog.jetbrains.com/idea/2016/08/using-junit-5-in-intellij-idea/" rel="noreferrer">Jetbrains blog</a> which uses the same line as above question. Also links to:</p></li>
  <li><p><a href="https://stackoverflow.com/questions/38576108/integrate-junit-5-tests-results-with-intellij-test-report">Integrate JUnit 5 tests results with Intellij test report</a>
  This one shows, in the question, as dependency also</p>
  
  <pre><code>testRuntime("org.junit.vintage:junit-vintage-engine:5.0.0-M1")
  </code></pre>
  
  <p>which is explained in <a href="https://stackoverflow.com/questions/38822189/why-were-junit-jupiter-and-junit-vintage-separated-when-i-running-testcase-in-in">Why were JUnit Jupiter and JUnit Vintage separated When I Running TestCase in IntelliJ?</a>
  Well, when I ran it, the output showed it couldn't find this version but according to the <a href="https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine/4.12.0-M4" rel="noreferrer">Maven Repository</a> this one is for JUnit 5:</p>
  
  <pre><code>testRuntime("org.junit.vintage:junit-vintage-engine:4.12.0-M4")
  </code></pre>
  
  <p>The answers there note that you can just run the tests within IntelliJ since the later versions have JUnit 5 support. I know, and the test runs fine when I run from within IntelliJ. But I want to use Gradle (and Travis, which needs dependency management).</p></li>
  <li><p><a href="https://stackoverflow.com/questions/43151448/how-to-capture-stdout-stderr-in-junit-5-gradle-test-report">How to capture stdout/stderr in junit 5 gradle test report?</a></p>
  
  <p>I tried using </p>
  
  <pre><code>testCompile("org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3")
  testCompile("org.junit.jupiter:junit-jupiter-engine:5.0.0-M3")
  </code></pre>
  
  <p>but results didn't change.</p>
  
  <p>My template project is located on <a href="https://github.com/PHPirates/java-template-project" rel="noreferrer">https://github.com/PHPirates/java-template-project</a> but this question should contain all information necessary.</p></li>
  </ul>