You need to sign in to do that
Don't have an account?
St2018
How to write unit test?
Im having trouble writing unit test for my method. I've gone through apex testing on trailhead but I don't fully grasp how to get started. Its still a confusing topic for me. Below is the method I would like to write unit test for. An example of how I can test my method or something similar to code would be helpful. What my method does is onclick of custom button it will check if the fields are blank if it is blank it will display a message to user that the field is required. Im really stuck on how to get started writing a unit test for this.
WebService static String validationsl(String objId){ Opportunity OppSubmit =[select id,Final_Space__c,Date_of_Possession__c,Store_Opening_Projected__c,recordtype.name from opportunity where id =:objId]; jsonResult finalResult=new jsonResult(); finalResult.Message = ''; finalResult.status = true; try{ if(OppSubmit.recordtype.name=='Permanent'){ if(OppSubmit.Final_Space__c == null){ getJoinText('"Final Space" is required', finalResult); } if(OppSubmit.Date_of_Possession__c == null){ getJoinText('"Date of Possession (Mutually Agreed)." is required', finalResult); } if(OppSubmit.Store_Opening_Projected__c == null){ getJoinText('"Store Opening (Projected)." is required', finalResult); } } return JSON.serialize(finalResult); }catch(exception ex){ System.debug('An Exception occured submitting deal for approval :'+ex.getTypeName()+'\t:'+ex.getMessage()); finalResult.Message +='\n'+ ex.getTypeName()+' : '+ex.getMessage(); return JSON.serialize(finalResult); } }
If you look at the class logic
When the opportunity with recordtype = "Permanent" get inserted or updated you wanted to get the final result based on the following values
Final_Space__c
Date_of_Possession__c
Store_Opening_Projected__c
In the Unit test you just have to build the data accordinlgy and call your method to test, making sure your test data help passing all the lines in the class that you expected it should go through.
Since your method is returning a String, making sure the result you get in the end match what you expected from the class.
I hope this clarfies
All Answers
System.assetEquals is sometimes not important, you can still live without it but that is not a best practise and also without that you can't guarantee your code work as expected even though it pass without it. So I will always recommend.
If you look at the class logic
When the opportunity with recordtype = "Permanent" get inserted or updated you wanted to get the final result based on the following values
Final_Space__c
Date_of_Possession__c
Store_Opening_Projected__c
In the Unit test you just have to build the data accordinlgy and call your method to test, making sure your test data help passing all the lines in the class that you expected it should go through.
Since your method is returning a String, making sure the result you get in the end match what you expected from the class.
I hope this clarfies