You need to sign in to do that
Don't have an account?
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.
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.
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
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;
}
}
Please paste your test class where you have kept assert statement..error is in test class not in main class...
Thanks,
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.
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
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.
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
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
you will get null if you have passed blank and null as value in parameter..
Thanks,
Sandeep
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