junit中如何输出多个字符串
我有一个消息列表,我将其与给定的计数进行比较。当计数失败时,我想输出找到的所有消息,以便我知道哪条消息丢失或多余。目前我使用:
import scala.collection.JavaConversions._
def Assert_Messages (
expected : Int,
actual : java.util.List [String])
{
if (expected != 0 && actual.size == 0)
{
junit.framework.Assert.fail ("An expected error message was not reported.")
}
else if (expected != actual.size)
{
actual foreach (junit.framework.Assert.fail (_))
} // if
} // Assert_Messages
但这只会输出第一条消息,因为 junit.framework.Assert.fail 不会返回。有人给我一个不涉及丑陋的 StringBuffer 的想法吗?
JUnit 设置为测试必须在 Android 上运行。
感谢您的任何帮助。我期待着学习一些新的、有趣的东西。
I have a list of messages which I compare with a given count. When the count fails then I want to output all messages found so I know which message is missing or superfluous. Currently I use:
import scala.collection.JavaConversions._
def Assert_Messages (
expected : Int,
actual : java.util.List [String])
{
if (expected != 0 && actual.size == 0)
{
junit.framework.Assert.fail ("An expected error message was not reported.")
}
else if (expected != actual.size)
{
actual foreach (junit.framework.Assert.fail (_))
} // if
} // Assert_Messages
But this will only output the first message as junit.framework.Assert.fail
does not return. Has anybody got an idea for me which does not involve an ugly StringBuffer?
JUnit is set as the test must run on Android.
Thanks for any help. I look forward to learning something new and nifty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试
junit.framework.Assert.fail(actual.mkString("; "))
。将"; "
替换为您想要用作分隔符的任何内容;如果您希望能够重建原始字符串序列,请务必转义与分隔字符串冲突的字符,例如使用反斜杠或其他字符。一些旁注:
someCollection.size == 0
,而是测试someCollection.isEmpty
。在 ScalaList
上,如果 n 是列表的大小,则size
是 O(n) 运算; isEmpty 的复杂度为 O(1)。scala
包中导入内容时,不需要显式编写scala.
前缀(除非存在歧义),因此import collection.JavaConversions ._
会做得很好。You can try
junit.framework.Assert.fail(actual.mkString("; "))
. Replace"; "
with whatever you want to use as separator; if you want to be able to reconstruct the original sequence of strings, be sure to escape characters that collide with your separating string, e.g. with a backslash or something.Some side remarks:
someCollection.size == 0
, it is good practice to test forsomeCollection.isEmpty
instead. On a ScalaList
,size
is an O(n) operation if n is the size of your list;isEmpty
is O(1).scala.
prefix when you import stuff from within thescala
package (unless there are ambiguities), soimport collection.JavaConversions._
will do nicely.