【发布时间】:2022-01-15 01:13:43
【问题描述】:
我刚刚学习 Java,可以使用您的帮助。我正在使用 Eclipse,并使用 org.openjfx 原型创建了一个 Maven 项目。一切似乎都很好,除非我尝试在 src/test/java 中编写测试,这会导致错误。
实例化类时出错 starcraft.warcraft.test.TestClass:无法公开 starcraft.warcraft.test.TestClass() 可访问:模块 starcraft.warcraft 不会“将 starcraft.warcraft.test”“导出”到 模块 org.testng
这就是我在 Eclipse 中使用默认设置创建项目的方式:
Project Setup with Maven Archetype Selection
现在,当 Eclipse 创建项目时,它没有 src/test/java 文件夹,所以我手动创建它。然后,我在 src/test/java 的包“starcraft.warcraft.test”中创建了一个名为“TestClass”的类,并在名为“adder”的 App 类中添加了一个简单的测试方法。可以看到项目结构
package starcraft.warcraft;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class App extends Application {
@Override
public void start(Stage stage) {
var javaVersion = SystemInfo.javaVersion();
var javafxVersion = SystemInfo.javafxVersion();
var label = new Label("Hello, JavaFX " + javafxVersion + ", running on Java " + javaVersion + ".");
var scene = new Scene(new StackPane(label), 640, 480);
stage.setScene(scene);
stage.show();
}
// WILL TEST THIS METHOD
public static int adder(int digit1, int digit2) {
return digit1 + digit2;
}
public static void main(String[] args) {
launch();
}
}
现在我想使用 TestNG 进行测试,所以我将它包含在我的 POM 中
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>starcraft</groupId>
<artifactId>warcraft</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>13</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.5</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.openjfx</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>0.0.6</version>
<executions>
<execution>
<!-- Default configuration for running -->
<!-- Usage: mvn clean javafx:run -->
<id>default-cli</id>
<configuration>
<mainClass>starcraft.warcraft.App</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
这是 Maven 原型创建的默认 POM,除了我添加的 TestNG 依赖项。当我尝试使用 TestNG 时,Eclipse 让我将其添加到模块路径中,如下所示:
Maven saying I need to add the TestNG library
这是我的模块信息:
module starcraft.warcraft {
requires javafx.controls;
requires org.testng;
exports starcraft.warcraft;
}
好的,到目前为止一切都很好,但是现在当我尝试在 TestClass 中运行我的测试时:
package starcraft.warcraft.test;
import org.testng.annotations.Test;
import starcraft.warcraft.App;
public class TestClass {
@Test
public void testAdder() {
int sum = App.adder(1, 2);
System.out.println(sum);
}
}
我得到了错误,这又是
实例化类时出错 starcraft.warcraft.test.TestClass:无法公开 starcraft.warcraft.test.TestClass() 可访问:模块 starcraft.warcraft 不会“将 starcraft.warcraft.test”“导出”到 模块 org.testng
我不知道如何进行导出。当我尝试在 module-info 中创建条目时,它没有让我选择在 src/test/java 中添加包,它允许我选择的唯一包是在 src/main/java 中。
我不太了解模块。如何让程序让我从 src/test/java 运行测试?
【问题讨论】:
标签: java eclipse maven testng java-module