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

时间:2016-04-25 10:55:06

标签: xml maven

背景:我想自动针对架构验证XML文件。为此,我在XMLValidator创建了一个maven项目和一个src/main/java - 类。

要运行测试,我在XMLValidatorTest中创建了src/test/java。我的想法是将我要验证的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 - 它在资源文件夹中是正确的。

我尝试通过<testresources> - 代码复制文件,从Maven (surefire): copy test resources from src/test/java开始尝试解决方案,但它没有效果。

<build>
  <testResources>
    <testResource>
      <directory>${project.basedir}/src/test/resources</directory>
    </testResource>
  </testResources>
</build>

有任何建议吗?

P.S。在我的XMLValidator中,我尝试通过以下方式使用给定的文件:

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

修改

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

因此,为了检索XML文件,我试试这个

validate (String xmlFileNameWithPath, String xsdFileNameWithPath) {

  Source xmlFile;
  try {
    // give systemid according to: https://stackoverflow.com/questions/10997453/java-xml-validation-does-not-work-when-schema-comes-from-classpath
    xmlFile = new StreamSource(getClass().getResourceAsStream(xmlFileNameWithPath), getClass().getResource(xsdFileNameWithPath).toString());
  } catch (Exception e) {
    System.out.println("XML is null");
    return false;   
  } 

  // ...  further steps, trying to get the XSD and validate them
}

并通过(正如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);

但我总是得到一个NPE,然后尝试获取该文件的StreamSource

1 个答案:

答案 0 :(得分:4)

不要尝试通过文件名访问文件,而是使用this.getClass().getResourceAsStream()方法通过CLASSPATH访问它们(您必须省略开始/src/test/resources

样本bleow文件中的

位于

src/test/resources/test/test.properties

的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>test</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>test</name>
  <dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <type>jar</type>
        <scope>test</scope>
    </dependency>
  </dependencies>
</project>

测试源

package test;

import java.io.IOException;
import java.net.URISyntaxException;
import org.junit.Test;

public class ReadFileTest {


    @Test
    public void readTest() throws IOException, URISyntaxException{
        System.out.println(ReadFileTest.class.getResource("test.properties").toURI());
    }

}

mvn clean test output

[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] ------------------------------------------------------------------------

请注意,返回路径正确地进入目标文件夹而不是src中