UNIT test dto and domain objects

I plan to write an abstract cool thing to test all DTO and DOMAIN objects. This class will take a template object (general type) and use reflection to get property types inside and assigns some default values ​​to the identified primitive types, and later it will approve these type values ​​by referring to them. Thus, when my DTO tests inherit this class, most of the code is tested with a single line of code written in the test. This is just an idea and you want to know from all of you if I invent a wheel, if something like that already exists? If there is a better way to test the DTO and the domain object with smaller and resuable code.

+3
source share
3 answers

I do not think this is a good approach for testing domain objects. By definition, these objects encapsulate data and their associated behaviors; they are supposed to be much more than just dumb data containers with getters and setters. You will have to manually record unit tests for these objects in the same way as you yourself wrote the objects themselves. Here you are actually planning to spend time in accordance with DDD.

Regarding DTO, you can look at this question.

+3
source

My advice:

  • Not a unit test DTO. These are just simple data structures with a bunch of getters and setters and no behavior. Getters and seters are too dumb to be tested (unless they encapsulate some kind of conditional logic, which is rarely seen with DTO).

  • . , , , , - .

+1

Even if I think this is useless for unit test DTOs based on @Dmitry's answer, I came up with this class:

[TestClass]
public class PeopleTest
{
    [TestMethod]
    public void OneObjectNull()
    {
        Person obj1 = null;
        var obj2 = new Person
        {
            Id = "101",
            Name = "George Waits",
            Address = "Lake Palmer 10"
        };

        Assert.AreNotEqual(obj1, obj2);
        Assert.AreNotEqual(obj2, obj1);
    }

    [TestMethod]
    public void DeepEqual()
    {
        var obj1 = new Person
        {
            Id = "101",
            Name = "George Waits",
            Address = "Lake Palmer 10"
        };

        var peolpleList1 = new List<Person> { obj1 };
        var peolpleList2 = new List<Person> { obj1 };

        Assert.AreEqual(obj1, obj1);
        CollectionAssert.AreEqual(peolpleList1, peolpleList2);
    }

    [TestMethod]
    public void DeepNotEqual()
    {
        var obj1 = new Person
        {
            Id = "101",
            Name = "George Waits",
            Address = "Lake Palmer 10"
        };

        var obj2 = new Person
        {
            Id = "102",
            Name = "Rachel Smith",
            Address = "Lake Palmer 10"
        };

        var peolpleList1 = new List<Person> { obj1 };
        var peolpleList2 = new List<Person> { obj2 };

        Assert.AreNotEqual(peolpleList1, peolpleList2);

        var group1 = new KeyValuePair<string, List<Person>>("group1", peolpleList1);
        var group2 = new KeyValuePair<string, List<Person>>("group2", peolpleList2);

        Assert.AreNotEqual(group1, group2);
    }

    [TestMethod]
    public void PropertyPositive()
    {
       var obj1 = new Person
        {
            Id = "101",
            Name = "George Waits",
            Address = "Lake Palmer 10"
        };
        obj1.Address = "Parker av 101";

        var obj2 = new Person
        {
            Id = "102",
            Name = "Rachel Smith",
            Address = "Lake Palmer 10"
        };
        obj1.Address = "Listener av 45";

        Assert.AreNotEqual(obj1, obj2);
    }
}
+1
source

All Articles