junit中如何输出多个字符串

发布于 2024-11-01 23:02:29 字数 722 浏览 0 评论 0原文

我有一个消息列表,我将其与给定的计数进行比较。当计数失败时,我想输出找到的所有消息,以便我知道哪条消息丢失或多余。目前我使用:

     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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡 2024-11-08 23:02:29

您可以尝试junit.framework.Assert.fail(actual.mkString("; "))。将 "; " 替换为您想要用作分隔符的任何内容;如果您希望能够重建原始字符串序列,请务必转义与分隔字符串冲突的字符,例如使用反斜杠或其他字符。

一些旁注:

  • 在这种特殊情况下可能并不重要,但最好不要检查 someCollection.size == 0,而是测试 someCollection.isEmpty 。在 Scala List 上,如果 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:

  • it may not matter much in this particular case, but instead of checking someCollection.size == 0, it is good practice to test for someCollection.isEmpty instead. On a Scala List, size is an O(n) operation if n is the size of your list; isEmpty is O(1).
  • You don't need to explicitly write the scala. prefix when you import stuff from within the scala package (unless there are ambiguities), so import collection.JavaConversions._ will do nicely.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文