Skip to content
Advertisement

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.

Test

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));

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

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

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(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)

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

$ 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

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.

Advertisement

Answer

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

URL url = ClientModificationEventReqTest.class.getClassLoader().getResource(path);
ClientEventReq req = (ClientEventReq) unmarshaller.unmarshal(url);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement