SOF User
SOF User

Reputation: 165

The method assertEquals(String, String) is undefined for the type TestJunit

I am new to JUnit. I just started working on JUnit and i am getting following error.

The method assertEquals(String, String) is undefined for the type TestJunit

and my Javacode:

import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;

public class TestJunit {

    String message = "Hello World";
    MessageUtil messageutil = new MessageUtil(message);

    public void testPrintMessage()
    {
        assertEquals(message,messageutil.printMessage());

    }
}

please help me resolve this issue.

Upvotes: 15

Views: 41154

Answers (3)

Mohit Sehgal
Mohit Sehgal

Reputation: 330

For Junit 5, I had to add following dependencies in my spring boot pom.xml files.

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

        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-runner</artifactId>
            <scope>test</scope>
        </dependency>

Upvotes: -1

Amanuel Kiya
Amanuel Kiya

Reputation: 1

if importing doesn't work try saving before you run the code

Upvotes: -4

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280122

You imported

import static org.junit.Assert.assertArrayEquals;

but not

import static org.junit.Assert.assertEquals;

You could also import every static member of Assert with

import static org.junit.Assert.*;

Without these, Java thinks you are calling the method assertEquals defined in the class the code is declared in. Such a method does not exist.

Upvotes: 31

Related Questions