我一直在尝试使用MavenCli
,给它ByteArrayOutpurStream
-sstdout
和stderr
. 我有一些测试可以验证某些记录是否已写入这些流。只要我有一个测试,一切都OK。但是,如果我引入另一个测试,它会失败,因为没有任何东西写入标准输出。
这是一个片段(Scala):
class MyIT extends SpecWithJUnit {
sequential
trait Ctx extends Scope {
val outStream = new ByteArrayOutpurStream()
val errStream = new ByteArrayOutpurStream()
val pomDir = Files.createTempDirectory("test_pom_").toFile
val mavenCli = new MavenCli()
def haveWrote(str: String): Matcher[Int] = {
contain(str) ^^ { (_: Int) =>
new String(outStream.toByteArray, "UTF-8") aka "written records" }
}
def maven(pom: Elem, goals: String*): Int = {
val pomFile = new File(pomDir, "pom.xml")
XML.save(pomFile.getCanonicalPath, pom)
System.setProperty(
"maven.multiModuleProjectDirectory",
pomDir.getCanonicalPath)
mavenCli.doMain(
goals.toArray,
pomDir.getCanonicalPath,
new PrintStream(outStream),
new PrintStream(errStream))
}
}
"test 1" should {
"write something" in new Ctx {
val pomXml =
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>some-artifact-id</artifactId>
<version>1.0.0-SNAPSHOT</version>
<description>some meaningful description</description>
<name>some catchy name</name>
...
</project>
maven(pomXml, "clean" "validate") must haveWrote("something")
}
"write something else" in new Ctx {
val pomXml =
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>some-artifact-id</artifactId>
<version>1.0.0-SNAPSHOT</version>
<description>some meaningful description</description>
<name>some catchy name</name>
... (different than the "write something" test)
</project>
maven(pomXml, "clean" "validate") must haveWrote("something else")
}
}
}
如果我注释掉"write something"
测试,则"write something else"
通过。
如果我注释掉"write something else"
test (并取消注释"write something"
),则"write something"
通过。
如果两者都未注释并执行,则一个失败("write something else"
,因为它是顺序的)。
在maven执行后添加println
-s outStream
,原因似乎outStream
是空的......
我无法弄清楚为什么,或更重要的是,如何解决。
有任何想法吗?...
在此先感谢, 奥哈德