You need to sign in to do that
Don't have an account?
Gina Garcia
Test Class Coverage is only 25%, please help.
Hello guys,
Below are parts of my apex class and test class.
This method is part of apex class that is not covered.
Apex method class :
public List<Opportunity> getAccountOppList()
{
accountOppList = [SELECT id, Name, Donor_Name__c, Donor_Name_Id__c, CloseDate, Campaign_Fund__c, Amount, Type, Parent_Campaign__c, Gift_Method__c
FROM Opportunity WHERE (ForecastCategoryName = 'Closed') AND (Account.id = :account.id OR Soft_Credit_Account__r.id = :account.id
OR Matched_Donor_Account__r.id = :account.id) ORDER BY Closedate DESC];
return accountOppList;
}
Part of Test class :
List<Opportunity> accountOppList = New List<Opportunity>();
accountOppList = [SELECT id, Name, Donor_Name__c, Donor_Name_Id__c, CloseDate, Campaign_Fund__c, Amount, Type, Parent_Campaign__c, Gift_Method__c
FROM Opportunity WHERE (ForecastCategoryName = 'Closed') AND (Account.id = :accid OR Soft_Credit_Account__r.id = :accid
OR Matched_Donor_Account__r.id = :accid) ORDER BY Closedate DESC];
System.AssertNotEquals(accountOppList,null);
Please let me know what do I need to add in the test class to increase the code coverage.
Many thanks,
Gina
Below are parts of my apex class and test class.
This method is part of apex class that is not covered.
Apex method class :
public List<Opportunity> getAccountOppList()
{
accountOppList = [SELECT id, Name, Donor_Name__c, Donor_Name_Id__c, CloseDate, Campaign_Fund__c, Amount, Type, Parent_Campaign__c, Gift_Method__c
FROM Opportunity WHERE (ForecastCategoryName = 'Closed') AND (Account.id = :account.id OR Soft_Credit_Account__r.id = :account.id
OR Matched_Donor_Account__r.id = :account.id) ORDER BY Closedate DESC];
return accountOppList;
}
Part of Test class :
List<Opportunity> accountOppList = New List<Opportunity>();
accountOppList = [SELECT id, Name, Donor_Name__c, Donor_Name_Id__c, CloseDate, Campaign_Fund__c, Amount, Type, Parent_Campaign__c, Gift_Method__c
FROM Opportunity WHERE (ForecastCategoryName = 'Closed') AND (Account.id = :accid OR Soft_Credit_Account__r.id = :accid
OR Matched_Donor_Account__r.id = :accid) ORDER BY Closedate DESC];
System.AssertNotEquals(accountOppList,null);
Please let me know what do I need to add in the test class to increase the code coverage.
Many thanks,
Gina
As a best practice, you should create your own data in your test classes and assert expected behavior. All the code you provided does is mimic the same logic as the original class - which doesn't test anything and ideally doesn't return anything unless you've already inserted data in your test class (or used seeAllData = true - which isn't recommended).
Ideally, you'd start off with something like this:
I'd also suggest going through the Trailhead module on test classes: https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro
Instead of calling query again in test class you have to set up or insert data for Opportunity fulfilling all conditions which you have applied in WHERE clause of query and then just call the function in testclass to increase your code coverage.
Sample Code for test class:
@isTest
private class testClass {
@testsetup
static void setUpdata() {
Account account = new Account();
insert account;
Opportunity testOpportunity = new Opportunity();
testOpportunity.Soft_Credit_Account__r.id = account.id;
insert testOppurtunity;
}
static void testMethod() {
List<Oppurtunity> oppList = ClassName.getAccountOppList();
System.assert(true, oppList.Size()>0);
}
}