Mocking in Scala could use Typesafe's official help

1,240 views
Skip to first unread message

Jon Steelman

unread,
Mar 5, 2012, 3:30:50 PM3/5/12
to scala-user, martin odersky, Josh Suereth
Will Typesafe hopefully be supporting a Scala native mocking framework, adding one to the official stack? We have found the Java native ones inadequate. Full mocking support will help Scala's adoption in conventional Java shops. ScalaMock has a start but needs more attention than Paul Butcher can give it at the moment.

Thanks,
Jon

etorreborre

unread,
Mar 5, 2012, 5:34:26 PM3/5/12
to scala...@googlegroups.com, martin odersky, Josh Suereth
Hi Jon,

I'm currently working on a solution to help with mocking of byname arguments with Mockito. There might also be a way to deal with functions like that

What other kinds of issues do you currently have?

Thanks.

Eric.

Mirco Dotta

unread,
Mar 5, 2012, 5:44:30 PM3/5/12
to etorreborre, scala-user
I'm currently working on a solution to help with mocking of byname arguments with Mockito. 

Eric, that's awesome! Looking forward to it.

-- Mirco

Jon Steelman

unread,
Mar 5, 2012, 5:57:00 PM3/5/12
to etorreborre, scala...@googlegroups.com
Hi Eric,

We only dabbled in Mockito. 

We stumbled into this issue on ScalaMock, basically part of a more general problem that ScalaMock needs to mock the entire class hierarchy and Traits:
https://github.com/paulbutcher/ScalaMock/issues/3
TDD shops need richer support for mocking in Scala at least than ScalaMock has right now. 

How close do you feel Mockito is to offering complete mocking for Scala? What is Mockito for Scala missing? Is this the best documentation regarding? http://code.google.com/p/specs/wiki/UsingMockito

Thanks,
Jon
Message has been deleted

etorreborre

unread,
Mar 6, 2012, 1:59:16 AM3/6/12
to scala...@googlegroups.com, etorreborre
Hi Jon, Mirco,

You can have a look at the latest specs2 1.9-SNAPSHOT. 

I've added some support for:

 - byname parameters
 - functions/partial functions parameters verification (Jon, please note that the updated Mockito doc is on the specs2 project now)

Jon, I also tested the following with specs2 and it works fine for me:

import org.specs2.mutable.Specification
import org.specs2.mock.Mockito

class TestSpec extends Specification with Mockito {
  val m = mock[ToMock]

  "try that" >> {
    m.amount returns 3
    m.amount === 3
  }
}

trait ToMock {
  lazy val amount: Int = 0
  def fn(arg1: =>String): Any
}

I don't claim that specs2/Mockito is "offering complete mocking for Scala" but it's nice to see that it can be pushed that far. Please test the SNAPSHOT and report any issue (I wouldn't be surprised to see some corner cases coming up) or feature requests.

Thanks,

Eric.

Paul Butcher

unread,
Mar 6, 2012, 6:59:26 AM3/6/12
to Jon Steelman, scala-user, martin odersky, Josh Suereth

On Monday, 5 March 2012 at 20:30, Jon Steelman wrote:

ScalaMock has a start but needs more attention than Paul Butcher can give it at the moment.
FYI - I've started looking at whether I can make use of the upcoming Scala macros as an alternative to the compiler plugin for type safe mock support.

The compiler plugin certainly is a viable approach, but the need for a complex build process and the number of special cases it needs to handle have lead me to consider whether macros might be a better approach.

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher


Ryan Cino

unread,
Mar 6, 2012, 10:24:50 AM3/6/12
to scala-user
Hey Eric,

I have prototyped a few tests using Mockito and enjoy its ease-of-use
and functionality. However, like Paul's ScalaMock, is there any
ability, or planned implementation, for mocking Scala objects. It may
be my current naivety with the product, but it seems that the mocking
is limited to classes and traits.

Thanks,
Ryan

On Mar 6, 1:59 am, etorreborre <etorrebo...@gmail.com> wrote:
> Hi Jon, Mirco,
>
> You can have a look at the latest specs2 1.9-SNAPSHOT.
>
> I've added some support for:
>
>  - byname parameters
>  - functions/partial functions parameters verification<http://etorreborre.github.com/specs2/guide-SNAPSHOT/org.specs2.guide....> (Jon,
> please note that the updated Mockito doc is on the specs2 project now)
>
> Jon, I also tested the following with specs2 and it works fine for me:
>
> *import org.specs2.mutable.Specification*
> *import org.specs2.mock.Mockito*
>
> *class TestSpec extends Specification with Mockito {*
> *  val m = mock[ToMock]*
> *
> *
> *  "try that" >> {*
> *    m.amount returns 3*
> *    m.amount === 3*
> *  }*
> *}*
> *
> *
> *trait ToMock {*
> *  lazy val amount: Int = 0*
> *  def fn(arg1: =>String): Any*
> *}*
>
> I don't claim that specs2/Mockito is "offering complete mocking for Scala"
> but it's nice to see that it can be pushed that far. Please test the
> SNAPSHOT and report any issue (I wouldn't be surprised to see some corner
> cases coming up) or feature requests.
>
> Thanks,
>
> Eric.
>
>
>
>
>
>
>
> On Tuesday, March 6, 2012 9:57:00 AM UTC+11, Jon wrote:
>
> > Hi Eric,
>
> > We only dabbled in Mockito.
>
> > We stumbled into this issue on ScalaMock, basically part of a more general
> > problem that ScalaMock needs to mock the entire class hierarchy and Traits:
> >https://github.com/paulbutcher/ScalaMock/issues/3
> > TDD shops need richer support for mocking in Scala at least than ScalaMock
> > has right now.
>
> > How close do you feel Mockito is to offering complete mocking for Scala?
> > What is Mockito for Scala missing? Is this the best documentation
> > regarding?http://code.google.com/p/specs/wiki/UsingMockito
>
> > Thanks,
> > Jon
>
> > On Mon, Mar 5, 2012 at 5:34 PM, etorreborre <etorrebo...@gmail.com> wrote:
>
> >> Hi Jon,
>
> >> I'm currently working on a solution to help with mocking of byname
> >> arguments with Mockito. There might also be a way to deal with functions like
> >> that<https://groups.google.com/forum/#!msg/specs2-users/R3DS_ZPe29w/WvmkfJ...>
> >> .

etorreborre

unread,
Mar 6, 2012, 8:13:35 PM3/6/12
to scala...@googlegroups.com
There is no functionality in Mockito/java for mocking static methods by design (see the FAQ). Mockito leaves that to other tools if absolutely necessary. 

Similarly in Scala, I will follow the same path and advise to avoid having to mock objects in the first place. If you really have to, that's where ScalaMock would come in handy.

Or to say it differently:
  •  if you need to mock objects it might be worth changing your design to make it more flexible and expose the dependency you want to mock. Then you can use Mockito
  • if you can't do anything because it's third-party code, use ScalaMock

Eric.

etorreborre

unread,
Mar 7, 2012, 6:47:53 PM3/7/12
to scala...@googlegroups.com, etorreborre
I added a little bit more Scala-friendliness to the Mockito integration in specs2 thanks to this StackOverflow question:

 - implicitly converted parameters

Eric.

Paul Butcher

unread,
Jun 5, 2012, 5:31:55 AM6/5/12
to Jon Steelman, scala-user, martin odersky, Josh Suereth

On Tuesday, 6 March 2012 at 11:59, Paul Butcher wrote:

On Monday, 5 March 2012 at 20:30, Jon Steelman wrote:

ScalaMock has a start but needs more attention than Paul Butcher can give it at the moment.
FYI - I've started looking at whether I can make use of the upcoming Scala macros as an alternative to the compiler plugin for type safe mock support.

Jon,

You may already have seen the announcement I sent yesterday, but if not - I've just released a preview of ScalaMock 3, which is implemented with macros instead of a compiler plugin. It's still got a way to go, but it is functional enough to both prove that macros are a viable approach (more viable than the compiler plugin) and to be useful, I think.

Reply all
Reply to author
Forward
0 new messages