Telerik® JustMock™ by Progress

The Telerik JustMock PrivateAccessor allows you to call non-public members of the tested code right in your unit tests.

The PrivateAccessor feature is enabled for both Free and Commercial versions of JustMock.

To give examples of how the PrivateAccessor can be used within your tests, we will use the following sample class:

Calling private methods

The next example will show how to invoke non-public method with the Telerik JustMock PrivateAccessor:

To call non-public methods with the PrivateAccessor you must:

  1. Wrap the instance holding the private method: var inst = new PrivateAccessor(new ClassWithNonPublicMembers());
  2. Call the non-public method by giving its exact name: inst.CallMethod("MePrivate");
  3. Finally, you can assert against its value: Assert.AreEqual(1000, actual);

Next, we will show how to call non-public methods from a mocked instance:

  1. Create a mocked instance of your class under test (you can also use original instance object and perform partial mocking later on): var mockedClass = Mock.Create<ClassWithNonPublicMembers>(Behavior.CallOriginal);
  2. Arrange your expectations: Mock.NonPublic.Arrange<int>(mockedClass, "MePrivate").Returns(5);
  3. Then, create new PrivateAccessor with the mocked instance as an argument: var inst = new PrivateAccessor(mockedClass);
  4. Call the non-public method by giving its exact name: inst.CallMethod("MePrivate");
  5. Finally, you can assert against its expected return value: Assert.AreEqual(5, actual);

Calling static private methods

The next example will show how to invoke non-public static method with the Telerik JustMock PrivateAccessor:

To call non-public static methods with the PrivateAccessor you must:

  1. Wrap the instance holding the private method by type: var inst = PrivateAccessor.ForType(typeof(ClassWithNonPublicMembers));
  2. Call the non-public static method by giving its exact name: inst.CallMethod("MePrivate");
  3. Finally, you can assert against its value: Assert.AreEqual(2000, actual);

Next, we will show how to call non-public static methods from a mocked instance:

  1. Setup your class for static mocking (this is not needed if you are to perform partial mocking later on): Mock.SetupStatic(typeof(ClassWithNonPublicMembers));
  2. Arrange your expectations: Mock.NonPublic.Arrange<int>(typeof(ClassWithNonPublicMembers), "MeStaticPrivate").Returns(5);
  3. Then, create new PrivateAccessor with the mocked instance type as an argument: var inst = PrivateAccessor.ForType(typeof(ClassWithNonPublicMembers));
  4. Call the non-public method by giving its exact name: inst.CallMethod("MeStaticPrivate");
  5. Finally, you can assert against its expected return value: Assert.AreEqual(5, actual);

Get or Set private properties

The next example will show how to get or set the value of a non-public property with the Telerik JustMock PrivateAccessor:

In the above example, we are:

  1. Passing the instance holding the private property to the PrivateAccessor: var inst = new PrivateAccessor(new ClassWithNonPublicMembers());
  2. Setting the value of the private property Prop to 555: inst.SetProperty("Prop", 555);
  3. Asserting that, the getter of Prop must return the above set value: Assert.AreEqual(555, inst.GetProperty("Prop"));

Next, we will show how to get an arranged non-public property from a mocked instance:

  1. Create a mocked instance of your class under test (you can also use original instance object and perform partial mocking later on): var mockedClass = Mock.Create<ClassWithNonPublicMembers>(Behavior.CallOriginal);
  2. Arrange your expectations: Mock.NonPublic.Arrange<int>(mockedClass, "Prop").Returns(5);
  3. Then, create new PrivateAccessor with the mocked instance as an argument: var inst = new PrivateAccessor(mockedClass);
  4. Cet the non-public property by giving its exact name: inst.GetProperty("Prop");
  5. Finally, you can assert against its expected return value: Assert.AreEqual(5, actual);

See Also