How to test an object's private methods in Scala

后端 未结 3 1845
醉话见心
醉话见心 2021-01-07 09:24

I have an example object:

object Foo {
  private def sayFoo = \"Foo!\"
}

And I want to test the private sayFoo method without the following

3条回答
  •  旧时难觅i
    2021-01-07 09:56

    It cannot be tested by any standard means.

    However, if you need to "cover" that section of code, then test the methods which use that private method in their implementation. It is indirect, granted, but it does effectively test the code.

    For example, in your class you could have another method:

    def dontSayBar = {
        sayFoo()
        println("Other processing...")
    }
    

    and this will "cover" the code in the private method if tested.

    P.S. A good rule of thumb is that if you see code that isn't showing up in a coverage run, then you probably don't actually need that code.

提交回复
热议问题