简体   繁体   English

JUnit AssertionError:在Maven中运行时无法识别平台

[英]JUnit AssertionError: Platform not recognized when running in Maven

I'm working to transition our project's build structure from Ant to Maven (3.3.3), using Java 1.8, and ran into an issue that's stumping me. 我正在使用Java 1.8将项目的构建结构从Ant转换到Maven(3.3.3),并遇到了一个困扰我的问题。 All of our unit tests are working properly in Ant and Eclipse, but I've run into several that fail when executing in Maven. 我们所有的单元测试都可以在Ant和Eclipse中正常工作,但是在Maven中执行时,我遇到了一些失败的测试。 The failing tests (unfortunately, I can't post source due to corporate restrictions) all are attempting to read an image through the javax.imageio.ImageIO class, and all seem to fail with a NoClassDefFoundError, stating that they could not initialize java.nio.file.TempFileHelper. 测试失败(不幸的是,由于公司的限制,我无法发布源代码)都试图通过javax.imageio.ImageIO类读取图像,并且所有测试似乎都因NoClassDefFoundError而失败,表明它们无法初始化Java。 nio.file.TempFileHelper。 Now, I've seen this type of problem crop up when something is attempting to initialize the class, and fails (rather than not being able to find the class definition at all), but I looked into the source of the TempFileHelper class, and I can't seem to figure out what would have failed. 现在,当某些事情试图初始化类时,我已经看到了这种类型的问题,但是失败了(而不是根本无法找到类定义),但是我查看了TempFileHelper类的源代码,并且我似乎无法弄清楚什么会失败。

The stack trace (hand-typed, apologies for any typeos): 堆栈跟踪(手工键入,对任何类型的操作都表示歉意):

java.lang.NoClassDefFoundError: Could not initialize class java.nio.file.TempFileHelper
    at java.nio.file.Files.createTempFile(Files.java:897)
    at javax.imageio.stream.FileCacheImageInputStream.<init>(FileCacheImageInputStream.java:102)
    at com.sun.imageio.spi.InputStreamImageInputStreamSpi.createInputStreamInstance(InputStreamImageInputStreamSpi.java:69)
    at javax.imageio.ImageIO.createImageInputStream(ImageIo.java:357)
    at javax.imageio.ImageIO.read(ImageIO.java:1397)
    ... our code beyond here ...

The class making call to ImageIO.read is defined in a different maven module than the unit test (called core), and core has built successfully before this happens. 调用ImageIO.read的类是在不同于单元测试的maven模块中定义的(称为core),并且core在此之前已经成功构建。 The class making the call to ImageIO.read is supplying a relative path to a png file that is defined in core, and the image is stored in the resources folder of the core, under an "images" subfolder. 调用ImageIO.read的类提供了内核中定义的png文件的相对路径,并且图像存储在内核的resources文件夹中的“ images”子文件夹下。

example, using foo.png as the filename: 例如,使用foo.png作为文件名:

core/src/main/resources/images/foo.png

URL imageUrl = SomeClass.class.getResource("/images/foo.png");
ImageIO.read(imageUrl);

I've verified that foo.png is in the core.jar after core has built, and is in the images folder immediately off of the root of the jar, and that the core module is a valid dependency of the module being tested. 我已经验证了foo.png在core构建之后就位于core.jar中,并且位于jar根目录之后的images文件夹中,并且core模块是被测试模块的有效依赖项。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Update 1 更新1

While poking around through the TempFileHelper, I stumbled on some code that could fail, and brought it into my unit test to see if it continued to fail. 在遍历TempFileHelper时,我偶然发现了一些可能失败的代码,并将其带入我的单元测试中以查看它是否继续失败。 The failure now seems to indicate that the default file system is not known, by way of the following stack trace: 现在,该故障似乎通过以下堆栈跟踪表明默认文件系统未知:

java.lang.AssertionError: Platform not recognized
    at sun.nio.fs.DefaultSystemProvider.create(DefaultSystemProvider.java:68)
    ... our code truncated...

Update 2 更新2

Per Alexandre Cartapanis's request, here's the POM snippets. 根据Alexandre Cartapanis的要求,以下是POM片段。 The project is multi-module, and the parent pom is using plugin management to control versions. 该项目是多模块的,父pom使用插件管理来控制版本。

Parent POM snippet: 父POM代码段:

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

Child POM snippet: 子POM片段:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgs>
                    <arg>-XDignore.symbol.file</arg>
                    <compilerArg>-XDignore.symbol.file</compilerArg>
                </compilerArgs>
                <fork>true</fork>
            </configuration>
        </plugin>
    </plugins>
</build>

After much searching around, I have discovered the problem. 经过大量搜索后,我发现了问题所在。 It seems that one of our developers had written a test that was resetting the os.name (to "testOs") and os.version system properties, and never resetting them to their former values. 似乎我们的一位开发人员编写了一个测试,该测试将os.name(更改为“ testOs”)和os.version系统属性重置,并且从未将其重置为以前的值。 This caused all java.nio.Paths calls to fail. 这导致所有java.nio.Paths调用失败。

I discovered this after looking at the source of the sun.nio.fs.DefaultFileSystemProvider class, which immediately told me what it was attempting to look for, and the expected values. 我在查看了sun.nio.fs.DefaultFileSystemProvider类的源之后才发现了这一点,该类立即告诉我它正在尝试寻找什么以及期望值。 After discovering this, and a good bit more googling, I dropped in a System.getProperty("os.name") and printed it out in one of the failing tests, leading me to find "testOs". 发现这一点并进行了更多的搜索之后,我放入了System.getProperty(“ os.name”)并在其中一项失败的测试中将其打印出来,导致我找到了“ testOs”。 After discovering that, it was just a matter of determining that no, Maven nor JUnit are setting the os.name to that value, and therefore, it must be in our code. 发现这一点之后,只需确定Maven或JUnit是否将os.name设置为该值即可,因此,它必须在我们的代码中。

Thank you to everyone that attempted to help, your assistance is greatly appreciated. 感谢所有尝试提供帮助的人,非常感谢您的帮助。

Rob

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM