Warm tip: This article is reproduced from serverfault.com, please click

java-Junit测试访问CP资源在gitlab上失败,但可在本地工作

(java - Junit test accessing CP resource fails on gitlab but works locally)

发布于 2020-11-30 15:52:02

我正在设置gitlab管道,但是构建失败。很奇怪,我看不到麻烦。JUnit 4.12,gradle 4.8.1。

测试

public class ClientTest {
@Test
public void test() throws JAXBException, SAXException {
    String xml = ClientTest.class.getClassLoader().getResource("xml/Client.xml").getFile();
    ClientEventReq req = (ClientEventReq) unmarshaller.unmarshal(new File(xml));

我将build目录另存为工件,它看起来正确并等于本地计算机:

module/build/resources/test/xml/Client.xml

但是它在gitlab上失败了:

java.nio.file.NoSuchFileException: builds/product/module/build/resources/test/xml/Client.xml
at sun.nio.fs.UnixException.translateToIOException(UnixException.java:86)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:102)
at sun.nio.fs.UnixException.rethrowAsIOException(UnixException.java:107)
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(UnixFileSystemProvider.java:214)
at java.nio.file.Files.newByteChannel(Files.java:361)
at java.nio.file.Files.newByteChannel(Files.java:407)
at java.nio.file.Files.readAllBytes(Files.java:3152)
at ClientTest.test(ClientModificationEventReqTest.java:41)

我添加了一些其他命令来确定文件的位置

$ pwd
/builds/product
$ ls
module
gradle
gradlew
gradlew.bat
settings.gradle
$ ls builds/product/module
ls: cannot access 'builds/product/module': No such file or directory

因此,似乎当前目录是,/builds/product但Java需要该文件 builds/product/module/build/resources/test/xml/Client.xml

Questioner
Leos Literak
Viewed
0
Leos Literak 2020-12-01 16:08:28

我已经重写了代码,URL而不是使用File

URL url = ClientModificationEventReqTest.class.getClassLoader().getResource(path);
ClientEventReq req = (ClientEventReq) unmarshaller.unmarshal(url);