Reputation: 43
Example:
object Test {
def test = {
doTest
}
protected def doTest = {
// do something
}
}
class MockTest extends WordSpec with Mockito{
"" in {
val t = spy(Test)
// how do i stub out doTest?
}
}
I have a Test class with a protected doTest method. How do I stub out this protected method?
Upvotes: 4
Views: 2163
Reputation: 108169
I would advise to make doTest
package private, so that clients of your object won't be able to call it, but you'll be able to test it from within the same package.
package com.company.my.foo
object Test {
def test = {
doTest
}
private[foo] def doTest = {
// do something
}
}
and
package com.company.my.foo
class MockTest extends WordSpec with Mockito{
"" in {
val t = spy(Test)
when(t.doTest()).thenReturn("foo")
}
}
Upvotes: 1
Reputation: 3298
Test
is a singleton (all Scala objects are), you can subclass a class, but not an object. Hence protected
is a bit meaningless here - you're saying this method should only be accessible to subclasses, but it's not possible to have any (since Test
is an object).
You have a couple options depending on what is best suited to your needs. 1) you can make Test
a class and then extend it, or 2) change the access level of doTest
to public
[which is the default in Scala if you don't specify an access modifier]
Upvotes: 0