开发者

How to output more than one string in junit

开发者 https://www.devze.com 2023-02-27 08:09 出处:网络
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开发者_如何学JAVA or superfluous. Currentl

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开发者_如何学JAVA 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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消