web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

Unit testing

(0) ShareShare
ReportReport
Posted on by 465

Hi,

Let's say i have the following class:

class class1
{
    public tableX getInfo(tableY _tableY)
    {
        tableX      tableX;
        this.validate(_tableY);
        int          x    =   2;

        if (x == 2)
        {    

            tableX = this.insertTableX(_tableY.Id);

        }

        return tableX;

    }

    public void validate(tableY _tableY)
    {
        if (!_tableY.Id )
        {
            throw Error(strFmt("@label:Error1"   " "   "@label:Error2",_tableY.desc));
        }
        if ( _tableY.Age != 0 )
        {
            throw Error(strFmt("label:Error1"   " "   "@label:Error3",_tableY.desc));
        }
    }


    }

    /// 
    ///   
    /// 
    public tableX insertTableX(tableY _tableY)
    {
        ttsbegin;
        _tableX.Id = _tableY.Id;
        _tableX.desc = _tableY.desc;
        _tableX.Age = _tableY.Age;
        _tableX.insert();
        ttscommit;

        return tableX;
    }

}

As you can see in method getInfo (i call the other two methods) so when i do unit testing  1) should i test the method getInfo or only the other two methods?

This is my first time doing unit testing. So i wanted to start with only the method Validate():

[SysTestTargetAttribute('class1',UtilElementType::Class)]
class class1Test extends SysTestCase
{
    tableX tableX;
    tableY tableY;
    class1 class1;
    /// 
    ///
    /// 
    public void setUp()
    {
        class1 = new class1();
        super();
    }

    [SysTestMethodAttribute]
    public void testValidate_ValidationPassed()  //it passed
    {       
        tableY.Id = "1";
        tableY.desc = "desc";
        tableY.Age = 3;
        class1.validate(tableY);
        this.parmExceptionExpected(false);
    }

    [SysTestMethodAttribute]
    public void testValidate_IdMissing() //this is failing why?
    {
        tableY.Id = "";
        tableY.desc = "desc";
        tableY.Age = 3;
        class1.validate(tableY);
        this.parmExceptionExpected(true,strFmt("@label:Error1"   " "   "@label:Error2",tableY.desc);
    }
    
    [SysTestMethodAttribute]
    public void testValidate_AgeMissing()   //this is failing why?
    {
        tableY.Id = "1";
        tableY.desc = "desc";
        tableY.Age = 0;
        class1.validate(tableY);
        this.parmExceptionExpected(true,strFmt("@label:Error1"   " "   "@label:Error3",tableY.desc);
    }

}

As you can see i made three scenarios for method validate 2) is that correct? and i read about AAA concept, 3) am i doing it right? or i should have defined tableX,table Y in the setUp method? Also is it correct that i'm redefining the values of tableY in each method?

4) testValidate method passed but testValidate_IdMissing() and testValidate_AgeMissing() failed can someone tell me why????? I noticed that this is because the actual method throws an error before it reaches parmExceptionExpected so how should i do my unit testing for this?

5) how to do unit testing for the insert method?

I have the same question (0)
  • D365FO user Profile Picture
    465 on at

    Answering my forth question, it seemed  that i should write this.parmExceptionExpected() before calling the actual method : class1.validate()

  • Verified answer
    Jie G Profile Picture
    on at

    Hello,

    Here are the answers for your questions:

    1) Unit testing is targeting at the method itself. You should write test cases for all your methods. When test getInfo(), ideally you should write the stub for the other methods it’s calling.
    2) You should consider all the possible scenarios for validate(), e.g. _tableY Is null, _tableY.Id Is null/zero, _tableY.Age Is zero/non-zero…
    3) It’s up to you to choose which kinds of initialization method. In a complex test case, usually we consider to initialize the parameters in the setUp().
    4) Yes, you should write this.parmExceptionExpected() before calling the actual method.
    5) Call insertTableX with your test data and query the table to verify the test result.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 544 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 450 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 250 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans