Skip to content

Junit test accessing CP resource fails on gitlab but works locally

I am setting up a gitlab pipeline but the build fails. It is weird, I do not see a trouble. JUnit 4.12, gradle 4.8.1.


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

I have saved the build directory as an artifact and it looks correct and equal to local machine:


But it fails on gitlab with:

java.nio.file.NoSuchFileException: builds/product/module/build/resources/test/xml/Client.xml
at sun.nio.fs.UnixException.translateToIOException(
at sun.nio.fs.UnixException.rethrowAsIOException(
at sun.nio.fs.UnixException.rethrowAsIOException(
at sun.nio.fs.UnixFileSystemProvider.newByteChannel(
at java.nio.file.Files.newByteChannel(
at java.nio.file.Files.newByteChannel(
at java.nio.file.Files.readAllBytes(
at ClientTest.test(

I have added few more commands to identify where the files are located

$ pwd
$ ls
$ ls builds/product/module
ls: cannot access 'builds/product/module': No such file or directory

So it seems that the current dir is /builds/product but the java wants the file builds/product/module/build/resources/test/xml/Client.xml.


I have rewritten the code to use URL instead of File:

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