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
Trif Cristian 7Trif Cristian 7 

How to write a System.assert() for this test method?

static void testUpdate() {
    User userStandard = TestUtil.getUserStandardDE(0);
    System.runAs(userStandard) {
        Account accountBusiness= [Select Id from Account where Name = :TestUtil.ACCOUNT_BUSINESS_NAME LIMIT 1];
        Account accountBusinessDuplicate= [Select Id from Account where Name = 'DuplicateCompany' LIMIT 1];

        AccountDuplicate__c ad = new AccountDuplicate__c();
        ad.Account1__c = accountBusiness.Id;
        ad.Account2__c = accountBusinessDuplicate.Id;
        ad.Status__c = 'Open';
        ad.Criteria__c ='Other matches';

        Test.startTest();
        insert ad;


        ad.Remarks__c = 'changed';
        update ad;


        ad.Status__c = 'Rejected';
        update ad;


        ad.Status__c = 'Open';
        update ad;


        Test.stopTest();
    }
}
Best Answer chosen by Trif Cristian 7
v varaprasadv varaprasad
Hi Trif,

Please check once below sample code.


 
static void testUpdate() {
    User userStandard = TestUtil.getUserStandardDE(0);
    System.runAs(userStandard) {
        Account accountBusiness= [Select Id from Account where Name = :TestUtil.ACCOUNT_BUSINESS_NAME LIMIT 1];
        Account accountBusinessDuplicate= [Select Id from Account where Name = 'DuplicateCompany' LIMIT 1];

        AccountDuplicate__c ad = new AccountDuplicate__c();
        ad.Account1__c = accountBusiness.Id;
        ad.Account2__c = accountBusinessDuplicate.Id;
        ad.Status__c = 'Open';
        ad.Criteria__c ='Other matches';

        Test.startTest();
        insert ad;


        ad.Remarks__c = 'changed';
        update ad;


        ad.Status__c = 'Rejected';
        update ad;


        ad.Status__c = 'Open';
        update ad;
        
		//assert mean true or false	Using assert statements in test class we will check our expected result in test class.
         system.assert(ad.Status__c != null);	
		 system.assert(ad.Status__c == 'Open');  //assert mean true or false	
         system.assertEquals('Open', ad.Status__c);  //Here assert equals means it will check your expected value is coming or not.
        Test.stopTest();
    }
}

More Info : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 

All Answers

Vinod ChoudharyVinod Choudhary
Hi Trif 

A simple scenario would be to create a test class +methods in which some work is done (e.g. put something into the DB, update a field or whatever). Once this is done you would use System.Assert functions to check what has been done.

For example:-
integer i = 0;
integer j = 1;

//this would throw an error  and cause your test to fail 
System.assertEquals(i,j);
//this would pass
System.assertNotEquals(1,i);
 
you could interchange any of the Assert functions dependent on your logic. Up to you. I find it easier to use AssertEquals as I mostly want to check that a proper number of records have been returned or that something I know about has happened and I'm expecting a certain result etc...
 

 
Thanks
Vinod
v varaprasadv varaprasad
Hi Trif,

Please check once below sample code.


 
static void testUpdate() {
    User userStandard = TestUtil.getUserStandardDE(0);
    System.runAs(userStandard) {
        Account accountBusiness= [Select Id from Account where Name = :TestUtil.ACCOUNT_BUSINESS_NAME LIMIT 1];
        Account accountBusinessDuplicate= [Select Id from Account where Name = 'DuplicateCompany' LIMIT 1];

        AccountDuplicate__c ad = new AccountDuplicate__c();
        ad.Account1__c = accountBusiness.Id;
        ad.Account2__c = accountBusinessDuplicate.Id;
        ad.Status__c = 'Open';
        ad.Criteria__c ='Other matches';

        Test.startTest();
        insert ad;


        ad.Remarks__c = 'changed';
        update ad;


        ad.Status__c = 'Rejected';
        update ad;


        ad.Status__c = 'Open';
        update ad;
        
		//assert mean true or false	Using assert statements in test class we will check our expected result in test class.
         system.assert(ad.Status__c != null);	
		 system.assert(ad.Status__c == 'Open');  //assert mean true or false	
         system.assertEquals('Open', ad.Status__c);  //Here assert equals means it will check your expected value is coming or not.
        Test.stopTest();
    }
}

More Info : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com
 
This was selected as the best answer
v varaprasadv varaprasad
system.assert(ad.Status__c = 'Open');