Mock. Just Mock. JustMock.

by Hristo Kosev | Comments 19

It's my pleasure to announce the newest member of the Telerik product family – JustMock . We’re about to release the first beta of this new product on the 12th of April and the first official version is expected to be part of the Q2 release. Except JustMock, there’re some other big announcements we’re going to make on the 12th of April so please, stay tuned. This is just announcement 1/3.

JustMock, as can be inferred from its name, is a mocking tool that will help you create better unit tests by making it easier for you to create mock objects (http://en.wikipedia.org/wiki/Mock_object) and set the respective expectations.

Unlike the available free mocking frameworks, JustMock allows you to mock, generally speaking... everything. While with the free tools you could mock interfaces and virtual and abstract methods and properties, with JustMock you can also mock sealed classes, non-virtual methods and properties, static classes, static methods and properties, etc., even those coming from the mscorlib assembly like DateTime, File, FileInfo and so on.

Due to the “dual” architecture of JustMock, mocking an interface or an abstract or virtual methods and properties is as easy as referencing a single assembly and using our API. When you want to use the advanced features like mocking final methods and properties or static classes and members, all you need is to make use the integration tools we’ve provided. When you want to run your unit tests using JustMock from Visual Studio you should use the “Enable” and “Disable” commands from within the JustMock menu. One of the greatest benefits of this “dual” mode is that you don’t have to make a compromise between performance and capabilities. If you need to mock say a virtual method, the tool will not use the Profiler API and will be lightning fast. It will use the Profiler API only in case you need the advanced capabilities of JustMock. In that case, you will have slower performance, but will get all of the features you need.

justmock mocking tool

When you want to run the tests as part of an MSBuild project you should just include the JustMock targets file and run these tasks before and after running the tests, e.g.

<Target Name="BeforeTest">
    <JustMockStart />
</Target>

<Target Name="AfterTest">
    <JustMockStop />
</Target>

Another thing you might find handy in JustMock is the API. We really tried to have a clean and easy to use API and we really like the end result. We’ve tried to facilitate as much as possible the AAA (http://c2.com/cgi/wiki?ArrangeActAssert) pattern of structuring your unit tests so creating, arranging and asserting against a mock is easy as:

var foo = Mock.Create<Foo>();
Mock.Arrange(() => foo.Echo(1)).Returns(10);
Assert.Equal(foo.Echo(1), 10);

The API that you’ll use will be similar no matter whether you’re mocking an interface, a sealed class or a static class for example and the good news is that everything is strongly typed. This means that you won’t need to use any “magical” strings or setting arranges for methods using incorrect number of parameters, thus you’ll be able to refactor the code under test without worrying that your tests will start failing after that.

We’ve also tried to cover various mocking scenarios like:

- “Loose” mocks, which means that you’re not required to set arranges for every mock object’s method you want to use and you can just do:

var foo = Mock.CreateInstance<IFoo>();
foo.Submit();

- “Partial” mocking, which you could use when you want to mock only a particular method and you want to use the original object and not a mock object, e.g.:

var foo = new Foo(); // Not going through Mock.CreateInstance<Foo>()
Mock.Arrange(()=> foo.Echo(Arg.Matches(x => x > 10)).Returns(10);

- We support scenarios like recursive / nested mocking (where you want to mock members  that are resulted  from calls starting from current mock), e.g.

Mock.Arrange(() => foo.Bar.Do("ping")).Returns("result");

and sequential mocking (where you want to return different values on different calls of same type), e.g.

var bar1 = Mock.Create<IBar>();
var bar2 = Mock.Create<IBar>();

var foo = Mock.Create<IFoo>();

Mock.Arrange(() => foo.GetBar()).Returns(bar1);
Mock.Arrange(() => foo.GetBar()).Returns(bar2);

Assert.Equal(foo.GetBar().GetHashCode(), bar1.GetHashCode());
Assert.Equal(foo.GetBar().GetHashCode(), bar2.GetHashCode());

That said, we hope you’ll enjoy the newest member of the Telerik product line and that it will help you create better unit tests in less time. We’ve planned some major improvements for the first official release like a good integration story between JustMock and its “bigger brother” JustCode and especially with the unit test runner we’ve planned for the Q2 release of JustCode but we’re still open to feedback from you and we’ll consider any suggestions for further improvements.

Have a great and productive day!

Hristo Kosev

19 Comments

Brian
Just Code, Just Mock. What's next? I see a trend developing.
Daryl
Hi, will this only be separate product or will it be part of the bundle too?

w
there are plenty of mocking tool already, why just mock?
Vassil Terziev
@w,

Have you used any of the tools available? If you have, you will know the shortcomings of most and would easily see how JustMock is different. We have never played the game of "me too" and JustMock, like any other Telerik product, brings unique features and advantages over the existing tools.

Here's just a few advantages:
1. It uses a hybrid approach and you don't have to use profiler all the time, only use when it is necessary. This allows you to distribute it as a single dll if you don't need to do mocking of static methods
2. Clean AAA syntax
3. Easy to use
4. Fast and lightweight
5. Strongly typed fluent interface
6. Unique features such as partial and sequential mocking (of static methods for example)
Mehfuz Hossain

@w
Our goal is to have a powerful yet easy to use mocking tool.
For the first time we have introduced a new concept that we call hybrid mode. JustMock don’t use profiler all the way . Generally you can use it as a single dll for most of your mocking cases (virtuals, interfaces ,etc) , which will never require you a profiler or anything installed. But as you turn on the Justmock addin or decide to mock some of your sealed classes or third party stuffs , extension methods, .NET framework libraries, JustMock will pick it automatically out of the box. Profiler is used but JustMock uses it when its necessary , thus giving you plenty of options how you use it in your projects (OS or Enterprise).

We want to have a mocking tool that is very simple to use, theoretically, it will be nice if there we dont have to mock a test class but it’s not the case. So, we try[will try more] our level best to make a cleaner AAA syntax possible.

There are loads more that we will roll out in our blogs, overtime.

Hope that Helps
M

Derick Bailey
so far, i fail to see any new value that this tool brings to the mocking space in .NET.

there are plenty of AAA syntax mocking frameworks with fluent API's, such as RhinoMocks, Moq and TypeMock. TypeMock allows you to use the profiler to replace static / sealed / etc, and has options for not using the profiler, as well.

what does JustMock bring to the table that is new or unique? the list that Vassil posted isn't new or unique. the three tools i've mentioned already encompass those items. you claim that you're not trying to do "me too" yet that's all i see so far.
Sean
TypeMock is the only one capable of doing this but it is pretty expensive. I welcome this competition. It can only make these tools better and cheaper.
Mark Foley
Sounds good.  I am yet to put significant time into setting up unit tests and mocks but can certainly see their benefit.  I'll be giving this a go as soon as my projects get a bit more complex and the beta matures!
Michael
The most important question to me: How much will it cost?
Hristo Kosev

@Derick

Thanks a lot for asking these questions. First let me throw some light upon the "profiler things" I mentioned above.
The free mocking frameworks like Rhino Mocks and Moq that you mentioned use the "DynamicProxy approach" and build the mock objects dynamically. I think both these frameworks use an OS project called Castle DynamicProxy. We decided to build our own dynamic proxy library as we wanted something much simpler to meet our mocking needs only.
The advantage of the proxy approach is that these frameworks are quite easy to use as you just need to reference a dll in your unit test project and then just start using the mocking API.
The disadvantage of this approach is that this way you can't mock static methods and classes, sealed classes. non virtual methods and properties and so on.

To mock these things JustMock uses the .NET Profiling API and behaves like a profiler but instead of profiling the code it changes it before the just-in-time compilation and injects code in the original code on-the-fly. You could find more information about this approach here:
http://msdn.microsoft.com/en-gb/magazine/cc188743.aspx
This approach is quite powerful but has its own disadvantages like the hard deployment. You need to have it integrated with your Visual Studio and also with your testing framework and also in some cases its performance is worse compared to how the dynamic proxy approach works.

In JustMock we've tried to take the best of both worlds and that's why we stated that we can bring something new to the table.

AFAIK TypeMock Isolator uses the .NET Profiling API all the time and doesn't have an option to use it or not to use it. Needless to say, I might be wrong and I'll be more than happy to update my blog post if this is the case.
Btw, TypeMock is a company we respect a lot and I don't want to say they've done something wrong. Just the opposite, I think they have a great product. I'm just saying that from our perspective we decided that the approach we've chosen has more advantages but of course we'd like to hear our customers' feedback first because our customers know best and they might prove us wrong. In any case, having more quality tools in the space ultimately benefits customers as there’s more innovation.

Milosz
Guys,

Good news, finally some competition for expensive TypeMock and similar (but

awkward syntax) MS Pex.

Regards,

Roy
It may be interesting if it offers the same features as Typemock, but at a more reasonable price.

Telerik, what do you expect this product will cost, or is this not known yet?

I expect that people will only start testing it en-masse when they know what it will cost -- why would I spend time writing unit tests with your mocking framework if I have no idea how much it will cost, knowing that too high a cost will mean that my company is not going to buy it?
Hristo Kosev
@Roy

This is not known yet as we want to get some feedback first and see what people really want and whether we'll be able to implement the requested features in a reasonable timeframe.

We've been thinking about something like 299$ for the developer license and 399$ for the developer license with subscription and priority support but this is yet to be decided.

Additionally JustMock will be available as part of the Ultimate Collection license (http://www.telerik.com/purchase.aspx).
MF
I help maintain a poorly-designed codebase and a large portion of it uses static methods extensively. I've also had an unattainable need to mock sealed classes from time to time.
The ability to mock both of these is awesome.
MF
I help maintain a poorly-designed codebase and a large portion of it uses static methods extensively. I've also had an unattainable need to mock sealed classes from time to time.
The ability to mock both of these is awesome.
MF
Odd question: Is there any limit to the number of classes/interfaces that can be mocked in a single program execution in .NET 3.5?
My team has had problems with RhinoMocks; we would frequently see AccessViolationExceptions occur when mocking a couple thousand interfaces. (It doesn't happen in .NET 4.0, but our project is stuck with 3.5 for a while.)
Jeff
I'm glad to see you've chosen lambdas for setting up the mock. Once you get used to the syntax, you can fly through writing unit tests.

Does it support extern calls? Specifically working with interop between C# and Win32.
Have you added any NAnt or MSBuild support out of the box for integrating the tool with automated build systems?

If it's anything like the other Telerik products we use I can't wait to see the direction the tool takes, should be exciting!        
Bob
All the code I have seen appears to be C#. Will we need to use that syntax for setting up the mocks?
Hristo Kosev

@Jeff
Mocking P/Invoke calls is not supported at the moment but we'll add support for it in some of the next JustMock builds so it's definitely on the list for the Q2 release.

In the current build we've included MSBuild targets file for easy integration with MSBuild based build systems and soon we'll add support for NAnt too.

@Bob

I guess you're asking about VB.NET samples?
With the installation we've included a VB.NET sample solution too, so you might want to have a look at it.

Comments

  1.    
      
      
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)
Read more articles by Hristo Kosev - or - read latest articles in Developer Tools