Scala问题与jMock期望并从mock返回一个值

时间:2010-07-08 06:19:40

标签: scala jmock

解决。 IntelliJ没有强调我的导入不完整的事实。

您好,

我有一个简单的Scala程序,我正在尝试使用jMock开发。设置基本期望很有效,但由于某种原因,Scala不理解我尝试从模拟对象返回值。我的maven构建出现了以下错误

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

各个代码段

@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))

听起来我觉得Scala期望静态jMock方法returnValueval?我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

您确定';'吗?

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

可能会更好。

this example中,您会看到如下行:

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

带有以下评论:

  

通过将“will”定义为Scala中缀运算符,指定返回值在同一表达式中的含义   在Java等价物中,我们必须进行单独的方法调用(我们最喜欢的IDE可能会坚持使用下一行!)

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

OP自己解释说:

  

returnValue静态方法不可见,因此错误   并且will方法只记录对最新模拟操作的操作,这就是它可以在下一行或分号后的原因:)

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