如何在测试期间使用来自test/resources文件夹的文件进行验证?

12 浏览
0 Comments

如何在测试期间使用来自test/resources文件夹的文件进行验证?

背景:我想要自动验证XML文件是否符合模式。为此,我创建了一个Maven项目和一个XMLValidator类在src/main/java中。

为了运行测试,我在src/test/java中创建了一个XMLValidatorTest。我的想法是将我想要验证的XML和XSD文件放入src/test/resources文件夹中,并通过文件名来"抓取"它们。但是当我运行mvn test时,找不到这些文件。

我的测试类:

public class XMLValidatorTest {
  XMLValidator xmlvalidator;
  // XSD is in src\test\resources\XSD\myXSD.xsd
  String xsdFileNameWithPath = "XSD\\myXSD.xsd";
  @Before
  public void setup() {
    xmlvalidator = new XMLValidator();
  }
  @Test
  public void testValidate() {
    // XML is in src\test\resources\XML\test001.xml
    String xmlFileNameWithPath = "XML\\test001.xml";
    assertTrue(xmlvalidator.validate(xmlFileNameWithPath, xsdFileNameWithPath));
  } 
}

当我运行mvn test时,我得到一个文件找不到的异常,比如java.io.FileNotFoundException: D:\workspace_eclipse\XSDTester\XSD\myXSD.xsd - 这是正确的,因为它在资源文件夹中。

我尝试了从Maven(surefire):从src/test/java复制测试资源中获取的解决方案,通过-标签复制文件,但没有效果。


  
    
      ${project.basedir}/src/test/resources
    
  

有什么建议吗?

附加信息:在我的XMLValidator中,我尝试使用给定的文件:

Source xmlFile = new StreamSource(new File(xmlFileNameWithPath));
Source xsdFile = new StreamSource(new File(xsdFileNameWithPath));

编辑

我再次尝试实施Michele Sacchetti的建议,使用this.getClass().getResourceAsStream()

因此,为了检索XML文件,我尝试这样做:

validate(String xmlFileNameWithPath, String xsdFileNameWithPath) {
  Source xmlFile;
  try {
    // 根据https://stackoverflow.com/questions/10997453/java-xml-validation-does-not-work-when-schema-comes-from-classpath给出systemid
    xmlFile = new StreamSource(getClass().getResourceAsStream(xmlFileNameWithPath), getClass().getResource(xsdFileNameWithPath).toString());
  } catch (Exception e) {
    System.out.println("XML为空");
    return false;   
  } 
  // ...  进一步的步骤,尝试获取XSD和验证它们
}

并通过以下方式调用函数(如Michele Sacchetti所说,省略src\test\resources):

// XSD is in src\test\resources\XSD\myXSD.xsd
String xsdFileNameWithPath = "XSD\\myXSD.xsd";
// XML is in src\test\resources\XML\test001.xml
String xmlFileNameWithPath = "XML\\test001.xml";
validate(xmlFileNameWithPath, xsdFileNameWithPath);

但是当我尝试获取文件的StreamSource时,我总是得到一个NPE的异常。

0
0 Comments

如何在测试期间使用来自test/resources文件夹的文件进行验证?

问题的出现原因是:

在测试期间,尝试通过文件名来访问文件是行不通的。需要使用this.getClass().getResourceAsStream()方法通过CLASSPATH来访问文件(必须省略以/src/test/resources开头的部分)。

解决方法是:

将文件放在src/test/resources文件夹下,并使用this.getClass().getResourceAsStream("test.properties")来访问文件。

示例代码如下:

package test;
import java.io.IOException;
import java.net.URISyntaxException;
import org.junit.Test;
public class ReadFileTest {
    public void readTest() throws IOException, URISyntaxException{
        System.out.println(ReadFileTest.class.getResource("test.properties").toURI());
    }
}

运行mvn clean test命令后,输出的结果如下:

[INFO] Scanning for projects...

[INFO]

[INFO] ------------------------------------------------------------------------

[INFO] Building test 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[INFO]

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ test ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Copying 0 resource

[INFO]

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ test ---

[INFO] Nothing to compile - all classes are up to date

[INFO]

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ test ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Copying 1 resource

[INFO]

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ test ---

[INFO] Nothing to compile - all classes are up to date

[INFO]

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ test ---

[INFO] Surefire report directory: /Users/msacchetti/fworks/oss_projects/test/target/surefire-reports

-------------------------------------------------------

T E S T S

-------------------------------------------------------

Running test.ReadFileTest

file:/Users/msacchetti/fworks/oss_projects/test/target/test-classes/test/test.properties

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.037 sec

Results :

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

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 1.318 s

[INFO] Finished at: 2016-04-26T21:36:54+02:00

[INFO] Final Memory: 10M/245M

[INFO] ------------------------------------------------------------------------

以上是问题的原因和解决方法,通过使用this.getClass().getResourceAsStream()方法来访问test/resources文件夹下的文件,可以确保文件能够正确地被找到。

0