This content originally appeared on Telerik Blogs and was authored by Mihail Vladov
Telerik JustMock R1 2022 brings great performance optimization, the ability to disable features, support for C# 10, and support for official versions of VS 2022 and .NET 6.
Hey, developer folks! It's R1 2022 release time for our mocking framework, Telerik JustMock, which includes significant performance optimization, the ability to disable features that affect performance, support for C# 10, plus support for official versions of Visual Studio 2022 and .NET 6. Let’s look at the details.
Performance Optimization (Beta)
Those of you who follow the releases of JustMock already know that, in the past year, we released a lot of optimizations around the JustMock profiler. With R1 2022, we made it possible for the profiler to work on demand. What does that mean? To explain, I will start with how JustMock worked before. As you know, JustMock is able to mock everything. This ability is achieved by using a profiler which inserts the required code into all compiled code. This process is known as instrumentation. The optimization now allows the profiler to skip this step and instrument the code on demand when you use the JustMock API.
The optimization is quite significant and varies depending on your exact scenario. Our measurements shows that in worst-case scenarios the performance gain is around 40% and in best-case scenarios, around 90%. You are probably asking what is a worst-case scenario. Such a scenario is when almost all of the code you have is mocked in some of the unit tests. A best-case scenario is the opposite, when almost no code is mocked.
As the change in how JustMock works is significant, we are releasing this optimization in Beta.
You are probably wondering how you can take advantage of all of this. Well, its easy. Just open the JustMock Visual Studio extension, navigate to Options and enable the "On Demand Instrumentation Enabled" option.
The options are also available through Tools -> Options -> Telerik -> JustMock.
Because not all methods will be instrumented each time, two functional breaking changes arise.
The first one is related to mocking the new operator. With the normal instrumentation, there is the possibility an already existing instance of a particular class will be returned when creating a new instance of that class. Here is an example:
[TestMethod]
public
void
ShouldMockNewObjectCreation()
{
// ARRANGE - Every new instantiation of the Foo class should return a predefined instance.
var testObj =
new
Foo();
testObj.MyProp =
"Test"
;
Mock.Arrange(() =>
new
Foo()).Returns(testObj);
// ACT
var myNewInstance =
new
Foo();
// ASSERT
Assert.AreEqual(
"Test"
, myNewInstance.MyProp);
}
With the on demand optimization enabled, mocking the new operator does not work in all cases. For that reason, you should use IgnoreInstance
in those cases. Here is an example of how to rework the previous example:
[TestMethod]
public
void
ShouldUseIgnoreInstance()
{
// ARRANGE - Every new instantiation of the Foo class should a property with pedefined value.
var testObj =
new
Foo();
Mock.Arrange(() => testObj.MyProp).IgnoreInstance().Returns(
"Test"
);
// ACT
var myNewInstance =
new
Foo();
// ASSERT
Assert.AreEqual(
"Test"
, myNewInstance.MyProp);
}
The second functional breaking change is related to the usage of actions, particularly when setting a property value with the ArrangeSet(Action) method. Here is an example:
[TestMethod]
public
void
Bar_OnSetTo1_ShouldNotify()
{
// Arrange
// Creating a mocked instance of the "IFoo" interface.
var foo = Mock.Create<IFoo>();
bool
isSetTo1 =
false
;
// Arranging: When foo.Bar is set to 1 it should change "isSetTo1" to true.
Mock.ArrangeSet(() => { foo.Bar = 1; }).DoInstead(() => isSetTo1 =
true
);
// Act
foo.Bar = 1;
// Assert
Assert.IsTrue(isSetTo1);
}
The reason for this breaking change is that an action can’t provide information about the property owner type in all scenarios, and JustMock doesn’t know which type it should instrument. This information can be supplied using the overload ArrangeSet<OwnerType>(Action)
. Here is an example of reworking the previous example:
[TestMethod]
public
void
Bar_OnSetTo1_ShouldNotify()
{
// Arrange
// Creating a mocked instance of the "IFoo" interface.
var foo = Mock.Create<IFoo>();
bool
isSetTo1 =
false
;
// Arranging: Provide the type owner of the Bar property
Mock.ArrangeSet<IFoo>(() => { foo.Bar = 1; }).DoInstead(() => isSetTo1 =
true
);
// Act
foo.Bar = 1;
// Assert
Assert.IsTrue(isSetTo1);
}
We hope that most of you will adapt the new on demand approach, and after most of you do that we will set this behavior to be the default for Telerik JustMock. Give it a try and get back to us with your results and feedback.
Ability To Disable Features That Have an Impact On the Performance
You’ve probably noticed that there are additional options in the Profiler Runtime Options. Those options allows you to disable features that have an impact on the performance. I will explain each one of them.
Automatic Mock Repository Cleanup Enabled
There are a lot of situations where a mock can’t be associated to an object. An example is a mock of a static method. As the mock is not associated to an object, it can’t be freed when an object is collected by the garbage collector. For that reason, the JustMock profiler inserts a call to the Mock.Reset method at the end of each unit test. Monitoring if a test method is compiled by the CLR leads to slowdown in the execution time.
With this option, you can disable that instrumentation which will result in performance improvement. Please have in mind that this can lead to memory leaks if you do not add a call to Mock.Reset in each unit test method that uses JustMock. Use it with caution.
DLLImport Method Instrumentation Enabled
The name speaks for itself. This options controls whether methods marked with the DLLImport attribute can be mocked. This feature can be safely disabled if no mocking of such methods is expected.
Asynchronous Test Context Resolution Enabled
This option controls the execution of asynchronous test methods and whether async tests should be instrumented. This feature can be safely disabled if only synchronous test methods are executed.
Support for C# 10
I am happy to announce that we’ve tested all new features that come with C# 10, and we can now claim support.
Support for Official Version of Visual Studio 2022
Following the latest technology releases, we introduced support in JustMock for the official version of Visual Studio 2022.
Support for Official Version of .NET 6
Following the development of the .NET, we introduced support in JustMock for the official version of .NET 6.
Get the New Version Now
The R1 2022 release is already available for download in customers’ accounts. If you are new to Telerik JustMock, you can learn more about it via the product page. It comes with a 30-day free trial, giving you some time to explore the capabilities of JustMock.
Sign Up for the Webinar and Catch Us on Twitch
To see the new release in action, please join us on the Telerik R1 2021 webinar, on Thursday, February 3, 2022, at 11:00 am ET when our developer advocates will dive into your favorite Telerik productivity features.
Twitch
In addition, we have a Twitch session planned, which will cover our mocking tools in addition to embedded reporting and automated testing on Friday, January 28, 2022, at 10:00 am ET.
Let Us Know Your Thoughts
Feel free to drop us a comment below sharing your thoughts. Or visit our Feedback Portal and let us know if you have any suggestions or if you need any particular features.
You can also check our release history page for a complete list of the included improvements.
This content originally appeared on Telerik Blogs and was authored by Mihail Vladov
Mihail Vladov | Sciencx (2022-01-19T16:18:44+00:00) What’s New in Telerik JustMock With R1 2022. Retrieved from https://www.scien.cx/2022/01/19/whats-new-in-telerik-justmock-with-r1-2022/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.