You need to sign in to do that
Don't have an account?
smita bhargava
test class for creating new opportunity
Hello
I am new to writing test class. I need help in writing test class for the following code.
scenairo: when I create a new account from UI, a nee opportunity record gets created under the account.
Thanks
smita
I am new to writing test class. I need help in writing test class for the following code.
scenairo: when I create a new account from UI, a nee opportunity record gets created under the account.
public class CreateNewOpportunity { public List<Opportunity> oppList=new List<Opportunity>(); public void CreateOpportunity(List<Account> accList) { for(Account a:accList) { Opportunity o = new Opportunity(); o.AccountID=a.ID; o.Name=a.Name; o.CloseDate=System.Today().addDays(3); o.StageName='Prospecting'; oppList.add(o); } if (oppList.size() > 0) Database.Insert(oppList); } } trigger trg_createnewoppor on Account (after Insert,after update) { if ((Trigger.isInsert) || (Trigger.IsBefore)) { CreateNewOpportunity o = new CreateNewOpportunity(); o.CreateOpportunity(Trigger.New); } }
Thanks
smita
All Answers
You can use following test class for you trigger and handler.
Copy this code and create new class. Then run this test class and check the code coverage.
Please mark this answer as SOLVED and BEST ANSWER if it helps you.
Regards,
Yogesh More
Salesforce consultant || Salesforce Developer
more.yogesh422@gmail.com || Skype:-yogesh.more44
The reason for writing test classes is not only to get code coverage but also to check whether we are getting the expected outcome. The previous answers may provide 100 percent code coverage but no one tested the outcome. Below is the code. Use this you can see 100percent code coverage in you class and in trigger.
Please mark it as the best answer if it helps.
Yuvaraj
I tested your code, working fine and getting 100% code coverage. I have a doubt.
From developer point of view how do we know that this test class(CreateNewOpportunitytest) is asscoiated with the apex class (CreateNewOpportunity)?
Because in test class we are not calling any method of apex class (CreateNewOpportunity).
I hope I am clear.
Thanks
smita
This question is very confused for new developer, but I'll tell you how it's work for class step by step.
1. When you create a Test class, it go and check the trigger.
2. When trigger is fire then trigger checked that how many classes called in that trigger dml.
3. After that all of the classes run for get coverage.
I think this thing never tell you anyone, if you don't ask this question.
I'm also a fresher in this technology.
Please mark this answer as SOLVED and BEST ANSWER if it helps you, I was also give you a simple test class above.
Regards,
Sitanshu Tripathi
https://sitanshusfdc.blogspot.in/
I checked both ur codes but right when I run the test class now I am getting error as "Method defined as testMethod do not support webservice
callouts'".
can u pls le tme know how to take care of this?
thanks
smita
I am getting the code coverage as 100% for apex class and trigger, but I fail to understand why the test class is failing and it shows the error as mentioned previously..
But I tried just now in my dev org and I'm not facing any error. It maybe some your org issue.
Will you please try in fresh org ?