0

I'm writing an apex test class for a trigger and it's showing zero errors, but not sure if the class is passing or not. any help would be appreciated. Thanks

Trigger:

trigger OppToUpdate on Opportunity (after update) {
    Map<Id,String> rfpResStatus = new Map<Id,String>();
    //Create a set collector for Account's associated to the opportunity
    Set<Id> associatedAccId = new Set<Id>();

    for(Opportunity opp: Trigger.new)
    {
        if(opp.RFP_Response_Status__c =='out of business' 
           || opp.RFP_Response_Status__c =='Do Not Call'
           || opp.RFP_Response_Status__c == 'Bad Contact Number'
           || opp.RFP_Response_Status__c == 'sub-contractor')
        {
            associatedAccId.add(opp.AccountId);
            rfpResStatus.put(opp.AccountId,opp.RFP_Response_Status__c); 
        }

    }
    Opportunity[] opps = [SELECT Id, AccountId 
                          FROM Opportunity 
                          WHERE AccountId IN :associatedAccId 
                          AND
                          RFP_Response_Status__c NOT IN ('Out of Business',
                          'Do NOT CALL','sub-contractor','Bad Contact Number') 
                          AND
                          StageName NOT IN ('Closed Won','Closed Lost')
                          AND Id NOT IN :Trigger.new];

    for(Opportunity opp: opps) 
    { 
        opp.RFP_Response_Status__c = rfpResStatus.get(opp.AccountId); 
        opp.StageName = 'Closed Lost';        
    }
    update opps;
}

Test Class for the above trigger is:

@isTest
public class Test_OppToUpdate {
    static testMethod void testOppToUpdate()
    {
        Account a = new Account();
        a.Name ='Neil Account';
        a.RecordTypeId = '012j0000000qS49';
        a.Account_Status__c = 'Active';
        Insert a;

        List<Opportunity> opp = new List<Opportunity>();
        for(Integer i=0; i>2; i++)
        {
            Opportunity o = new Opportunity();
            o.AccountId = a.Id;
            o.Name = 'oppo' + i;
            o.RFP_Response_Status__c='Round 2';
            o.StageName = 'Round 1';
            o.CloseDate = date.today();
            opp.add(o);
        }
        Insert opp;


        for(Integer x=0; x>1; x++)
        {
            Opportunity o = new Opportunity();
            o.RFP_Response_Status__c = 'Out of Business';
            update opp;
        }

        opp = [select Id, Name, AccountId, RFP_Response_Status__c, StageName, CloseDate
               From Opportunity];
       system.debug('Opp' + opp); 
    }
}
  • to know if a test class is 'passing' - use the system.assert(..) methods to verify expected results after querying the database of the changed Opportunity(s) – cropredy May 31 '16 at 19:44
0

You have your operators backwards in your loop. You're setting your index (i and x) to 0, then checking to see if they're greater than 2 or 1. This is false, and therefore the loop doesn't execute at all, so you get no coverage for your trigger. You just need to use the "less than" operator instead:

for(Integer i = 0; i < 2; i++) {

And

for(Integer x = 0; x < 1; x++) {

Also note that the loop with the x index is probably not what you intended: you're calling an update on a record that doesn't have an ID value, which will cause an error. Instead, consider re-using the values in opp:

insert opp;
update opp;
  • Sorry it took me awhile to get back, I just wanted to say thank you. – Templar Jun 4 '16 at 1:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.