我目前正在使用Ant从JUnit测试套件生成html报告,但是我遇到了一些非常奇怪的问题。当我在Eclipse中运行我的测试套件时,我得到了30个成功、0个错误和0个失败。当我将测试套件作为Ant目标运行时(同样是在Eclipse中),我得到了7个成功、10个错误和13个失败,以及不同的输出。为了解决这个问题,我将只关注一个测试用例。
Ant目标: <target name="run-unit-test" depends="build-unit-test" description="Run all unit tests.">
<delete failonerror="false" dir="${unittest.dir}"/>
<mkdir dir="${unittest.dir}"/>
<junit printsummary="true" haltonfailure="false" fork="true" forkmode="once" showoutput="true">
<env key="BATON_HOME" value="/home/natshiel/git"/>
<classpath>
<path refid="libs"/>
<path refid="junit-libs"/>
<pathelement location="${sep.classes.dir}"/>
<pathelement location="${classes.dir}"/>
</classpath>
<formatter type="xml" usefile="true"/>
<batchtest fork="yes" todir="${unittest.dir}">
<fileset dir="${basedir}/build/classes">
<!--include name="**/*Test.class"/> -->
<include name="**/KeywordToolsTest.class"/>
<!-- Exclude non-junit tests -->
<exclude name="**/nbi/test/*"/>
<exclude name="**/configmgmt/test/*"/>
<exclude name="**/valuecollection/**"/>
</fileset>
</batchtest>
</junit>
</target>
JUnit Test in Question:
@Test
public void testExtractKeywordsInclInvalid() {
String fullString = "${CP_IM} ${b} ${1} ${} ${LAST-NAM[E]} ${IP}i ${12} ${34}${56} ${[BRACKETS]}";
fullString += " hello test b ${hg ${$} ${hello word} ${12-34_ab%}";
Set<String> validExpected = new HashSet<String>();
Set<String> invalidExpected = new HashSet<String>();
validExpected.addAll(Arrays.asList("", "1", "b", "IP", "34", "56", "LAST-NAM[E]", "12", "CP_IM", "[BRACKETS]"));
invalidExpected.addAll(Arrays.asList("hg ${$", "hello word", "12-34_ab%"));
Map<String,Set<String>> keywordMap = KeywordTools.extractKeywordsInclInvalid(fullString);
Set<String> validReturned = keywordMap.get(KeywordTools.VALID);
Set<String> invalidReturned = keywordMap.get(KeywordTools.INVALID);
System.out.println("String: " + fullString);
System.out.println("InclInvalid expected: " + validExpected.toString());
System.out.println("InclInvalid returned: " + validReturned.toString());
assertTrue(validExpected.equals(validReturned));
assertTrue(invalidExpected.equals(invalidReturned));
}
被测试的类方法:
public static Map<String,Set<String>> extractKeywordsInclInvalid(String input) {
Set<String> validKW = new HashSet<String>();
Set<String> invalidKW = new HashSet<String>();
Map<String,Set<String>> results = new HashMap<String,Set<String>>();
results.put(VALID, validKW);
results.put(INVALID, invalidKW);
Pattern kwAllPattern = Pattern.compile("\$\{(.*?)\}");
Matcher matcher = kwAllPattern.matcher(input);
while (matcher.find()) {
String kw = matcher.group(1);
if (Pattern.matches("[\w-_\[\]]*", kw) {
validKW.add(kw);
} else {
invalidKW.add(kw);
}
}
return results;
}
在Eclipse中运行Test Suite时的输出:
String: ${CP_IM} ${b} ${1} ${} ${LAST-NAM[E]} ${IP}i ${12} ${34}${56}${[BRACKETS]} hello test b ${hg ${$} ${hello word} ${12-34_ab%}
InclInvalid expected: [, 1, b, 56, [BRACKETS], IP, LAST-NAM[E], 34, CP_IM, 12]
InclInvalid returned: [, 1, b, 56, [BRACKETS], IP, LAST-NAM[E], 34, 12, CP_IM]
从build.xml运行Test Suite时的输出
[junit] String: ${CP_IM} ${b} ${1} ${} ${LAST-NAM[E]} ${IP}i ${12} ${34}${56} ${[BRACKETS]} hello test b ${hg ${$} ${hello word} ${12-34_ab%}
[junit] InclInvalid expected: [, 1, b, 56, [BRACKETS], IP, LAST-NAM[E], 34, CP_IM, 12]
[junit] InclInvalid returned: [, 1, b, 56, IP, 34, 12, CP_IM]
可以看到,当使用build.xml运行测试套件时,包含方括号的字符串处理不正确。我怎么也弄不明白为什么会发生这种事。
在使用Eclipse的调试器检查代码时,看起来Regex在由Ant运行时以不同的方式工作。我不知道该怎么解决这个问题。
系统信息:
CentOS 6.6
Eclipse 3.6.1JRE 1.6.0-openjdk.x86_64
Apache Ant 1.9.6
你知道这里可能发生了什么吗?提前感谢!
原来在我的项目中隐藏了一个jar,其中包含旧版本的类文件。为了找到它,我在测试类中添加了以下几行:
Class cls = Class.forName("com.package.KeywordTools");
System.out.println("Location: " + cls.getResource("KeywordTools.class"));
我从jar中删除了类文件,问题解决了。