You need to sign in to do that
Don't have an account?
Sandesh Vishwakarma 9
Can anyone please help me with the test class for this apex code. Thanks in advance
public class OpportunityFetchField {
@AuraEnabled
public static list<Opportunity> insertOpportunity(list<Opportunity> oplist, ID ContactId){
Contact rec = [SELECT ID ,Name from Contact where ID =: ContactId];
system.debug('rec '+rec);
system.debug('oplist '+oplist);
for(Opportunity op :oplist){
op.ContactId = rec.Id;
op.StageName='Needs Analysis';
}
System.debug('ContactId' + ContactId);
insert oplist;
system.debug('oplist '+oplist);
return oplist;
}
}
@AuraEnabled
public static list<Opportunity> insertOpportunity(list<Opportunity> oplist, ID ContactId){
Contact rec = [SELECT ID ,Name from Contact where ID =: ContactId];
system.debug('rec '+rec);
system.debug('oplist '+oplist);
for(Opportunity op :oplist){
op.ContactId = rec.Id;
op.StageName='Needs Analysis';
}
System.debug('ContactId' + ContactId);
insert oplist;
system.debug('oplist '+oplist);
return oplist;
}
}
The test class can be as below. It covers 100% of the code. We have to use System.AssertEquals to check if the opportunity is inserted succesfully or not.
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Try Below Test Class 100%Coverage Please Mark It As best Answer If It Helps
Thank You!
The test class can be as below. It covers 100% of the code. We have to use System.AssertEquals to check if the opportunity is inserted succesfully or not.
If this solution helps, Please mark it as best answer.
Thanks,
Try this test class (100%Coverage)
@isTest
Private class TestClassForOpportunityFetchField {
@Testsetup
public static void setUp(){
Account accInstance = new Account();
accInstance.Name = 'acctest';
insert accInstance;
Contact conInstance = new Contact();
conInstance.LastName = 'contest';
insert conInstance;
}
@isTest
public static void opportunityFetchMethod(){
Test.startTest();
Contact con = [SELECT id,Name from Contact];
ID contactId= con.Id;
Account acc = [SELECT id From Account];
List<Opportunity>oppList = new List<Opportunity>();
Opportunity oppInstance = new Opportunity();
oppInstance.Name = 'opptest';
oppInstance.CloseDate = system.today();
oppInstance.StageName = 'Closed Won';
oppInstance.AccountId = acc.Id;
oppList.add(oppInstance);
List<Opportunity>returnedOppList= OpportunityFetchField.insertOpportunity(oppList,contactId);
system.debug(returnedOppList);
Test.stopTest();
system.assertEquals(1,returnedOppList.size());
system.assertEquals('Needs Analysis',returnedOppList[0].StageName);
system.assertEquals(contactId,returnedOppList[0].ContactId);
}
}
If this solution helps, Please mark it as best answer.
Thanks