Stav Alfi
Stav Alfi

Reputation: 13923

Including JUnit 5 dependency in IntelliJ IDEA

From jetbrains blog:

IntelliJ IDEA supports the ability to actually run tests written for JUnit 5 – there’s no need to use the additional libraries (like the Gradle or Maven plugins for example), all you need is to include the JUnit 5 dependency.

I'm new to Java and IntelliJ IDEA and it's not clear to me what are the steps that I should do for making test using Junit 5.

Upvotes: 21

Views: 51706

Answers (5)

GreenMarty
GreenMarty

Reputation: 382

There is much simpler way to generate test class in IntelliJ IDEA.
(Works on IntelliJ IDEA 2022.3.3)

  1. Click on class you want to test and press alt+enter or click yellow lightbulb > pick Create Test start test class generation

  2. Setup where and how you wish to generate test class.
    ( Please note that IntelliJ IDEA will detect if you have appropriate JUnit and prompts you to for automatic download if you don't. )testing class generation setup

  3. Write your test class code enter image description here

You can find more details in jetbrains documentation

Upvotes: 0

Qasim Ali
Qasim Ali

Reputation: 107

I made this work by adding this to my pom:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.0.0-M4</version>
    <scope>test</scope>
</dependency>       
<dependency>
    <groupId>org.junit.platform</groupId>
    <artifactId>junit-platform-launcher</artifactId>
    <version>1.0.0-M4</version>
    <scope>test</scope>
</dependency>

Upvotes: 7

nahs
nahs

Reputation: 143

Got the latest IntelliJ IDEA (2017.3), being able to add JUnit5 library when creating test class in IntelliJ, but still failed to find tests. Tried the suggestion by @CrazyCoder, and found out the org.junit.jupiter.api has existed in my IntelliJ and has the version 5.0.0-M6. And finally solved by downloading org.junit.platform:junit-platform-commons:1.0.0-M6 from Maven Repository and adding it into classpath.

For someone like me, new to the IntelliJ, the detailed steps I followed:

  1. Open Project Settings -> Libraries -> + New Project Library -> from Maven...
  2. Search and add org.junit.platform:junit-platform-commons:1.0.0-M6
  3. Modules -> module name you want to add it to -> Dependencies -> + 2 Library ... (should have library jar listed)

Upvotes: 0

AAryan
AAryan

Reputation: 20140

Previously you need plugin to run unit test like this

buildscript {
    repositories {
        mavenCentral()
        // The following is only necessary if you want to use SNAPSHOT releases.
        // maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M2'
    }
}

apply plugin: 'org.junit.platform.gradle.plugin'

But for JUnit5 no need of plugin just compile

dependencies {
     testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.0-M2'
}

Upvotes: 3

CrazyCoder
CrazyCoder

Reputation: 401975

If your project is Maven or Gradle based, the dependency is added via pom.xml or build.gradle, otherwise you just add the .jar files to the Module Dependencies.

IDE can help you with that, press Alt+Enter on the red code:

add library

The following dependencies will be downloaded from the Maven repository and added to the classpath:

deps

Upvotes: 26

Related Questions