I have a file src/test/resources/ConfigTest.json
with my test configuration. My maven test helper class (an enum) has the following 2 lines:
ClassLoader c = this.getClass().getClassLoader();
File configFile = new File(c.getResource("ConfigTest.json").getFile());
When I run mvn test
from Eclipse or local command line, works as expected. However when run from a Bitbucket pipeline it throws a NullPointerException
for not finding the file. My bitbucket-pipelines.yml
file:
image: maven:3.3.9
clone:
depth: full
pipelines:
default:
- step:
caches:
- maven
script:
- mvn -B verify
I also tried:
this.getClass().getResource("/ConfigTest.json")
ClassLoader c = Thread.currentThread().getContextClassLoader()
Full example class:
import java.io.File;
import org.junit.Assert;
import org.junit.Test;
public class ClassLoaderTest {
@Test
public final void test() {
ClassLoader c = this.getClass().getClassLoader();
File configFile = new File(c.getResource("ConfigTest.json").getFile());
Assert.assertTrue(configFile.exists());
}
}
What do I miss? How is the Bitbucket runtime different from my local environment when it comes to resource loading?
Update The POM.XML
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.projectcastle</groupId>
<artifactId>io.projectcastle.tjwt2sfoauth</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Authentication using custom JWT</name>
<description>Translates custom JWT into OAuth session</description>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Mystery solved. The culprint was this line in my .bash_profile
on my Mac:
# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob;
It allows the getResourceAsStream(name)
(and any other file operation) find files case insensitive. I presumed that Java on Mac would always be case sensitive.
There was one capitalisation error in my resource name, so the pipeline rightfully failed. Fixed the typo, all is good.
:blush:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.