Cannot find symbol assertEquals

后端 未结 6 1601
庸人自扰
庸人自扰 2021-02-02 07:37

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

相关标签:
6条回答
  • 2021-02-02 07:57

    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

    0 讨论(0)
  • 2021-02-02 07:59

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

    0 讨论(0)
  • 2021-02-02 08:02

    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

    0 讨论(0)
  • 2021-02-02 08:06

    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

    0 讨论(0)
  • 2021-02-02 08:10

    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 讨论(0)
  • 2021-02-02 08:11

    You have to add the dependency to pom.xml file

    <dependency>
      <groupId>junit</groupId>          
      <artifactId>junit</artifactId>            
      <version>4.12</version>       
    </dependency>
    
    0 讨论(0)
提交回复
热议问题