You need to sign in to do that
Don't have an account?
Eric Blaxton
Help to increase test coverage
Hi and thanks in advance.
I have several trigger and a several tests written. The code coverage is good, 77% and higher per test, but I want to get them higher. The tests don't cover my .addError code. the 2 lines not covered are in bold.
My trigger:
trigger checkAccountCreditStatusisRejected on Opportunity (before insert) {
//Create list to store data in
list<id>AccountidLst=new list<id>();
for(Opportunity opp : System.trigger.new)
{ // store all Opportunity Account Id's
AccountidLst.add(opp.AccountId);
}
// Create map that stores the id and credit status of Active Opportunity Account
map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where id In : AccountidLst]);
for (Opportunity opp : System.trigger.new) {
if(accountMap.containskey(opp.AccountId))
{
if (accountMap.get(opp.AccountId).Credit_Status__c == 'Rejected')
{
opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');
}// end if
}
}//end for
}// end trigger
My test:
@isTest
public class testCheckAccountCreditStatusisRejected{
static testMethod void checkAccountCreditStatusisRejected () {
// create mock user
User mockUser = new User(alias = 'newUser', email='newuser@pbsnow.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US', localesidkey='en_US', profileid = UserInfo.getProfileId(),
timezonesidkey='America/Los_Angeles', CommunityNickname='test1', username='testAddDefaultTeam@pbsnow.com');
insert mockUser;
//Define list
list<id>AccountidLst=new list<id>();
//create Account and Opportunity to add default team member
Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749' );
insert acc;
// store all Opportunity Account Id's
AccountidLst.add('001J000001DYN5I');
// Create map that stores the id and credit status of Active Opportunity Account
map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where Name = 'testAccount']);
Opportunity opp = new Opportunity( Name = 'testOpportunity', Account = acc,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
OwnerId = mockUser.id);
try{
insert opp;
if(accountMap.containskey('001J000001DYN5I'))
{
if (accountMap.get('001J000001DYN5I').Credit_Status__c == 'Rejected')
{
opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');
}// end if
}
}//end try
catch (ListException e) {
}
}
}// end public class
I have several trigger and a several tests written. The code coverage is good, 77% and higher per test, but I want to get them higher. The tests don't cover my .addError code. the 2 lines not covered are in bold.
My trigger:
trigger checkAccountCreditStatusisRejected on Opportunity (before insert) {
//Create list to store data in
list<id>AccountidLst=new list<id>();
for(Opportunity opp : System.trigger.new)
{ // store all Opportunity Account Id's
AccountidLst.add(opp.AccountId);
}
// Create map that stores the id and credit status of Active Opportunity Account
map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where id In : AccountidLst]);
for (Opportunity opp : System.trigger.new) {
if(accountMap.containskey(opp.AccountId))
{
if (accountMap.get(opp.AccountId).Credit_Status__c == 'Rejected')
{
opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');
}// end if
}
}//end for
}// end trigger
My test:
@isTest
public class testCheckAccountCreditStatusisRejected{
static testMethod void checkAccountCreditStatusisRejected () {
// create mock user
User mockUser = new User(alias = 'newUser', email='newuser@pbsnow.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US', localesidkey='en_US', profileid = UserInfo.getProfileId(),
timezonesidkey='America/Los_Angeles', CommunityNickname='test1', username='testAddDefaultTeam@pbsnow.com');
insert mockUser;
//Define list
list<id>AccountidLst=new list<id>();
//create Account and Opportunity to add default team member
Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749' );
insert acc;
// store all Opportunity Account Id's
AccountidLst.add('001J000001DYN5I');
// Create map that stores the id and credit status of Active Opportunity Account
map<id,account>accountMap=new map<id,account>([select id,Credit_Status__c from account where Name = 'testAccount']);
Opportunity opp = new Opportunity( Name = 'testOpportunity', Account = acc,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
OwnerId = mockUser.id);
try{
insert opp;
if(accountMap.containskey('001J000001DYN5I'))
{
if (accountMap.get('001J000001DYN5I').Credit_Status__c == 'Rejected')
{
opp.addError('WARNING: New Opportunity cannot be created when Account Credit Status = Rejected');
}// end if
}
}//end try
catch (ListException e) {
}
}
}// end public class
1. //create Account and Opportunity to add default team member
Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749', Credit_Status__c = 'Rejected' );
insert acc;
2. Opportunity opp = new Opportunity( Name = 'testOpportunity', AccountID = acc.id,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
OwnerId = mockUser.id);
All Answers
...
1. //create Account and Opportunity to add default team member
Account acc = new Account ( Name = 'testAccount', BillingStreet = '123 Main Street', BillingCity = 'Dallas' ,
BILLINGCOUNTRYCODE='US', BillingStateCode = 'TX', Industry = 'Healthcare', Company__c = 'Pinnacle Business Systems', BillingPostalCode = '75749', Credit_Status__c = 'Rejected' );
insert acc;
2. Opportunity opp = new Opportunity( Name = 'testOpportunity', AccountID = acc.id,/* ToBeSync__c = false,*/ StageName ='Prospecting', Is_this_a_Maintenance_Renewal_Op__c = 'No',Description = 'Test', CloseDate = Date.today(),
OwnerId = mockUser.id);