Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find symbol assertEquals

I'm trying to write my first unit tests for a calculator, but NetBeans says it can't find the symbol assertEquals and annotation @Test.
Should i include something?
I'm using NetBeans 7.3.1 and W7.

package calculator;

import org.junit.Assert.*;

public class UnitTests{

    @Test
    public void checkAdd(){
        assertEquals(2, Calculator.rpnCalc(" 2 3 + "));
    }
}

EDIT: Thanks guys, importing it as static helped. Test annotation required only including

import org.junit.Test;

like image 641
Giome Pool Guy Avatar asked Dec 17 '13 10:12

Giome Pool Guy


People also ask

What does Cannot find symbol mean in Java?

The cannot find symbol error, also found under the names of symbol not found and cannot resolve symbol , is a Java compile-time error which emerges whenever there is an identifier in the source code which the compiler is unable to work out what it refers to.

How do you check assertEquals in JUnit?

The assertSame() method tests if two object references point to the same object. The assertNotSame() method tests if two object references do not point to the same object. void assertArrayEquals(expectedArray, resultArray); The assertArrayEquals() method will test whether two arrays are equal to each other.

What is the method assertEquals () used for?

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.


2 Answers

assertEquals is a static method. Since you can't use static methods without importing them explicitly in a static way, you have to use either:

import org.junit.Assert;
...
Assert.assertEquals(...)

or:

import static org.junit.Assert.assertEquals;
...
assertEquals(...)

For @Test it's a little bit different. @Test is an annotation as you can see by the @. Annotations are imported like classes.

So you should import it like:

import org.junit.Test;

Generally avoid using wildcards on imports like import org.junit.*. For reasons see Why is using a wild card with a Java import statement bad?.

like image 97
bobbel Avatar answered Oct 16 '22 08:10

bobbel


JUnit 5 Jupiter

In JUnit 5 the package name has changed and the Assertions are at org.junit.jupiter.api.Assertions and Assumptions at org.junit.jupiter.api.Assumptions

So you have to add the following static import:

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

See also http://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions

like image 36
isapir Avatar answered Oct 16 '22 10:10

isapir