Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab object values during runtime for creating Mock objects required for writing Unit Test

Consider the below class which needs to be tested,

class ToBeTested
{
  Employee _e;
  public ToBeTested(Employee e)
  {
  _e = e;
  }

  void Process()
  {
  // Do something with _e
  }
}

    [TestClass]
    class ToBeTestedTest
    {
    [TestMethod]
    public void TestProcessMethod()
    {
      Employee e = // Initialise it with some test value..
      ToBeTested tbt = new ToBeTested(e);
      tbt.Process();

      //Assert to Verify the test results...  
    }

The Problem is Employee can really be a very complex type with properties in it which themselves can be objects of more classes. It becomes difficult to initialise Employee with Mock Values and generate a testable object.

While debugging I can set a breakpoint and see what Employee object in ToBeTested contains. Is there a way by which I can grab all the values from this object as is available during runtime and use it in my test method?

like image 438
user1928158 Avatar asked Jun 04 '15 11:06

user1928158


People also ask

How do you mock an object in unit testing?

What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

Why mock objects are needed when writing test classes?

We create mock objects to isolate the code we're testing from any code in our org that we're not testing—for example, third-party code, services, and even classes other than the one we're actively testing.

What are mock objects in testing?

Mock objects are a type of test double. They replace production objects to make for easier and more reliable testing practices. If developers want to use mock objects as part of their testing process, they need to be aware of potential pitfalls that can harm their applications' code.

How do you make a mock object in JUnit?

Creating mock objects with the Mockito API class) extension for JUnit 5 in combination with the @Mock annotation on fields. Using the static mock() method. Using the @Mock annotation.


2 Answers

You can use Object Exporter. Its an extension for Visual studio which will generate C# initialization code from any object in your debugging windows. You can use this generated code in your unit test initialization.

like image 116
Carbine Avatar answered Oct 05 '22 23:10

Carbine


The mentioned Visual Studio Extension (OmarElabd/ObjectExporter) was a good idea, but I needed to generate C# code from in-memory objects at runtime, during unit test execution. This is what evolved from the original problem: https://www.nuget.org/packages/ObjectDumper.NET/

ObjectDumper.Dump(obj, DumpStyle.CSharp); returns C# initializer code from a variable. Please let me know if you find issues, you might want to report them on github.

like image 24
thomasgalliker Avatar answered Oct 06 '22 00:10

thomasgalliker