function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Cristian TrifCristian Trif 

What assertions can i do in my test class?

So i created my test class for a controller. And I have coverage 100% but i need to make some assertions using the System.assert(), System.assertEquals(), System.assertNotEquals(), I wrote a simple assert but it's way to simple. 

@isTest 
public class ControllerTestClass 
{
   static testMethod void testMethod1() 
   {

        screen1 sc = new screen1();
        Account acc = sc.getAccount();
        acc.name = 'TestAcc';
        sc.save();
       
        System.assertEquals('TestAcc', acc.name);

          PageReference pageRef = Page.screen1VSF ;
          pageRef.getParameters().put('id',acc.Id);
          Test.setCurrentPage(pageRef);

     }
}


And the class which I'm testing.
public class screen1 
{
  
    public Account myAccount;
    public Account callAccMethod; 
    
    public Account getAccount()
    {
        if(myAccount == null)
        {
            myAccount = new account();
        }
        return myAccount;
    }

    public PageReference save()
    {
       
       callAccMethod = getAccount();
       if(callAccMethod != null)
       {
           insert myAccount;
       }
       
       PageReference pageRef = new PageReference('/apex/screen2');
       pageRef.getParameters().put('id', myAccount.Id);
       return pageRef;
       
    }
    
}
Best Answer chosen by Cristian Trif
Gaurish Gopal GoelGaurish Gopal Goel
Hi Cristian,

You can make the following assertion. If you Account ID has been generated then it means that your account was successfully inserted.
1. Check whether the account ID has been generated or not.
System.assertNotEquals(null, acc.Id);

If this answer solves your problem then mark it as the solution to help others. Thanks.

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

This methods can be used based on the logic in your code.

System.Assert accepts two parameters, one (mandatory) which is the condition to test for and the other a message (optional) to display should that condition be false.
System.AssertEquals and System.AssertNotEquals both accepts three parameters; the first two (mandatory) are the variables that will be tested for in/equality and the third (optional) is the message to display if the assert results in false.

Refer below link which has the explanation with example.

https://salesforce-parth.blogspot.sg/2016/07/salesforce-systemassertmethods.html
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
 
Best Regards
Sandhya
 
 
Gaurish Gopal GoelGaurish Gopal Goel
Hi Cristian,

You can make the following assertion. If you Account ID has been generated then it means that your account was successfully inserted.
1. Check whether the account ID has been generated or not.
System.assertNotEquals(null, acc.Id);

If this answer solves your problem then mark it as the solution to help others. Thanks.
This was selected as the best answer