Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JUnit测试在同一个包中找不到父类_Java_Intellij Idea_Junit5 - Fatal编程技术网

Java JUnit测试在同一个包中找不到父类

Java JUnit测试在同一个包中找不到父类,java,intellij-idea,junit5,Java,Intellij Idea,Junit5,我在IntellijIdea,试图从我的测试中提取一些逻辑,以便在其他测试中重用。问题是每次我创建另一个类(在同一个测试包中);测试运行正常,但随后停止查找另一个文件,抛出“java:cannotfind symbol”错误。每当我对原始文件进行更改时,似乎都会发生这种情况。对于我尝试创建的任何新类都是一样的,而不仅仅是本例中的抽象父类 我在这里搜索过类似的问题,因为测试一开始是有效的,所以看起来很有希望。但是,当我检查时,没有脚本例外 目前,我正在运行我的单元测试,只需右键单击我的测试文件夹并

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

我在这里搜索过类似的问题,因为测试一开始是有效的,所以看起来很有希望。但是,当我检查时,没有脚本例外

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

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

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

请注意,添加了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命令处理;
导入GameLogic.Game;
导入org.junit.jupiter.api.beforeach;
导入java.lang.reflect.Field;
导入静态org.junit.jupiter.api.Assertions.fail;
公共抽象类输入模拟器{
受保护的游戏;
受保护的命令解析器命令解析器;
@之前
无效设置(){
游戏=新游戏();
试一试{
Class gameClass=game.getClass();
Field cmdField=gameClass.getDeclaredField(“commandParser”);
cmdField.setAccessible(true);
commandParser=(commandParser)cmdField.get(游戏);
}捕获(例外e){
不合格(e);
}
}
受保护的无效模拟输入(字符串输入){
commandParser.handleInput(输入);
}
类输入用例{
私有字符串输入;
应为私有字符串;
公共InputTestCase(字符串输入,应为字符串){
这个输入=输入;
this.expected=expected;
}
公共字符串getInput(){return input;}
公共字符串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);
        }
    }
}
package命令处理;
导入数据库。*;
导入GameLogic.Game;
进口船舶*;
导入IOHandler.*;
导入org.junit.jupiter.api.beforeach;
导入org.junit.jupiter.api.Test;
导入java.lang.reflect.Field;
导入java.util.ArrayList;
导入静态org.junit.jupiter.api.Assertions.*;
类InfoTest扩展了InputSimulator{
私有最终输入测试用例[]测试用例=
{
新输入用例(“信息cpit”,“驾驶舱-健康:100.0,防护:100.0”),
新输入用例(“信息工程”,“发动机-健康:100.0,防护:100.0”),
新的输入用例(“信息shld”,“盾牌-健康:100.0,盾牌:100.0”),
新输入用例(“信息武器”,“武器-健康:100.0,盾牌:100.0”),
新输入用例(“信息油箱”、“燃油箱-运行状况:100.0,防护罩:100.0”),
新输入测试用例(“信息车”,“货货舱-健康:100.0,防护:100.0”)
};
@试验
void printPartsOnInfo(){
模拟输入(“信息”);
应为ArrayList=新的ArrayList();
应为.add(game.dbAccess().getMessage(Message.Info));
对于(PartType零件:PartType.values()){
字符串partString=part.toString();
应为.add(partString);
}
ArchiveAccess ArchiveAccess=game.getArchiveAccess();
ArrayList lines=archiveAccess.getOutput();
资产质量(预期,行);
}
@试验
void printPartDetails(){
for(InputTestCase测试用例:测试用例){
模拟输入(testCase.getInput());
ArchiveAccess ArchiveAccess=game.getArchiveAccess();
字符串行=archiveAccess.lastOutput();
直线=直线;
assertEquals(testCase.getExpected(),第行);
}
}
}

所有错误:

第一次试运行良好:

不包括:


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

事实上,我曾经有过正确的答案,但读得不好:

测试根文件夹应位于主文件夹的旁边,主文件夹应标记为源根文件夹,而不是其父“src”文件夹。我已经将这个父目录标记为源根目录——也就是说,默认的源根目录已经被称为“src”,并且我设法将我的测试根文件夹放在其中,这是一个很难复制的项目结构。因为这个名字,我感到困惑


首先,我是如何让它工作的,这对我来说是一个谜,因为当我试图重现这个问题时,它需要走一些弯路才能将我的测试根放在源根中。即使这样,测试在运行时也找不到JUnit包(尽管他们编译得很好)。

新的示例项目是否可以复制JUnit包?谢谢,y.bedrov,尝试复制错误让我意识到了问题所在!作为补充说明,最好避免测试中的继承。所以不要从其他测试中创建测试的子类tests@ACV非常感谢你指出这一点!研究它给了我一些很好的见解。