Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

junit

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.

like image 846
SOF User Avatar asked Mar 27 '14 00:03

SOF User


People also ask

Can you use assertEquals for strings?

IIRC assertEquals() succeeds if both strings are null. If this is not what you want then call assertNotNull() as well.

What does the assertEquals () method do?

assertEquals. Asserts that two objects are equal. If they are not, an AssertionError is thrown with the given message. If expected and actual are null , they are considered equal.

What can we use instead of assertEquals?

Use assertThat instead An alternative option to assertEquals is assertThat .


1 Answers

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.

like image 139
Sotirios Delimanolis Avatar answered Oct 27 '22 00:10

Sotirios Delimanolis