JUnit5: Trouble Finding AssertEquals

7.4k views Asked by At

I've set up JUnit in IntelliJ IDEA and have a bunch of tests with nothing in them. When I run them, they all pass as expected. However, when I type "assertEquals", it shows up in red. When I hover over it, it says "Cannot resolve method."

I've googled around and it looks like I need to do:

import static org.junit.Assert.*;

However, when I start typing import static org.junit., the next options are "*", "jupiter", or "platform"...

For reference, here's what a sample test looks like in my IDE:

@org.junit.jupiter.api.Test
void isButton() {
    assertEquals()
}

Any idea how to fix this?

Thanks!

2

There are 2 answers

0
Andrii Abramov On

The full path to Assertions class is:

org.junit.jupiter.api.Assertions.assertEquals

Ensure you have added Jupiter API to your dependencies:

Gradle:

dependencies {
    testCompile("org.junit.jupiter:junit-jupiter-api:5.9.0")
}

Maven:

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.9.0</version>
    <scope>test</scope>
</dependency>

There is a nice guide for Intellij IDEA and JUnit 5. Take a look at it: Using JUnit 5 in IntelliJ IDEA

0
Basil Bourque On

Maven

Verify your dependency specified in your POM file. You should have following nested within your dependencies element.

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.4.0-RC1</version>
    <scope>test</scope>
</dependency>

If calling the assertions outside of your test classes, in your regular app classes, drop the <scope>test</scope> element.

Example class

Here is an example trivial test.

package work.basil.example;


import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Unit test for simple App.
 */
public class AppTest 
{
    /**
     * Rigorous Test :-)
     */
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

New junit-jupiter artifact

Note that as of 5.4.0 of JUnit, we can specify the new and very convenient single Maven artifact of junit-jupiter which in turn will supply 8 libraries to your project.