57

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;

bobbel
  • 3,327
  • 2
  • 26
  • 43
Giome Pool Guy
  • 607
  • 1
  • 6
  • 8

7 Answers7

90

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?.

Community
  • 1
  • 1
bobbel
  • 3,327
  • 2
  • 26
  • 43
39

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

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
isapir
  • 21,295
  • 13
  • 115
  • 116
  • 1
    wow, this worked. With the JUnit 4 static imports, `import static org.junit.Assert.assertTrue;` I was able to pass the unit test in the IDE. But when I run maven install, it complains Noclassdefound. I replaced with `import static org.junit.jupiter.api.Assertions.*;` and it works like a charm now. Thank you so much. – ever alian Jan 16 '22 at 22:28
10

I am working on JUnit in java 8 environment, using jUnit4.12

for me: compiler was not able to find the method assertEquals, even when I used
import org.junit.Assert;

So I changed
assertEquals("addb", string);
to
Assert.assertEquals("addb", string);

So if you are facing problem regarding assertEqual not recognized, then change it to Assert.assertEquals(,); it should solve your problem

Akshay Vijay Jain
  • 13,461
  • 8
  • 60
  • 73
  • 2
    If you're not using a **static** import directly on the `assertEquals` method, you can't use it in your code without the class `Assert`, see my answer! – bobbel Mar 15 '16 at 08:36
5

I was having the same problem cannot resolve symbol Assert i have tried these solutions by adding the different import from the different answers.

  1. import org.junit.Assert;
  2. import static org.junit.Assert.*;
  3. import static org.junit.Assert.assertEquals;
  4. import static org.junit.jupiter.api.Assertions.*;
  5. import org.junit.Assert;

but the solution that did the magic was just place the junit-4.12.jar in the app\lib ditectory and just build the project, and import like this

import org.junit.Assert;

you can download the junit-4.12.jar from here

Ali Tamoor
  • 896
  • 12
  • 18
1

Using IntelliJ 2019.2.4 with a start.sping.io default setup...

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

but now instead of

Assert.assertEquals(expected, actual);

use

assertEquals(expected, actual);
0

You have to add the dependency to pom.xml file

<dependency>
  <groupId>junit</groupId>          
  <artifactId>junit</artifactId>            
  <version>4.12</version>       
</dependency>
Tu Minh
  • 1
  • 1
  • The Question contained a hint from 6 years ago that the missing "import static" was the actual problem, so your answer is missing the point I'm afraid. – creinig Jul 24 '19 at 10:40
0

my project is based at maven,although I editor junit in the pom.xml,I still can't find junit in my repository,so I download the junit.jar into my repository.It works!you can try it!

  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/30279049) – Antoine Nov 08 '21 at 07:58