我有一个项目,在使用 Java 17 和 Maven 放置了一段时间后,我现在正在开发它。在编译项目期间,我收到以下错误消息:
[INFO] --- surefire:3.5.0:test (default-test) @ energy-meters ---
[INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[ERROR] Error occurred during initialization of boot layer
[ERROR] java.lang.module.FindException: Error reading module: C:\MyProject\target\test-classes
[INFO] Results:
[INFO] Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
[WARNING] Corrupted channel by directly writing to native stream in forked JVM 1. See FAQ web page and the dump file C:\MyProject\target\surefire-reports\2024-09-27T18-37-41_721-jvmRun1.dumpstream
在上述文件中包含以下消息:
Corrupted channel by directly writing to native stream in forked JVM 1. Stream 'Error occurred during initialization of boot layer'.
Corrupted channel by directly writing to native stream in forked JVM 1. Stream 'java.lang.module.FindException: Error reading module: C:\Users\PowerStat\Documents\EnergyMeters\target\test-classes'.
Corrupted channel by directly writing to native stream in forked JVM 1. Stream 'Caused by: java.lang.module.InvalidModuleDescriptorException: Package de.powerstat.energymeters.entities not found in module'.
我的项目结构如下:
src/main/java/
de/powerstat/energymeters/
entities/
MeterReader.java
values/
*.java
module-info.java
src/test/java/
de/powerstat/energymeters/
entities/test/
MeterReaderTests.java
values/test/
*.java
module-info.java
我的测试
module-info.java
文件如下所示:
open module de.powerstat.energymeters
{
exports de.powerstat.energymeters.entities;
exports de.powerstat.energymeters.values;
requires transitive de.powerstat.validation;
requires org.apache.logging.log4j;
requires com.github.spotbugs.annotations;
requires org.junit.jupiter.api;
requires org.junit.platform.launcher;
requires org.junit.platform.suite.api;
requires org.junit.jupiter.params;
}
过了一段时间,我发现问题似乎不在surefire插件的上下文中,而是在编译器插件中,因为在使用
-X
时我有以下输出:
[INFO] New dependency detected: C:\Users\PowerStat\Documents\EnergyMeters\target\classes\de\powerstat\energymeters\entities\MeterReader.class
并且在
C:\MyProject\target\test-classes\de\powerstat\energymeters\entities\
目录中缺少这个包/类 - 只有测试类可以在 test/
子目录中找到。
所以我的问题是:这个错误消息的原因是什么?
经过一番挖掘和思考,我发现了问题:
在我让我的项目投入使用之前,我似乎还没有完成为 MeterReader 类编写测试。所以 JUnit 测试只是一个骨架:
package de.powerstat.energymeters.entities.test;
final class MeterReaderTests
{
MeterReaderTests()
{
super();
}
}
没有 MeterReader 类作为其中的依赖项。
添加依赖项和基本测试后,编译再次正常工作:
package de.powerstat.energymeters.entities.test;
import static org.junit.Assert.assertNotNull;
import org.junit.jupiter.api.Test;
import de.powerstat.energymeters.entities.MeterReader;
final class MeterReaderTests
{
MeterReaderTests()
{
super();
}
@Test
void testMeterReaderOk()
{
final MeterReader cleanMeterReader = MeterReader.of();
assertNotNull(cleanMeterReader, "MeterReader not as expected");
}
}