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
Helen Y.Helen Y. 

Assertion Failed: Expected: Account:{Name=}, Actual: null

Hi All,

I am totally new in program language world, just doing exercise on Headtrail and met this question in debug log:
Assertion Failed: Expected: Account:{Name=}, Actual: null
Cannot figure out what is the difference between Name = and null, can someone help how can this work? Thanks.
 
sandeep sankhlasandeep sankhla
Hi Snow,

Account:{Name=} ----> this is a string which you have kept in your assert..


string is not equal to null ...

Please paste your code for same so I can guide you to correct it..

Thanks,
Sandeep
Helen Y.Helen Y.
Hi Sandeep,

Please review my work:

public class AccountHandler {
/*The Apex class must be called 'AccountHandler' and be in the public scope. The Apex class must have a public static method called 'insertNewAccount'.The 'insertNewAccount' method must accept an incoming string as a parameter, name the account after the parameter, insert it into the system and then return the account record. The 'insertNewAccount' method must also accept an empty string, catch the failed DML and return null.*/
  public static account insertNewAccount(String s){
  Account acctList = new Account(Name = s);
  try{ insert acctList;
   system.debug('Account List is:'+acctList);
  }catch(DmlException e){
   system.debug('A Dml exception has occurred:' + e.getMessage());
   }
   if(acctList.name!= null)
   return acctList;
   if(acctList.name == '') {
       system.debug('Empty string passed.');}
   return acctList;
 }
}
sandeep sankhlasandeep sankhla
Hi Snow,

Please paste your test class where you have kept assert statement..error is in test class not in main class...

Thanks,
Sandeep
Helen Y.Helen Y.
Sorry, Sandeep.

So the code gets called from anonymous block:
Account a = AccountHandler.insertNewAccount(''); 
System.assertEquals(a,null);

I wanna exam when passing an empty string then returns null as expected. 
sandeep sankhlasandeep sankhla
Hi Snow,

You can simply replace your assert statment with below code

System.assertEquals(a.Name, '');

Please check and let me know if this help you...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer 

 
Helen Y.Helen Y.
Good point, Sandeep.

But how about if I cannot change in the 
System.assertEquals(a,null);

Do you think of any changes need in my code?

Really appreciate your guide, learned a lot.
sandeep sankhlasandeep sankhla
Hi Snow,

If you will not change your assert then you will keep getting the error, because your instance for Account object is not null..you will not get null if Account is not inserted...

so if you can use a.Name to check if it is what you have pased then it is inserted else not...but Null you will never get...

Thanks,
Sandeep
sandeep sankhlasandeep sankhla
Hi Snow,

If you dont want to change your assert then you can add a simple if condition in your class to check if string is not null or blank if yes then return null..

see below code
 
public static account insertNewAccount(String s){

    if(s != '' && s!= null){

  Account acctList = new Account(Name = s);

  try{ insert acctList;

   system.debug('Account List is:'+acctList);

  }catch(DmlException e){

   system.debug('A Dml exception has occurred:' + e.getMessage());

   }

   if(acctList.name!= null)

   return acctList;



   if(acctList.name == '') {



       system.debug('Empty string passed.');}
   return acctList;

 }
    else 
        return null;
}


Account a = insertNewAccount(''); 
System.assertEquals(a,null);
you will get null if you have passed blank and null as value in parameter..

Thanks,
Sandeep
 
Helen Y.Helen Y.
Awosome Sandeep! It worked, I got this badge now. Could you help me understand why can this line do the magic?
sandeep sankhlasandeep sankhla
Hi Snow, 

As I mentioned above, it will never return you null if you will pass blank and null..you can simply do null or blank check there....null and blank it wont accept in name field which is mandatory one..so it will not get inserted..you can simply do field check instead of null check , or if you wan to do null check then you can use above code where it will check if null or blank then return null...

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Thanks,
Sandeep
Salesforce Certified Developer