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 tests for void methods

(0) ShareShare
ReportReport
Posted on by 35

Hello everyone :) 

So I am working on some unit test, 

Let's say I have this method for whom should be written a unit test

 public void parmItemId(ItemId _itemId = itemId)
    {
        itemId = _itemId;
    }

So , how should i test this method which returns void ?

I have the same question (0)
  • Suggested answer
    GirishS Profile Picture
    27,827 Moderator on at

    Hi AnawI,

    Method you have created is parm method which is used to get and set the values.

    To test that you just assign the values to a parm method and print it to Infolog. Create a class.

    In the main method of your class assign the values to parm method and print the same.

    ClassName
    {
        ItemId itemId;
        Public static void main(Args _args)
        {
            ClassName name1 = new className();
            name1.parmItemId("Item1");
            info(strfmt("%1",itemId));
        }
        
        public void parmItemId(ItemId _itemId = itemId)
        {
            itemId = _itemId;
        }
    }

    Thanks,

    Girish S.

  • AnawI Profile Picture
    35 on at

    Okey,

    but actually, i want to do this with assert method, so it runs like a test

  • Verified answer
    Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    You can test methods that return void. Their purpose is change the state of an object, therefore you can test whether the state has changed as expected. It means that you must be able to somehow observe the state from your test method.

    Usually the object should expose the state by a method. You'll call the method and check the response.

    If there is nothing public that you could check, but there is something protected, you could create a child of the class (in your test project) and expose the state there.

    If all you have is private variable, you can't read it from the test class. But you could observe the behaviour influenced by the changed state and verify whether changing the state had the expected effect.

    In general, the point is testing the behavior, not that the assignment operator works.

    By the way, you method looks strange. If you meant to implement a normal parm method, you should return a value. If you want to have just a setter, remove the default parameter, because calling the method without the parameter makes no sense.

  • AnawI Profile Picture
    35 on at

    So, with that being said ,testing the method with assertNull is it a good idea?

    [SysTestMethodAttribute, SysTestNonCheckInTest, TestMetadataAttribute(classStr(Class), methodStr(Class, parmItemId))]
        public void parmCustomsDuty()
        {
            itemId = "test$$$";
    
            this.assertNull(class.parmItemId(itemId),'Test successeded');
        }

  • Martin Dráb Profile Picture
    237,795 Most Valuable Professional on at

    No, it's not a good idea.

    It doesn't make sense and it won't even compile.

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 663 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 348 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans