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

Announcements

News and Announcements icon
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,843 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
    239,660 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
    239,660 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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

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

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 612

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 592 Super User 2026 Season 1

#3
Abhilash Warrier Profile Picture

Abhilash Warrier 586 Super User 2026 Season 1

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans