Telerik blogs

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 ( 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 Name="AfterTest">
    <JustMockStop />

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 ( 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>();

- “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!

About the Author

Hristo Kosev


Related Posts


Comments are disabled in preview mode.