【发布时间】:2010-06-05 17:15:03
【问题描述】:
我有一个 Maven 维护的项目,其中包含一些模块。一个模块包含一个 XML 文件和一个解析类。
第二个模块依赖于第一个模块。第一个模块中有一个类调用了解析类,但是Maven无法测试第二个模块中的类。测试报告:
java.lang.NullPointerException
at java.util.Properties.loadFromXML(Properties.java:851)
at foo.firstModule.Parser.<init>(Parser.java:92)
at foo.secondModule.Program.<init>(Program.java:84)
在Parser.java(第一个模块)中,它使用Properties 和InputStream 来读取/解析XML 文件:
InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
Properties properties = new Properties();
properties.loadFromXML(xmlStream);
Data.xml 位于第一个模块的 resources/foo/firstModule 目录中,并在第一个模块中测试 OK。
似乎在测试第二个模块时,Maven 无法从第一个模块正确加载 Data.xml。
我认为我可以通过使用 maven-dependency-plugin:unpack 来解决问题。所以我将这些 sn-ps 添加到第二个模块的 POM 文件中:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.1</version>
<executions>
<execution>
<id>data-copying</id>
<phase>test-compile</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>foo</groupId>
<artifactId>firstModule</artifactId>
<type>jar</type>
<includes>foo/firstModule/Data.xml</includes>
<outputDirectory>${project.build.directory}/classes</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
在这个 POM 文件中,我解压第一个模块并将 Data.xml 复制到 classes/foo/firstModule/ 目录中,然后运行测试。
可以看到确实是复制到了正确的目录下,但是Maven测试还是读不出来(Properties.loadFromXML() throws NPE)。
我还尝试了不同的输出目录,例如 ${project.build.directory}/resources 和 ${project.build.directory}/test-classes ,但一切都是徒劳的。
环境:Maven 2.2.1、eclipse、m2eclipse
----更新----
我忘了提到,第二个模块中的程序,扩展第一个模块中的解析器和解析器的构造函数属性被加载和解析。事实上,Program 是另一个具有更多功能的 Parser。
我认为 Program extends Parser 可能会导致问题(即ClassLoader 问题)。
如果我断开“继承”并在程序中初始化一个新的解析器,它可以正常工作并且测试通过了! 但是,由于它的设计方式,我无法更改继承。
---- 更新完整代码----
这是第一个模块中的Parser:
package foo.firstModule;
import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
public class Parser
{
private Properties properties;
public Parser()
{
InputStream xmlStream = getClass().getResourceAsStream("Data.xml");
properties = new Properties();
try
{
properties.loadFromXML(xmlStream);
}
catch (InvalidPropertiesFormatException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public Properties getProperties()
{
return properties;
}
}
这是Parser's 测试用例,通过了。
package foo.firstModule;
import junit.framework.TestCase;
public class ParserTest extends TestCase
{
public void testParser()
{
Parser p = new Parser();
assertEquals(64 , p.getProperties().size());
}
}
这是 secondModule 中的 ParserExtend,它扩展了 firstModule 中的 Parser:
package foo.secondModule;
import java.util.Properties;
import foo.firstModule.Parser;
public class ParserExtend extends Parser
{
private Properties properties;
public ParserExtend()
{
this.properties = getProperties();
}
public int getSize()
{
return properties.size();
}
}
这是ParserExtend's 测试用例:
package foo.secondModule;
import junit.framework.TestCase;
public class ParserExtendTest extends TestCase
{
public void testParserExtend()
{
ParserExtend pe = new ParserExtend();
assertEquals(64 , pe.getSize());
}
}
上述测试用例失败,因为 Properties.loadFromXML(Properties.java:851) 抛出 NPE。
但是,如果我不扩展 Parser 而只是初始化一个新的 Parser:
package foo.secondModule;
import java.util.Properties;
import foo.firstModule.Parser;
public class ParserInit
{
private Properties properties;
public ParserInit()
{
Parser p = new Parser();
this.properties = p.getProperties();
}
public int getSize()
{
return properties.size();
}
}
并使用以下方法对其进行测试:
package foo.secondModule;
import junit.framework.TestCase;
public class ParserInitTest extends TestCase
{
public void testParserInit()
{
ParserInit pi = new ParserInit();
assertEquals(64 , pi.getSize());
}
}
测试用例通过!
这是我的整个测试场景。
如何通过ParserExtend's 测试用例?
【问题讨论】:
标签: unit-testing testing maven-2 maven