首页 > 解决方案 > JUnit Test 在同一个包中找不到父类

问题描述

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

我在这里搜索了类似的问题,并且因为测试起初有效,所以这似乎很有希望IntelliJ can't find classes in same package when compile。但是,当我检查时,没有脚本除外。

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

我将非常感谢任何见解。

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

请注意,添加了 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; }
    }
}

2

原班:

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);
        }
    }
}

1

所有错误:

3

第一次测试运行良好:

4

不排除:

5

标签: javaintellij-ideajunit5

解决方案


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

我实际上曾经有过正确的答案,但读得很糟糕: 如何在 Intellij 13 中创建测试目录?.

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

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


推荐阅读