Using a mocking tool to write the unit tests required for your existing C# app is an easy task with JustMock.
StackOverflow is probably the largest and best-known developer community, right? And here is a random question with 23K views (and it is not the only one regarding this delicate subject):
Can unit testing be successfully added into an existing production project?
If so, how and is it worth it?
Long story short—yes and it is totally worth it. Regarding the ‘how’—take your time and read on.
If you happen to be in the exact same position and are asking yourself the above question—welcome to the right place! No worries, there is a cure. And you do not even have to climb too steep of a hill for it.
I have already chosen my ‘victim’ for our little experiment. It is called ERP and is part of Telerik UI for WPF’s Sample Applications. You can check it out here and download its source code by following the steps from the Download Product Files article. Long story short, you need a Telerik account and the zip before the last from the following screenshot:
I am glad that we cleared the above up, we can go on with the real deal now. Ready? I'm ready.
Writing a thorough set of unit tests is definitely time-consuming. Even a simple if statement with one line of code and a Boolean condition would require at least two tests to cover the possible outcomes (one for true, and one for false). Imagine what would be if the lines of code and the conditions were more than one. Yes, it would all escalate pretty fast. 😂
Have you heard about mocking?
Mocking is a process employed in unit testing to simulate external dependencies.
Kurzgesagt (a.k.a. in English, in a nutshell), mocking is a concept in unit testing where real objects are substituted with fake objects that imitate the behavior of the real ones. It is done so that a test can focus on the code being tested and not on the behavior or state of external dependencies.
Of course, these fake (or mock) objects can be created and maintained manually. Doing it all by yourself could cost you tons of time and be ultimately unproductive (and sometimes boring 🤫). Oh, and let's not forget the case in which your app code is so complex that you need to either do a major refactoring, or (even worse)—rewrite it completely from scratch.
That's why I would suggest use a mocking framework. Especially when talking about apps written without the slightest idea for covering with unit tests.
What is a mocking framework?
Mocking frameworks are used to generate replacement objects like Stubs and Mocks. Mocking frameworks complement unit testing frameworks by isolating dependencies but are not substitutes for unit testing frameworks. By isolating the dependencies, they help the unit testing process and aid developers in writing more focused and concise unit tests. The tests also perform faster by truly isolating the system under test.
Currently, there are some free mocking frameworks on the market, yet my honest opinion is that they are simply insufficient. Most mocking frameworks have limitations as to what they can mock. The limitation comes from the way the mock object is created, which is through inheritance. The mock object can inherit only public APIs like virtual methods, abstract classes and interfaces. This functionality may be enough for your project as many companies have internal projects where all APIs can be public as they won’t be consumed by a third party. Yet, if your development team is dealing with internal, private logic, static calls or a dependency on a third-party library, then Telerik JustMock is a better suited solution. It helps you advance the unit testing of C# devs.
Spending money for a complete mocking tool like JustMock enables developers to focus solely on testing the system under test and forget about the distracting mocking details. Mock objects are created automatically in memory when the tests are run based on the simple configuration in the unit test. There are no ‘physical’ mock objects that must be maintained as the project changes.
Telerik JustMock is the fastest, most flexible, and complete mocking solution for crafting unit tests that support Blazor, ASP.NET Core, ASP.NET MVC, ASP.NET Web Forms, .NET Core, WPF, WinForms and the current preview of .NET 5. And it will definitely help you solve all potential problems before being busted by the QA. 🤓 Oh, yeah!
With Telerik JustMock, any developer could start creating mocks, subs and fakes and use them in their tests. Wanna learn how JustMock makes unit tests more concise? Okay, let's dive right in.
Let's first sum up the requirements of our experiment. We have:
I suggest we take a look at the ERP app for starters:
The times I have seen or interacted with it can be counted on the fingers of a hand. But that should not bother me, or you (well, at least not a lot).
Forgot to mention something about JustMock – it follows the Arrange-Act-Assert Pattern. The AAA Pattern makes a test look simpler and more structured by dividing it in three subsections, as explained below:
Assert – the section where all verifications of expectations and results are performed
Having the JustMock extension installed, you are guaranteed a flying start. The project templates that come with it helped me create a test project in seconds.
Now, I'm ready for unit testing!
See the edit and delete buttons at the left below the Breadcrumb bar? I bet there is a Command related to both.
Noticed that the Orders item is initially selected? Surprisingly (or not) its DataContext is the OrdersViewModel. Back to the commands—yup, there they are:
Let us see how the above can be covered with unit tests and write our first ones with Just Mock! The purpose of both tests—suppose there is a direct binding between the buttons and the commands. This way, we will be able to test the commands themselves. All dependencies (e.g., the ERPDialogFactory, the SalesOrderHeader, the CollectionView) will be mocked and the successful execution of the respective methods (e.g., the ShowEditDialog, ShowDeleteDialog, Refresh)—verified. Starting with the EditRecordCommand.
Covering the DeleteRecordCommand with a unit test looks a lot like the EditRecordCommand's:
What about the print button at the right below the Breadcrumb bar? That button:
It has a PrintTarget set and I am guessing this is what I'm looking for. Let's peek at where this PrintTarget comes from and what it is supposed to do. This appears to be a dependency property, the setter of which will be called, and respectively—the PropertyChanged, when the above Binding is executed. This leads us to:
Let's start from the button's XAML definition this time and test whether the binding is correctly resolved and respectively—clicking this button does its magic.
This unit test does not seem sufficiently stable enough. It only ensures that there is a print button and when it is clicked, the static method of the above helper class is called. But does it function properly, i.e., what exactly happens when the button's command is executed? Let us test:
Here, we need to arrange three more things (apart from those in the previous test)—the window, the spreadsheet and its workbook (these will do the magic of exporting the grid data into a spreadsheet's workbook).
The act we are performing is calling the OnPrintCommandExecuted method with the mocked grid view.
The assert phase ensures that all the above arrangements were made, this time including the ones regarding the grid, the spreadsheet and its workbook apart from the PrintExportHelper.
This is the happy path of the print functionality. What about passing an item, different than the expected grid view—e.g., an empty string, to the print command? Let us cover that with a test—assuming we passed a fake argument, the print command execution is expected to fail, right?
Guess what, it did. Not the test. The command execution—we arranged that no invocations of the above methods have ever occurred when acting with an empty string instead of the expected grid view.
Well, seems like the export, delete and print functionalities work as expected. Good job!
What about the last button of this button bar—the export? Well, I'm leaving this as your JustMock homework. 📚
If you are intrigued (hopefully you are 🤞), I'll be more than happy to hear your honest feedback in the comments.
Regardless of the above ‘cases,’ don't be shy to:
Try The Latest JustMock Now
You won't regret it.
Viktoria Grozdancheva is a frontend developer, part of the Progress Telerik UI for WPF & Silverlight team in Sofia. She holds a bachelor's degree in Computer Systems and Technologies. In her spare time, Viktoria loves practicing (and constantly updating) her collection of art hobbies, playing sports and hanging out with friends.
Subscribe to be the first to get our expert-written articles and tutorials for developers!
All fields are required