3

I am planning to write an abstract class kind of thing for testing all my DTOs and DOMAIN objects. This class will take in templatable object (generic type)and use reflection to get the properties' types within and will assign some default values to the primitive types identified and later will assert these type values by accessing them. This way whenever my DTO tests inherit this class, most of the code is tested with one line of code written in the test. This is just an idea and want to know from you all if I am reinventing the wheel if something like this already exists? If there is a better way to test DTO's and domain object with less and resuable code.

2
  • 2
    if i understood you correctly you try to test if the mapping beween domainobject and dto is complete ie no attribute is missing/lost. if you add the programminglanguage to you question there might be already a solution for this. From my experience in .dot its only worth the efford if you have many dtos( > 20). your testapi must consider properties to be excluded from conversion and order of collections that might differ Commented May 9, 2012 at 14:00
  • You should test behavior, not data structures...DTO are data structure, you just need to test the aggregates method that modify their state with some behavior (validation, if, rules), it's worthless test set methods...maybe it's better use some library like project lombok to generate them if you feel the need to test them... Commented Dec 29, 2016 at 14:00

3 Answers 3

4

I don't think that this is a good approach for testing Domain objects. By definition, these objects encapsulate data and related behavior, they suppose to be much more than just dumb data containers with getters and setters. You will have to hand write unit tests for these objects just like you hand-wrote the objects themselves. This is where you actually suppose to be spending time according to DDD.

Regarding DTOs you may want to look at this question.

Sign up to request clarification or add additional context in comments.

2 Comments

I agree about Domain object testing. One more small question. If few of my Domain objects are just get/set can they be used as DTO also. can I use domain objects as dto for different cases for my webservices.
I would not use domain objects as DTO because it will couple your web service consumers to your Domain objects. This will cause issues when you want to evolve your domain without affecting web service clients.
3

My advice :

  • Don't unit test DTO's. These are just simple data structures with a bunch of getters and setters and no behavior. Getters and setters are too dumb to be tested (unless they encapsulate some kind of conditional logic which is rarely the case with DTO's).

  • Don't try to automate or genericize your domain object tests. I can't see how code that tests their behavior can be reused anyway since they all have a different behavior by definition.

Comments

0

Even though I think is kind of worthless to 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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.