Override a method in a mock object

I need to write a unit test in which one of the dependencies of the tested object will be mocked. In that mocked object I would like to override one function (I do not care about the other functions at all).

What I need to do, is to provide a completely new implementation of that function. I am not particularly happy with providing mockito-style when / thenResult pairs for each potential input, i.e. I do not want to write:


I would rather like to write something like:

mock.foo = (a: Int) => if (a == 5) 3 else ...

Which mocking framework should I choose to substitute the whole implementation of a function in an easy way?


You don't need a mocking framework at all if you're only overriding one method and want to do it by actually defining the method.

If it's a class, just extend your class, override the method and inject your extension.

If it's an object or a final class of course you can't extend, but then you wouldn't be able to mock with many frameworks either, or inject an alternate easily.


See "onCall" method

(fooMock.increment _) expects(*) onCall { arg: Int => arg + 1}

Need Your Help

Dynamically generate proc files?

linux-kernel kernel-module

I'm making a linux kernel module and I was wondering if there was a way to dynamically generate proc files and use them in a sensible way.

HTML div display on top of Flash Object

html css flash

I have a flash slideshow with 2 divs overlaying it, one at the top and one at the bottom.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.