简体   繁体   English

如何在junit中输出多个字符串

[英]How to output more than one string in junit

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. 但这只会输出第一条消息,因为junit.framework.Assert.fail不会返回。 Has anybody got an idea for me which does not involve an ugly StringBuffer? 有没有人知道我不涉及丑陋的StringBuffer?

JUnit is set as the test must run on Android. JUnit设置为必须在Android上运行测试。

Thanks for any help. 谢谢你的帮助。 I look forward to learning something new and nifty. 我期待着学到新的和漂亮的东西。

You can try junit.framework.Assert.fail(actual.mkString("; ")) . 你可以试试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, eg 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. 在这种特殊情况下可能并不重要,但不是检查someCollection.size == 0 ,而是测试someCollection.isEmpty而不是。 On a Scala List , size is an O( n ) operation if n is the size of your list; 在Scala List ,如果nList size ,则size是O( n )操作; isEmpty is O(1). isEmpty是O(1)。
  • You don't need to explicitly write the scala. 您不需要显式编写scala. prefix when you import stuff from within the scala package (unless there are ambiguities), so import collection.JavaConversions._ will do nicely. scala包中导入东西时的前缀(除非有歧义),因此import collection.JavaConversions._会做得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM