【问题标题】:JUnit5: Trouble Finding AssertEqualsJUnit5:找不到 AssertEquals
【发布时间】:2017-05-21 14:25:40
【问题描述】:

我在 IntelliJ IDEA 中设置了 JUnit,并进行了一堆测试,其中没有任何内容。当我运行它们时,它们都按预期通过。但是,当我键入“assertEquals”时,它显示为红色。当我将鼠标悬停在它上面时,它会显示“无法解析方法”。

我用谷歌搜索了一下,看起来我需要这样做:

import static org.junit.Assert.*;

但是,当我开始输入 import static org.junit. 时,下一个选项是“*”、“jupiter”或“平台”...

作为参考,下面是我的 IDE 中的示例测试:

@org.junit.jupiter.api.Test
void isButton() {
    assertEquals()
}

知道如何解决这个问题吗?

谢谢!

【问题讨论】:

    标签: java intellij-idea junit junit5 junit-jupiter


    【解决方案1】:

    Assertions 类的完整路径是:

    org.junit.jupiter.api.Assertions.assertEquals
    

    你的dependencies 块应该是这样的:

    dependencies {
        testCompile("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
    }
    

    Intellij IDEA 和 JUnit 5 有一个很好的指南。 看看吧:Using JUnit 5 in IntelliJ IDEA

    【讨论】:

      【解决方案2】:

      Maven

      验证您在 POM 文件中指定的依赖项。您应该在 dependencies 元素中嵌套以下内容。

      <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
      <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter</artifactId>
          <version>5.4.0-RC1</version>
          <scope>test</scope>
      </dependency>
      

      如果在测试类之外调用断言,请在常规应用类中删除 &lt;scope&gt;test&lt;/scope&gt; 元素。

      示例类

      这是一个简单的测试示例。

      package work.basil.example;
      
      
      import org.junit.jupiter.api.Test;
      
      import static org.junit.jupiter.api.Assertions.assertTrue;
      
      /**
       * Unit test for simple App.
       */
      public class AppTest 
      {
          /**
           * Rigorous Test :-)
           */
          @Test
          public void shouldAnswerWithTrue()
          {
              assertTrue( true );
          }
      }
      

      新的junit-jupiter 工件

      请注意,从 JUnit 5.4.0 开始,我们可以指定 junit-jupiter 的新的非常方便的单个 Maven 工件,这反过来将为您的项目提供 8 个库。

      【讨论】:

        猜你喜欢
        • 2014-01-05
        • 1970-01-01
        • 1970-01-01
        • 2021-10-14
        • 2020-08-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-01
        相关资源
        最近更新 更多