我的 test 配置文件src/test/resources/ConfigTest.json
。我的maven测试辅助类(枚举)有以下两行:
ClassLoader c = this.getClass().getClassLoader();
File configFile = new File(c.getResource("ConfigTest.json").getFile());
当我从Eclipse或本地命令行运行mvn test
时,按预期工作。但是,当从Bitbucket管道运行时,它会抛出NullPointerException
以找不到该文件。我的bitbucket-pipelines.yml
文件:
image: maven:3.3.9
clone:
depth: full
pipelines:
default:
- step:
caches:
- maven
script:
- mvn -B verify
我也尝试过:
this.getClass().getResource("/ConfigTest.json")
ClassLoader c = Thread.currentThread().getContextClassLoader()
完整示例类:
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());
}
}
我想念什么?在资源加载方面,Bitbucket运行时与本地环境有何不同?
更新 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>
答案 0 :(得分:0)
神秘解决了。在我的Mac上.bash_profile
中,这句话就是这一行:
# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob;
它允许getResourceAsStream(name)
(以及任何其他文件操作)查找文件不区分大小写。我认为Mac上的Java总是区分大小写。
我的资源名称中存在一个大小写错误,因此管道正确地失败了。修正了错字,一切都很好。
:脸红: