junit测试在gitlab上访问cp资源失败,但在本地工作

kiayqfof  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(291)

我正在设置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

所以现在的dir是 /builds/product 但是java想要这个文件 builds/product/module/build/resources/test/xml/Client.xml .

eagi6jfj

eagi6jfj1#

我已经重写了要使用的代码 URL 而不是 File :

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

相关问题