tags:

views:

58

answers:

1

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

Hi,

I have a simple Scala program that 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?

+2  A: 

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") ) }
  }
) 
VonC
Hmm, the semicolon was a leftover, removed it, but now I get this `error: value will is not a member of java.lang.String`
puudeli
@puudeli: that is normal, `will` accepts a jMock action, like returnValue. Not a String.
VonC
@VonC the error pinpoints to the mock object: `one (nodeCtx).getParameter("FilenameRegex") will returnValue(regex)`the `getParameter` method returns a String, so the scala compiler attaches `will` to the ret val
puudeli