你正在使用 ClassLoader 的 getResources 方法时出现了什么错误?

huangapple go评论76阅读模式
英文:

What error am I making with the ClassLoader getResources?

问题

我已经编写了以下代码来测试我的代码中的一个方法。为了创建 s3Event,我有一个 JSON 文件,但是当我使用类加载器时,总是返回一个空异常。可能是我将绝对路径弄错了?我已经附加了一张图片,以显示文件路径布局,以防这是错误。

@Test
@DisplayName("Testing the handleRequest")
void testTheHandleRequest() throws URISyntaxException, IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(Objects.requireNonNull(classLoader.getResource("s3Event.json")).toURI());

    S3Event s3Event = new ObjectMapper().readValue(file, S3Event.class);
    Context context = null;
    LambdaHandler lambdaHandler = new LambdaHandler();

    assertEquals("Finished handleRequest()", lambdaHandler.handleRequest(s3Event, null));
}

你正在使用 ClassLoader 的 getResources 方法时出现了什么错误?

英文:

I have written the following code to test a method within my code. To create the s3Event, I have a json file but when I use the class loader I am always returned a null exception. It could be possible I have the absolute path incorrect? I have attached an image to show the file pathway layout in case this is the mistake.

@Test
@DisplayName("Testing the handleRequest")
void testTheHandleRequest() throws URISyntaxException, IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    File file = new File(Objects.requireNonNull(classLoader.getResource("s3Event.json")).toURI());

    S3Event s3Event = new ObjectMapper().readValue(file, S3Event.class);
    Context context = null;
    LambdaHandler lambdaHandler = new LambdaHandler();

    assertEquals ("Finished handleRequest()", lambdaHandler.handleRequest(s3Event, null));
}

你正在使用 ClassLoader 的 getResources 方法时出现了什么错误?

答案1

得分: 1

我唯一能找到的问题在于项目结构。Maven对于默认结构src/test/resources支持默认的ClassLoader资源行为,然而在你的情况下,文件夹名为src/test/java/testResources,这需要在POM或程序中进行额外的配置。

我的项目文件夹结构如下,

@Test
@DisplayName("Testing on the classloader")
public void testOnClassLoader() throws URISyntaxException {
    ClassLoader cl = getClass().getClassLoader();
        
    File file = new File(Objects.requireNonNull(cl.getResource("sample.json").toURI()));
        
    if(file != null) {
        System.out.println("File found " + file.getAbsolutePath());
        assertEquals(true, true);
    }        
}

输出:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running JsonLoaderTest
File found ~\resource-access\target\test-classes\sample.json
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.139 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.007 s
[INFO] Finished at: 2020-05-05T12:51:02+05:30
[INFO] ------------------------------------------------------------------------

希望对你有所帮助。

英文:

The only problem I could find is in the project structure. Maven supports the default ClassLoader resource behaviour for the default structure src/test/resources, whereas in your case the folder name is src/test/java/testResources, which requires additional configurations to be made either in POM or in program.

My project folder structure is given below,

你正在使用 ClassLoader 的 getResources 方法时出现了什么错误?

@Test
@DisplayName("Testing on the classloader")
public void testOnClassLoader() throws URISyntaxException {
	ClassLoader cl = getClass().getClassLoader();
		
	File file = new File(Objects.requireNonNull(cl.getResource("sample.json").toURI()));
		
	if(file != null) {
		System.out.println("File found "+ file.getAbsolutePath());
		assertEquals(true, true);
	}		
		
}

Output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running JsonLoaderTest
File found ~\resource-access\target\test-classes\sample.json
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.139 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.007 s
[INFO] Finished at: 2020-05-05T12:51:02+05:30
[INFO] ------------------------------------------------------------------------

Hope this helps.

huangapple
  • 本文由 发表于 2020年5月5日 14:44:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/61607238.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定