开发者

Scala problem with jMock expectations and returning a value from mock

开发者 https://www.devze.com 2023-01-06 14:31 出处:网络
Solved. IntelliJ didn\'t highlight the fact that my imports were incomplete. Hi, I have a simple Scala program tha开发者_JAVA百科t I\'m trying to develop using jMock. Setting basic expectations work

Solved. IntelliJ didn't highlight the fact that my imports were incomplete.

Hi,

I have a simple Scala program tha开发者_JAVA百科t I'm trying to develop using jMock. Setting basic expectations works nicely but for some reason Scala does not understand my attempt to return a value from a mock object. My maven build spews out the following error

TestLocalCollector.scala:45: error: not found: value returnValue
one (nodeCtx).getParameter("FilenameRegex"); will( returnValue(regex))
                                                   ^

And the respective code snippets are

@Before def setUp() : Unit = { nodeCtx = context.mock(classOf[NodeContext]) }
...
// the value to be returned
val regex = ".*\\.data"
...
// setting the expectations
one (nodeCtx).getParameter("FilenameRegex"); will( returnValue(regex))

To me it sounds that Scala is expecting that the static jMock method returnValue would be a val? What am I missing here?


Are you sure about the ';'?

one (nodeCtx).getParameter("FilenameRegex") will( returnValue(regex))

might work better.

In this example you see a line like:

  expect {
    one(blogger).todayPosts will returnValue(List(Post("...")))
  }

with the following comment:

Specify what the return value should be in the same expression by defining "will" as Scala infix operator.
In the Java equivalent we would have to make a separate method call (which our favorite IDE may insist on putting on the next line!)

  one(blogger).todayPosts; will(returnValue(List(Post("..."))))
                         ^
                         |
                         -- semicolon only in the *Java* version

The OP explains it himself:

the returnValue static method was not visible, thus the errors.
And the will method just records an action on the latest mock operation, that's why it can be on the next line or after the semicolon :)

import org.jmock.Expectations 
import org.jmock.Expectations._ 
... 
context.checking( 
  new Expectations {
    { oneOf (nodeCtx).getParameter("FilenameRegex") will( returnValue(".*\\.data") ) }
  }
) 
0

精彩评论

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