Cannot find symbol assertEquals

后端 未结 6 1599
庸人自扰
庸人自扰 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: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?.

提交回复
热议问题