【问题标题】:Attempting to create JUnit build configuration results in the error "JUnit not found in module" (IntelliJ)尝试创建 JUnit 构建配置会导致错误“在模块中找不到 JUnit”(IntelliJ)
【发布时间】:2018-09-11 04:17:58
【问题描述】:

我在 Intellij 中学习 JUnit,对 IDE 有点陌生,对 Java 不太熟悉。我能够(似乎)通过 Maven 顺利安装 JUnit(导入工作和一切),但是当我尝试进行构建配置时出现奇怪的错误。当我尝试在 JUnit 构建配置模板中指定测试类时,我收到此错误:

这没有任何意义,因为我的类路径中不仅所有必需的 .jar,它们甚至直接复制到模块中的 lib 文件夹,而不是“链接”到。

我已经看到很多关于如何安装 JUnit 或使用 Maven 的问题,但我已经能够很好地做到这一点。在这个 IDE 中,我没有看到任何关于这个特定问题的信息。

(哦,以防万一,这是我正在使用的代码。它可能与问题无关,而且相当荒谬,但我想我不妨包括它)

public class ThingDoer {

    private int foo;
    private int bar;

    public int doThing(int input) {
        bar = input;
        return (foo * foo);
    }

    public int doOtherThing(int input) {
        foo = input;
        return (bar * bar);
    }

}

.

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class ThingDoerTests {

    @Test
    void testDoThing() {

        ThingDoer d = new ThingDoer();
        int a = d.doThing(10);
        Assertions.assertEquals(0, a);

    }


    @ParameterizedTest
    @ValueSource(ints = {9, 4, 54, 6})
    void testDoOtherThing(int input) {

        ThingDoer d = new ThingDoer();
        Assertions.assertTrue(d.doOtherThing(input) > -100);

    }

}

【问题讨论】:

  • 你不需要用 jUnit 注释来注释你的测试类吗?
  • @Ivan 根据JUnit 5 User Guide,您不需要注释测试,只需注释我已经做过的方法。
  • 你试过指定测试类名吗?

标签: java maven intellij-idea junit


【解决方案1】:

您的项目配置正确,但不幸的是 IntelliJ IDEA 显示的消息令人困惑。通过在 Class 字段中按 ... 按钮创建测试配置的方式仅适用于 JUnit 3 测试,消息实际上意味着“未找到 JUnit 3”。在 IntelliJ 问题跟踪器中有一个关于此问题的 open issue

要运行测试,您只需右键单击测试类或测试方法,然后从上下文菜单中选择“运行”或“调试”选项。

【讨论】:

  • 当我点击任何东西时,我看不到任何“运行”选项,但切换到 JUnit 4 有效。我猜现在已经够好了。
  • 您使用的是哪个 IntelliJ IDEA 版本? JUnit 5 仅在相当新的版本中受支持。
【解决方案2】:

在 Android Studio 中将此行添加到 build.gradle (app):

android {
    defaultConfig {
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-28
    • 2021-03-28
    • 2019-12-05
    • 2023-02-08
    相关资源
    最近更新 更多