Telerik’s JustMock Free Review
Telerik’s JustMock Free is a unit testing mocking framework that allows developers to create mock objects for interfaces. This is a common practice which writing unit tests. It has recently been released as a free offering. It competes will with other free mocking libraries (Moq, RhinoMock,etc) and others. However, it has some advantages:
1) Cleaner API
2) Easy to learn
JustMock has an easy to remember pattern to follow: AAA
- Arrange
- Setup your methods
- Act
- Perform the operation
- Assert
- Check your results
I have used Moq for several years and find it easy. It has a few foibles, but once they’re understood it’s easy to use. JustMock Free fixes a few those issues and would be an easier entry for developers new to mocking. My examples will compare Moq and JustMock in two terms functionality and performance.
Mock creation
JustMock
IDummy dummy = Mock.Create<IDummy>();
This is clear. The dummy is clear and easy to understand. It’s fungible as a true IDummy.
Moq
Moq<IDummy> dummy = new Moq<IDummy>();
To get the fungible IDummy you need to remember to call dummy.Object.
Method Setup (Arrange)
JustMock
Mock.Arrange(() => dummy.GetStrings(Any.IsAny<int>)).Returns(()= {return new List<string>();}).OccursOnce();
I do like the static Mock.Arrange call. However, you have to look 16 characters in to find which mock you’re working with. It’s nothing a little line formatting can’t fix.
Moq
dummy.Setup(x => x.GetStrings(It.IsAny<int>())).Returns(()= {return new List<string>();}).Verifiable();
Here, it’s immediately clear what mock object is being setup. Verifiable() like OccursOnce() allows me to verify the method was called.
Mock Usage (Act)
JustMock
DummyUser user = new DummyUser(dummy);
It like it! It’s just how I would use it in my production code.
Moq
DummyUser user = new DummyUser(dummy.Object);
This has always been annoying. I’m constantly forgetting to add the “.Object”. I’m happy to have VisualStudio correct me here. Also, this is just another mental stumbling block when teaching other developers how to use mocking.
Result (Assert)
JustMock and Moq
Assertion is really a function of what Unit Testing library you use. I use MSTest. They’re both the same.
var retval = user.GetDummies(); Assert.IsNotNull(retval); Assert.AreEqual(0,retval.Count);
Mock Method (Assert)
Most good mocking frameworks allow you to verify that a specific method has been called. JustMock uses the .Occurs pattern. In the example above we see that it should be called once and only once by using .OccursOnce(). Then the .Assert() method needs to be called on the mock.
mockDummy.Assert(x => x.GetString(Arg.IsAny<int>()));
Moq uses a .Verifiable pattern. This allows you to verify call count after the call.
mock.Verify(x => x.GetString(It.IsAny<int>()), Times.Once);
I favor the Moq pattern. It keeps the verification and call count together. Plus I can use the same mock in different test cases and test for different call counts. Using JustMock, I have to re-Arrange the mock.
Performance
Mocking frameworks need to be fast. I always run tests all the time. The faster the tests can run the better.
Lower numbers are better
Moq runs faster than JustMock by ~0.17 seconds. For 1000 tests, this adds 170 seconds which is 2.8 minutes. This isn’t too bad for a continuous integration server. However, it’s bad if you run the test on your machine all the time.
Final thoughts
JustMock has some very nice features.
- Mock creation.
- It’s more natural and will help developers think about the code and not the mocking framework
- Mock usage.
- The created mock can be used directly as the interface you care about. No more .Object from Moq to get the underlying object!
JustMock room for improvement
- There needs to be a way to re-set the .Occurs for method call count.
- I shouldn’t have to re-Assembe the method.
- Performance.
- For me, this is the biggest weakness. I run unit tests all the time on my local machine.