【问题标题】:JUnit Test not finding parent class in same packageJUnit Test 在同一个包中找不到父类
【发布时间】:2021-03-29 23:33:07
【问题描述】:

我在 IntellijIdea 中,试图从我的测试中提取一些逻辑以便在其他人中重用。问题是每次我创建另一个类(在同一个测试包中);测试运行良好,但随后停止查找另一个文件,引发“java:找不到符号”错误。每当我对原始文件进行更改时,似乎都会发生这种情况。对于我尝试创建的任何新类都是一样的,而不仅仅是本例中的抽象父类。

我在这里搜索过类似的问题,因为测试一开始就有效,这似乎很有希望IntelliJ can't find classes in same package when compiling。但是,当我检查时,没有脚本除外。

目前我正在运行我的单元测试,只需右键单击我的测试文件夹并选择“运行所有测试”,或者以相同的方式运行单个测试。

如果有任何见解,我将不胜感激。

编辑:感谢您为我解决图像格式问题。 我还按要求添加了这两个类的源代码。不过,我仍然觉得留下图像是个好主意,因为我主要是为了传达项目结构和错误。另外,我想强调的是,我想说的是代码独立问题,因为它存在于任何两个文件中。

请注意,添加了 InfoTest 的无意义的第 51 行(“line = line;”)以复制错误。对原始类的任何更改都会导致找不到包中的任何其他类。您甚至可以看到它没有出现在显示测试运行良好的图像中。

提取的类:

package CommandProcessing;

import GameLogic.Game;
import org.junit.jupiter.api.BeforeEach;
import java.lang.reflect.Field;
import static org.junit.jupiter.api.Assertions.fail;

public abstract class InputSimulator {
    protected Game game;
    protected CommandParser commandParser;

    @BeforeEach
    void setUp() {
        game = new Game();
        try {
            Class<?> gameClass = game.getClass();
            Field cmdField = gameClass.getDeclaredField("commandParser");
            cmdField.setAccessible(true);
            commandParser = (CommandParser)cmdField.get(game);
        } catch (Exception e){
            fail(e);
        }
    }

    protected void simulateInput(String input){
        commandParser.handleInput(input);
    }

    class InputTestCase {
        private String input;
        private String expected;

        public InputTestCase(String input, String expected){
            this.input = input;
            this.expected = expected;
        }

        public String getInput() { return input; }

        public String getExpected() { return expected; }
    }
}

原课:

package CommandProcessing;

import Database.*;
import GameLogic.Game;
import Ship.*;
import IOHandler.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.lang.reflect.Field;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.*;

class InfoTest extends InputSimulator {

    private final InputTestCase[] testCases =
    {
        new InputTestCase("info cpit", "Cockpit - health: 100.0, shields: 100.0"),
        new InputTestCase("info engi", "Engine - health: 100.0, shields: 100.0"),
        new InputTestCase("info shld", "Shields - health: 100.0, shields: 100.0"),
        new InputTestCase("info weap", "Weapons - health: 100.0, shields: 100.0"),
        new InputTestCase("info tank", "FuelTank - health: 100.0, shields: 100.0"),
        new InputTestCase("info carg", "CargoBay - health: 100.0, shields: 100.0")
    };

    @Test
    void printPartsOnInfo() {
        simulateInput("info");
        ArrayList<String> expected = new ArrayList<String>();
        expected.add(game.dbAccess().getMessage(Message.Info));
        for(PartType part : PartType.values()){
            String partString = part.toString();
            expected.add(partString);
        }
        ArchiveAccess archiveAccess = game.getArchiveAccess();
        ArrayList<String> lines = archiveAccess.getOutput();

        assertEquals(expected, lines);
    }

    @Test
    void printPartDetails() {
        for (InputTestCase testCase : testCases) {
            simulateInput(testCase.getInput());
            ArchiveAccess archiveAccess = game.getArchiveAccess();
            String line = archiveAccess.lastOutput();
            line = line;
            assertEquals(testCase.getExpected(), line);
        }
    }
}

所有错误:

第一次测试运行良好:

不排除:

【问题讨论】:

  • 新的示例项目是否可以重现?
  • 谢谢,y.bedrov,试图重现错误让我意识到问题所在!
  • 附带说明,最好避免在测试中继承。所以不要从其他测试创建测试的子类
  • @ACV 非常感谢您指出这一点!研究它给了我一些很好的见解。

标签: java intellij-idea junit5


【解决方案1】:

所以,问题是测试根文件夹在我的源文件夹中!

实际上我曾经有过正确的答案,但读得很糟糕: How to create a test directory in Intellij 13?.

测试根文件夹应位于主文件夹旁边,该主文件夹应标记为源根文件夹,而不是其父“src”文件夹。我已将此父目录标记为源根目录 - 也就是说,默认源根目录已被称为“src”,并且我设法将我的测试根文件夹放入其中,这个项目结构可疑地难以复制,因为它会变成.我被命名弄糊涂了。

我一开始是如何让它工作的,这对我来说是个谜,因为当我试图重现这个问题时,它需要走一些弯路才能让我的测试根进入我的源根。即便如此,测试甚至在运行时都找不到 JUnit 包(尽管它们编译得很好)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2020-11-11
    • 2015-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多