I am trying to integrate and setup the JUnit 5 libraries with IntelliJ on my linux distro. I added JUnit to my gradle, and built it using gradle. But I am still seeing an error on my unit tests prompting me to "add junit to my classpath" even though it already is.
Here is my build.gradle
id 'java'
id 'idea'
}
group 'com.techchallenge'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testCompile('org.junit.jupiter:junit-jupiter-api:5.3.1')
testCompile('org.junit.jupiter:junit-jupiter-engine:5.3.1')
}
test {
useJUnitPlatform()
}
UPDATE
See screenshot. I added the dependency and it is still prompting me to add the junit to my classpath though I already did
UPDATE 2
I updated the build.gradle and also did gradlew clean build test which was successful. But it is still showing the error with my junit annotations- and keeps prompting me to add junit 5.4 to my classpath though it is there.
id 'java'
id 'idea'
}
group 'com.techchallenge'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
implementation 'org.junit.jupiter:junit-jupiter:5.4.2'
testCompile('org.junit.jupiter:junit-jupiter-api:5.4.2')
testRuntime('org.junit.jupiter:junit-jupiter-engine:5.4.2')
testRuntime('org.junit.vintage:junit-vintage-engine:5.4.2')
}
test {
useJUnitPlatform()
}
It should recognise your setup. You may try adding junit5 dependencies (which is how junit5 is added creating a Gradle project from scratch using IntelliJ's New Project option) as below then reload your Gradle Project.
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
}
You may try File | Invalidate caches/Restart... option to get rid of odd issues if your project runs tests successfully using gradle test cli command
https://www.jetbrains.com/help/idea/invalidate-caches.html
You have to use the right imports for Junit 5, You are using Junit4 imports. Since version 5 #Test, Assertions, etc are within org.junit.jupiter.api package. e.g:
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DummyTest {
#BeforeEach
void setUp() {
}
#AfterEach
void tearDown() {
}
#Test
void name() {
Assertions.assertTrue(true);
}
You may look into junit5-jupiter-starter-gradle
I had a similar problem with JUnit 5 using Maven. I discovered that IDEA had created some *.iml files where it should not have. I had one in the test folder called test.iml. After deleting them the problems went away.
Please check if IDEA did not create some sneaky modules unexpectedly.
I see in the screenshot that you have one of those:
My advise is to delete it.
Make sure you have correctly setup gradle inside your intellij like for maven
and make sure You have installed JUnit plugin for IntelliJ IDEA,
you can even point gradle to your local repository like
repositories {
flatDir {
dirs 'C:/path/to/local/directory'
}
}
dependencies {
compile name: 'name-of-jar'
}
Related
I am currently running the latest IntelliJ and I am trying to use SparkJava Webserver for a Gradle project I'm writing.
When I do compile and run with gradle everything works fine, however I am getting an error in
import static spark.Spark.*;
Where IntelliJ cannot resolve the symbol spark, even though I obviously have the correct dependency set in my build.gradle:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
implementation 'mysql:mysql-connector-java:8.0.27'
implementation "com.sparkjava:spark-core:2.9.3"
implementation "org.slf4j:slf4j-simple:1.7.9"
}
test {
useJUnitPlatform()
}
Is there any way to circumvent this?
Many thanks in advance.
I got it:
In IntelliJ, under View -> Tool Windows -> Gradle there's an option to refresh everything Gradle related at the following button:
Using this refreshes the cache and yields the correctly imported dependency.
I cannot run tests via Gradle in IntelliJ IDEA because of "No tests found for given includes" error.
How can I fix it?
GradleTests
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GradleTests {
#Test
public void initTest() {
assertTrue(true);
}
}
build.gradle
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
//testCompile group: 'junit', name: 'junit', version: '4.12'
// https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api
testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.0'
}
test {
useJUnitPlatform()
}
Error:
> Task :test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':test'.
> No tests found for given includes: [GradleTests.initTest](filter.includeTestsMatching)
Some notes:
Issue is reproduced with both JUnit 4 and 5
IntelliJ IDEA 2019.3.3 (Community Edition), Build #IC-193.6494.35, built on February 11, 2020
Test is in src/test/java
changing runner like Intelij 2019.1 update breaks JUnit tests didn't help
without useJUnitPlatform() result is the same
I had this error with a similar setup, but couldn't solve it with the previous answers. Resolved it by doing this.
File > Setting (Ctrl+Alt+S)
Build, Execution, Deployment > Build Tools > gradle
Run Tests using: Intellij IDEA
All credit to: https://linked2ev.github.io/devsub/2019/09/30/Intellij-junit4-gradle-issue/.
Thanks to Ben Watson I've found solution. Since JUnit 5.4.0 there is aggregate artifact with both api and engine dependencies. So just adding one dependency to build.gradle resolved this issue.
testCompile ('org.junit.jupiter:junit-jupiter:5.6.0')
Gradle has no idea where to look for tests. Add this to your app build.gradle file root(Not inside android or dependencies closure):
tasks.withType(Test) {
useJUnitPlatform()
testLogging { // This is for logging and can be removed.
events("passed", "skipped", "failed")
}
}
/app/build.gradle
android {
testOptions {
unitTests.all {
useJUnitPlatform()
}
}
}
Another cause of this behaviour when using IntelliJ to edit a Kotlin project is the folders used for the tests, Java classes need to be in a subfolder of java and Kotlin classes in a subfolder of kotlin.
Here's an example of a mini-project I created which demonstrates the folder structure https://github.com/znsio/AutomatedTestingWithKotlin/tree/main/src/test
I found the explanation which is quoted below together with the link to the source:
"My problem was in single path to kotlin and java tests. So kotlin tests are in root/src/test/kotlin/package and they are running fine with gradle :cleanTest :test and java tests must be in root/src/test/java/package. Otherwise neither compileTestKotlin nor compileTestJava will not find java tests to compile."
https://discuss.kotlinlang.org/t/not-able-to-run-unit-tests-on-kotlin-multiplatform-project/15779/7
Gradle is case sensitive in choosing its selector. See here You may need to change "GradleTests" to "gradleTests"
I've got an Error: No tests found for given includes: ... (filter.includeTestsMatching).
After I checked JUnit dependency I discovered junit-4.13.1 version.
Fixed with replacing this dependency to org.junit.jupiter.api-5.7.0.
In my case, I had the wrong import.
I needed import org.junit.jupiter.api.Test; instead of import org.junit.Test;
Make sure that the test method is granted public access.
After going through all configurations and finding nothing, a simple gradle clean fixed this very persistent issue for me, and i could keep using "run tests with gradle".
./gradlew :domain:test --tests "..."
> Task :domain:test FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':domain:test'.
> No tests found for given includes: ...
./gradlew clean
./gradlew :domain:test --tests "..."
BUILD SUCCESSFUL in 8s
My problem was the filename that contained the test class did not match the class name. So I had AppRepositoryTest.kt with FetchDataUseCaseTest test class inside. Apparently Junit 4 does not like that, so once I made both names the same it started working.
This question has been asked before (e.g. here), but my observation was not the same as those previously reported.
I noticed that to get JUnit 5 to work, I must include the overall JUnit 5 artifact
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
If I, instead, included the individual artifacts, then the JUnit test would not be picked up
testImplementation('org.junit.platform:junit-platform-runner:1.3.1')
testImplementation('org.junit.platform:junit-platform-launcher:1.0.0')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.1')
testImplementation('org.junit.vintage:junit-vintage-engine:5.5.1')
Has anyone seen something similar before?
(I also tried this with a non-Spring-Boot project -- in that case it was okay to include the individual artifacts. This was causing a lot of confusion.)
Here I am showing the result with gradle, but I have had similar result with maven too.
I am using Gradle 5.4.1, Spring Boot 2.1.7.RELEASE, and JUnit 5.5.1
I am including the full build.gradle and the test class below
build.gradle
plugins {
id 'org.springframework.boot' version '2.1.7.RELEASE'
id 'java'
}
apply plugin: 'io.spring.dependency-management'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
// testImplementation('org.junit.platform:junit-platform-runner:1.3.1')
// testImplementation('org.junit.platform:junit-platform-launcher:1.0.0')
// testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
// testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
// testImplementation('org.junit.jupiter:junit-jupiter-params:5.5.1')
// testImplementation('org.junit.vintage:junit-vintage-engine:5.5.1')
}
test {
useJUnitPlatform()
}
DemoApplicationTest.java
package com.example.demo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class DemoApplicationTests {
#Test
public void failMe() {
Assertions.assertTrue(Boolean.FALSE);
}
}
Note that in this case I was expecting an exception be thrown in the test method failMe() -- so as to prove that the test method had been picked up by the runner and was not silently ignored.
Thanks for the hint from #johanneslink (in the comment of the opening question), now I think I understand better the issues:
It is better to use the aggregate artifact
testImplementation('org.junit.jupiter:junit-jupiter:5.5.1')
If you really want to use the individual artifacts, make sure their versions are compatible
This combination would work
testImplementation('org.junit.platform:junit-platform-launcher:1.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
But not this
testImplementation('org.junit.platform:junit-platform-launcher:1.3.1')
testImplementation('org.junit.jupiter:junit-jupiter-engine:5.5.1')
testImplementation('org.junit.jupiter:junit-jupiter-api:5.5.1')
(The other three artifacts are not relevant so I am omitting them here. For example, according to the JUnit 5 User Guide
junit-platform-runner
Runner for executing tests and test suites on the JUnit Platform in a JUnit 4 environment.
and is not relevant here.)
I basically want to do something simple - or atleast i think it should be pretty simple.
My goal is to create an Intellij gradle project, add some dependencies to the module using gradle and add some java source code to it.
Then I just want to have an option to somehow compile the whole thing into 1 jar, containing all grade dependencies and being able to execute using "java -jar"
However it turned out that this is not as easy is i had thought.
I just created a new gradle project from intellij and added a Main class.
I´ll give you an overview over my files:
settings.gradle:
rootProject.name = 'gradleTestNewJar'
build.gradle:
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
sourceCompatibility = 1.6
version = '1.0'
repositories {
mavenCentral()
}
mainClassName = "com.randomPackage.StarterClass"
dependencies {
compile 'org.seleniumhq.selenium:selenium-java:2.46.0'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
main class:
package com.randomPackage;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class StarterClass {
public static void main(String[] args){
System.out.println("test");
WebDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
driver.quit();
}
}
The main method of "MyStart" is executed when running from Intellij via debug.
So it works, when all dependencies get loaded correctly.
NOTE: I use Intellij Community Edition if this makes any difference.
What i tried:
1. I tried to just use "gradlew clean build".
This created a jar, but without libs.
But I didn´t expect it to be as easy as this.
2. I tried to build an artifact of the module as suggested here:
http://blog.jetbrains.com/idea/2010/08/quickly-create-jar-artifact/
I tried it with extracted and not extracted dependencies.
In both cases the dependencies were added into the jar, but they were added to the root of the jar.
When i tried to run the jar file via "java -jar", it complained:
"Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
..."
OK, so it couldn´t load the dependencies.
NOTE: I thought that the dependencies were not added to the classpath, but i am not sure about this. However, i would expect Intellij to add dependencies to the classpath( or declare in the manifest file)
3. I also tried to use the gradle application plugin.
However this creates a zip/tar which contains a execute script and a bin folder which was not my intention.
So i started googling for hours and hours but i cann´t find a solution to my problem.
Come on this cannot be so hard - it is just so basic.
I am sure some genius can help me out and point me to my - probably stupid - failure.
My current solution is as follows:
I use gradle to build a jar containing all libs, I do this witha custom task called fatJar.
Here is a part from my build.gradle
apply plugin: 'java'
jar {
manifest {
attributes("Manifest-Version": "1.0",
"Main-Class": "com.randomPackage.MainClass");
}
}
task fatJar(type: Jar) {
manifest.from jar.manifest
classifier = 'all'
from {
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
} {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
with jar
}
Then I just execute "gradle fatJar" on the command line and get a perfect jar.
I am very new with Gradle and I would like to download all my test dependencies using Gradle. I used gradle init to generate my build file, and copied some dependencis from my previous scripts. However upon using gradle --refresh-dependencies in the project root, the test dependencies still do not download.
I tried searching for answers as to why this happens, but they don't seem to fix my issue. Is there something wrong with my build file?
Particularly, I am after downloading mockito and hamcrest below.
build file:
apply plugin: 'java'
repositories {
mavenCentral()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.5'
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-core:1.9.5"
testCompile "org.hamcrest:hamcrest-library:1.3"
}
test {
testLogging {
events 'started', 'passed'
}
}
task wrapper(type: Wrapper) { gradleVersion = '1.11' }
I am also using Eclipse if that helps.
EDIT: After adding the Gradle nature to my project, it seemed to work. Can anyone explain why?
As far as I understand, --refresh-dependencies makes sure that the already cached dependencies are ignored and that dependencies are re-downloaded when they're needed.
Just use gradlew build, or gradlew test, and gradle will compile your tests, and thus download the missing dependencies.
It seemed to work now, after I added the Gradle nature to my project, then cleaning and regenerating eclipse files.