Tuesday, April 20, 2010

JustMock unofficial FAQs (update)

justmock-faq With all of the attention on the just announced TeamPulse and WebUI Test Studio QA Edition, the JustMock Beta is over due for more face blog time. Hopefully you managed to attend today’s JustMock introduction webinar (if you did, you are now a proud license holder) and start to learn more about how JustMock benefits your unit testing. But whether you attended that event or not, I figured I’d start to round-up and address some of the most burning questions about Telerik’s newest developer tool.

In the same vein as the Unofficial FAQs for the MVC Extensions, here is round-one for JustMock. If I missed a question you want answered, let me know in the comments.

JustMock Unofficial FAQs

Q: What is JustMock?
JustMock is a new Visual Studio productivity tool from Telerik that helps you create unit tests more quickly by automatically mocking, or “faking,” objects in your tests. Mocking helps you break dependencies in your unit tests so that you can focus on testing a single concern and provide more complete unit test coverage. You can read a complete intro to JustMock from the Product Team on the Telerik Blogs.

[NEW] Q: What is mocking and why do I need it?
Mocking (Wikipedia) is a concept in unit testing where real objects and substituted with fake objects that imitate the behavior of the real deal. Mocking is done so that a test can focus on the code being tested and not on the behavior or state of external dependencies. For example, if you have a data repository class that runs business logic and then saves information to a database, you want your unit test to focus on the business logic and not on the database. Mocking the “save” calls to your database ensures your tests run quickly and do not depend on the availability or state of your database.

When you’re ready to make sure the “save” calls are working, then you’re moving on to integration testing. Unit tests should not cross system boundaries, but integration tests are allowed to cross boundaries and make sure everything works together (your code, your database, your web services, IIS, etc.).

Wikipedia does a good job explaining additional reasons for using mock objects, such as working with objects that have non-deterministic results (like DateTime.Now), have states that are difficult to reproduce (like testing how your code behaves if a web service is unavailable), or have operations that are slow to test (like using a test database with setup/teardown scripts).

[NEW] Q: How does JustMock help with mocking?
Mock objects can be created and maintained manually, but this is time consuming and ultimately unproductive approach. A tool like JustMock allows you to focus on writing tests and forget about the mocking details. Mock objects are created automatically in memory when the tests run based on your simple configuration in the unit test. There are no “physical” mock objects that have to be maintained as your project changes.

JustMock goes even further and does not force you to distinguish between “mocks,” “fakes,” and “stubs.” You have a single API for mocking and JustMock handles the rest.

[NEW] Q: Why not mock everything with JustMock?
It is possible to overuse mocking. The goal of mocking is to isolate the code you want to test from other components in the system. There are two rules of thumb that should be followed to ensure mocking is done properly:

  1. Only test (and thus mock) the external behavior of any object
    Mocking the internal behavior of objects can make unit tests brittle and hard to maintain during refactoring.
  2. Mock all objects and external dependencies that are not being actively tested
    Mocking all objects obviously produces meaningless test results. Use mocking to keep unit tests focused the behavior of the method you’re testing. Nothing more. Nothing less.

Q: Is JustMock included in the Telerik Premium Collection?
No, JustMock will not be part of the Premium Collection. Instead, it is one of the new tools included in the recently introduced Ultimate Collection (along with WebUI Test Studio Developer Edition).

Q: Is JustMock available as a standalone product?
Not today, but it will be when it is officially released. Final pricing is not yet set, but you can expect it to be very price competitive with other commercial mocking tools. We currently expect pricing to fall somewhere between $299 and $399.

Q: Is JustMock free and open source?
The beta (which is available now) is free, but the final product will be commercial. JustMock is not an open source product, nor does it derive from an open source library. It’s built from the ground-up by Telerik.

Q: When will JustMock be released?
JustMock is currently in beta, and it will be officially released with our Q2 2009 release (slated for July). The beta is free, though, so download it today and take it for a spin.

Q: How is JustMock different from existing mocking frameworks?
We are well aware that there are many great mocking frameworks available for .NET developers, both open source and commercial. In our experience as a software development company, though, none are perfect and we see room to fill in some gaps and create a better competitive environment around mocking tools. Among the many features in the JustMock beta, here are areas we aim to provide unique value:

  • Performance – We provide both “simple” (aka, Proxy) and Profiler API-based (aka, Elevated) approaches for mocking so that you can use the technique best suited for your code. The simple method is comparable to many open source frameworks and is fast and easy to setup/deploy. The Profiler-based approach is what commercial mocking frameworks tend to employ, and it is more powerful, capable of mocking just about everything. Our hybrid approach is unique and gives you the best of both worlds.
  • Simplicity – We know that mocking is still new for many of you, so we are providing simple to use strongly-typed, fluid API that employs the AAA Patern- Arrange/Act/Assert. There is one simple way to use JustMock, based on the latest framework features, so no “legacy” concepts pollute the API.
  • Integration – Of course, one of the advantages of a Telerik mocking tool is that it’s part of the legendary Telerik stack that includes great support. We plan to really build on the integration story with other tools like JustCode in future releases.

Q: Why does JustMock use the Arrange/Act/Assert pattern? What about Record/Replay?
The Arrange/Act/Assert pattern is a more logical and clean approach to unit testing than the legacy Record/Replay. With AAA, you group your testing actions by function, making it clear what part of your test is involved in setup versus verification. The pattern can be applied to all unit testing, but it is especially useful when mocking is involved.

Record/Replay is an older pattern and it is similar to using GOTO statements in your unit tests. This makes the pattern more difficult to follow and clearly less ideal from a programming perspective. Therefore, JustMock is focused on supporting the AAA pattern.

Q: Is there any JustMock documentation?
Of course! Even in the beta release there is documentation, though most of it exists as “learn by example.” When you download the beta, you’ll find a demo project that shows you through code all of the various ways you can use JustMock. For the final release, we’ll be updating and providing complete product documentation.

You can (and should) also follow Team Lead Mehfuz Hossain’s blog, where he is posting regularly on JustMock.

2 comments:

Anonymous said...

"JustMock is a new Visual Studio productivity tool from Telerik that helps you create unit tests more quickly by automatically mocking, or “faking,” objects in your tests."

Can you elaborate? They keep saying this, but what's a practical example of it?

Todd Anglin said...

@steve - Sure! I'll update the post with more detail, but in short, you "fake" objects so that your unit test does not have external dependencies. When you're testing *your* code, you don't want to cross boundaries. So, you don't want to talk to the DB, the webservice, or the HttpContext. We need a way to "break" those connections so our tests can run. Mocking is how we do that, and JustMock is how we do that easily.