Java 从maven项目中的主包访问测试资源

Java 从maven项目中的主包访问测试资源,java,Java,作为练习的一部分,我正在开发代码,该代码将读取一个文件并检查它是否有浮动。我已经编写了代码,但我不确定是否正确访问了文件,或者是否有更好的方法 应用程序: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class Question1 { /* Wr

作为练习的一部分,我正在开发代码,该代码将读取一个文件并检查它是否有浮动。我已经编写了代码,但我不确定是否正确访问了文件,或者是否有更好的方法

应用程序:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Question1 {
    /* Write a method public ArrayList<Double> readValues(String filename) throws ... that reads a file
     * containing floating-point numbers. Throw appropriate exceptions if the file could not be opened
     * or if some of the inputs are not floating-point numbers.
     */

    private static final String RESOURCE_BASE_PATH = "src/test/resources/";

    //under src/main/ java : java_impatient.chapter4.Question1
    public static List<Double> readValues(String filename) throws IOException {
        ArrayList<Double> values = new ArrayList<>();
        try (BufferedReader fileReader = new BufferedReader(new FileReader(filename))) {
            //TODO : Complete this with TDD ?
        }
        return values;
    }

}
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.*;

import java.util.List;

public class Question1Test {
    //Some other test code here.

    //under src/test : testjava_impatient.chapter4.Question1Test
    @Test
    public void returnsEmptyListForEmptyFile() throws Exception {
        //Is this the best way to get the full path of a file?
        List<Double> values = Question1.readValues( getFilePath("src/test/resources/chapter4/question1_empty.txt") );
        Assert.assertTrue(values.isEmpty(), "List is not empty as expected!");
    }

    private static String getFilePath(String relativePath){
       File file = new File(RESOURCE_BASE_PATH + relativePath);
       String fullPath = file.getAbsolutePath();
       return fullPath;
    }

}
导入java.io.BufferedReader;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
公开课问题1{
/*写一个公共ArrayList readValues(字符串文件名)抛出的读取文件的方法
*包含浮点数。如果无法打开文件,则引发相应的异常
*或者,如果某些输入不是浮点数。
*/
私有静态最终字符串RESOURCE_BASE_PATH=“src/test/resources/”;
//在src/main/java下:java_uncerte.chapter4.Question1
公共静态列表readValues(字符串文件名)引发IOException{
ArrayList值=新的ArrayList();
try(BufferedReader fileReader=new BufferedReader(new fileReader(filename))){
//TODO:用TDD完成这个?
}
返回值;
}
}
测试:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Question1 {
    /* Write a method public ArrayList<Double> readValues(String filename) throws ... that reads a file
     * containing floating-point numbers. Throw appropriate exceptions if the file could not be opened
     * or if some of the inputs are not floating-point numbers.
     */

    private static final String RESOURCE_BASE_PATH = "src/test/resources/";

    //under src/main/ java : java_impatient.chapter4.Question1
    public static List<Double> readValues(String filename) throws IOException {
        ArrayList<Double> values = new ArrayList<>();
        try (BufferedReader fileReader = new BufferedReader(new FileReader(filename))) {
            //TODO : Complete this with TDD ?
        }
        return values;
    }

}
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.*;

import java.util.List;

public class Question1Test {
    //Some other test code here.

    //under src/test : testjava_impatient.chapter4.Question1Test
    @Test
    public void returnsEmptyListForEmptyFile() throws Exception {
        //Is this the best way to get the full path of a file?
        List<Double> values = Question1.readValues( getFilePath("src/test/resources/chapter4/question1_empty.txt") );
        Assert.assertTrue(values.isEmpty(), "List is not empty as expected!");
    }

    private static String getFilePath(String relativePath){
       File file = new File(RESOURCE_BASE_PATH + relativePath);
       String fullPath = file.getAbsolutePath();
       return fullPath;
    }

}
import org.testng.Assert;
导入org.testng.annotations.AfterClass;
导入org.testng.annotations.BeforeClass;
导入org.testng.annotations.Test;
导入java.io.*;
导入java.util.List;
公开课问题1测试{
//这里还有一些其他的测试代码。
//在src/test下:testjava_uncenter.chapter4.Question1Test
@试验
public void returnsEmptyListForEmptyFile()引发异常{
//这是获取文件完整路径的最佳方法吗?
列表值=Question1.readValues(getFilePath(“src/test/resources/chapter4/Question1_empty.txt”);
Assert.assertTrue(values.isEmpty(),“列表不是预期的空!”;
}
私有静态字符串getFilePath(字符串相对路径){
文件文件=新文件(资源\基础\路径+相对路径);
字符串fullPath=file.getAbsolutePath();
返回全路径;
}
}
Maven默认情况下,将“src/*/java”中的“.java”文件打包为“.class”,并将“src/*/resources”中的“*”文件打包为“.class”,您不会使用“src/*/*”,因为编译后它不存在

只需使用java和resources文件夹中的文件夹结构

另外,不要忘记,您想要访问的文件可能在运行的jar中,或者在ide生成的构建结构中

如果您没有更改资源的打包方式,要访问资源,您可以使用:

Class<T> {
    public InputStream getResourceAsStream(String name);
    public URL getResource(String name);
}

这个问题的标题有点误导,起初我认为您希望在运行主版本而不是测试版本时使用测试包中的某些内容。如果您只是想从IDE运行它,这一点都不重要,但是如果这是在另一个文件夹/机器上运行的.jar,测试文件夹将无法访问,因为主构建忽略了它。这里我们不是讨论包/模块,而是运行时组件,Question1Test.class可以访问“question1\u empty”,与question1.class相同,因为在测试构建中,它们都打包在一起。