0

I'm having issues with a Test class to provide code coverage to a Trigger I created. Here is the Trigger:

trigger UpdateOpportunity on Opportunity (before insert, before update){    

    Map<Id, Opportunity> recordTypeMap = new Map<Id, Opportunity>([SELECT  RecordType.Name FROM Opportunity WHERE ID IN :Trigger.new]);  

    for(Opportunity opp : Trigger.new){

      if(trigger.isInsert || trigger.isUpdate && recordTypeMap.get(opp.Id).RecordType.Name == 'SGI Opportunity' && opp.StageName != 'Lead 0%'){


             if(opp.Win_Reason__c == 'No Competition/Sole Source'){

                opp.Competitor_s__c = 'No Competitor/Sole Source'; 

            }

               if(opp.Unsolicited_Bid__c == 'Yes' && opp.Competitor_s__c == null){

                  opp.Competitor_s__c = 'No Competitor/Sole Source';

            } 

         }
    }
}

And here is the Test class.

@isTest
private class UpdateOpportunityTest {
    static testMethod void UpdateOpportunityTest() {      

        Id sgiOpportunityRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'SGI Opportunity'].Id;

        Opportunity opportunity = new Opportunity(
          Name = 'Test Opportunity', Opportunity_Type__c = 'Add-On',
            Product_Revenue_Type__c = '% of Completion', CloseDate = Date.today(), StageName = 'Lead 0%',
            CurrencyIsoCode = 'USD', Opportunity_Intergaph_Line_of_Business__c = 'Government', Solution__c = 'Border Security',
            OwnerId = UserInfo.getUserId(), RecordTypeId = sgiOpportunityRecordTypeId);

        Test.startTest();
        try {
          insert opportunity;
        }
        catch (DmlException ex) {

      System.assertEquals(StatusCode.FIELD_CUSTOM_VALIDATION_EXCEPTION, ex.getDmlType(0));
      System.assertEquals('Error', ex.getDmlMessage(0));
        }
        Test.stopTest();
    }

    static testMethod void UpdateOpportunityWinReasonUpdateTest() {

      Account account = new Account(Name = 'Test Account', Account_Intergraph_Line_of_Business__c = 'Government', Industry = 'Other',
                                    Country_Code__c = 'United States', Street_Address__c = '123 Test Drive', City__c = 'Testville',
                                    State_Province__c = 'AL', Zip_Postal_Code__c = '55555', Type='Suspect');
        insert account;       

        Id sgiOpportunityRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'SGI Opportunity'].Id;

        Contact contact = new Contact(AccountId = account.Id, FirstName = 'Test', LastName = 'User', Email = 'test@intergraph.com', Active__c = 'No', Contact_Status__c = 'Active');
        insert contact;

        Opportunity opportunity = new Opportunity(
          Name = 'Test Opportunity', AccountId = account.Id, Opportunity_Type__c = 'Add-On',
            Product_Revenue_Type__c = '% of Completion', CloseDate = Date.today(), StageName = 'Lead 0%',
            CurrencyIsoCode = 'USD', Opportunity_Intergaph_Line_of_Business__c = 'Government', Solution__c = 'Border Security',
            Win_Reason__c = 'Price', OwnerId = UserInfo.getUserId(), RecordTypeId = sgiOpportunityRecordTypeId);
        insert opportunity;




        OpportunityContactRole contactRole = new OpportunityContactRole(ContactId = contact.Id, OpportunityId = opportunity.Id);
        insert contactRole;



               Test.startTest();

                opportunity.Win_Reason__c = 'No Competition/Sole Source';
                opportunity.StageName = 'Qualification 10%';
                opportunity.Unsolicited_Bid__c ='Yes';
                update opportunity;

                Opportunity updatedOpportunity = [SELECT Competitor_s__c FROM Opportunity WHERE Id =: opportunity.Id];

                System.assertEquals('No Competitor/Sole Source', updatedOpportunity.Competitor_s__c);

                Test.stopTest();
    }

    static testMethod void UpdateQuoteOwnerTest() {

      Account account = new Account(Name = 'Test Account', Account_Intergraph_Line_of_Business__c = 'Government', Industry = 'Other',
                                    Country_Code__c = 'United States', Street_Address__c = '123 Test Drive', City__c = 'Testville',
                                    State_Province__c = 'AL', Zip_Postal_Code__c = '55555', Type='Suspect');
        insert account;       

        Id sgiOpportunityRecordTypeId = [SELECT Id FROM RecordType WHERE Name = 'SGI Opportunity'].Id;

        Contact contact = new Contact(AccountId = account.Id, FirstName = 'Test', LastName = 'User', Email = 'test@intergraph.com', Active__c = 'No', Contact_Status__c = 'Active');
        insert contact;

        Opportunity opportunity = new Opportunity(
          Name = 'Test Opportunity', AccountId = account.Id, Opportunity_Type__c = 'Add-On',
            Product_Revenue_Type__c = '% of Completion', CloseDate = Date.today(), StageName = 'Lead 0%',
            CurrencyIsoCode = 'USD', Opportunity_Intergaph_Line_of_Business__c = 'Government', Solution__c = 'Border Security',
            Win_Reason__c = 'Price', OwnerId = UserInfo.getUserId(), RecordTypeId = sgiOpportunityRecordTypeId);

        insert opportunity;



        OpportunityContactRole contactRole = new OpportunityContactRole(ContactId = contact.Id, OpportunityId = opportunity.Id);
        insert contactRole;

        Profile p = [SELECT Id, Name FROM Profile WHERE Name='System Administrator'];
        String orgId = UserInfo.getOrganizationId();
        String dateString = String.valueof(Datetime.now()).replace(' ','').replace(':','').replace('-','');
        Integer randomInt = Integer.valueOf(math.rint(math.random()*1000000));
        String uniqueName = orgId + dateString + randomInt;
        User u = new User(Alias ='test', Email='atest@bbandt.com', 
                EmailEncodingKey='UTF-8', LastName='Testing'+p.name, LanguageLocaleKey='en_US', 
                LocaleSidKey='en_US', ProfileId = p.Id, 
                TimeZoneSidKey='America/Los_Angeles', Username = uniqueName + '@intergraph' + orgId + '.org');

        insert u;

        List<BigMachines__Quote__c> quoteList = new List<BigMachines__Quote__c>();
        for(Integer j=0;j<20;j++){
        BigMachines__Quote__c quote = new BigMachines__Quote__c(Name = '2009-111'+j, BigMachines__Opportunity__c = opportunity.Id,
                                                    BigMachines__Is_Primary__c = false, ownerId = UserInfo.getUserId());
        quoteList.add(quote);
        insert quote;       
        }

               Test.startTest();

                opportunity.Win_Reason__c = 'No Competition/Sole Source';
                opportunity.StageName = 'Qualification 10%';
                opportunity.Unsolicited_Bid__c ='Yes';
                opportunity.OwnerId = u.Id;
                update opportunity;

                Opportunity updatedOpportunity = [SELECT OwnerId FROM Opportunity WHERE Id =: opportunity.Id];

                System.assertEquals(updatedOpportunity.OwnerId!=null,true);

                Test.stopTest();
    }
}

* We have Validation Rules that require an Account is associated with an Opportunity, which in turn requires me to insert a Contact and ContactRole.

** This test class had 100% code coverage until I made a field dependent on another field and/or that I added 'Active' as the default value for the Contact Status field.

*** Here is the error I'm getting:

System.AssertException: Assertion Failed: Expected: Error, Actual: No Active Contact Present. Add an active Contact.

We have a custom field 'Active' and 'Contact Status'. I've tried setting those fields equal to various different values and it still won't insert the opportunity.. Maybe I have things in the wrong places or inserting in the wrong order? I'm not sure but I'm out of options. Please ask any questions if the problem is unclear.

  • Did you said you've added two fields ? Active and Contact Status ? If yes on what object have you created them ? – Mihai Neagoe Jan 28 '15 at 19:20
  • The fields were already there.. I just made the dependent on each other. They are both on the Contact object. – user3708392 Jan 28 '15 at 19:21
  • Can you share the validation rule as well ? – Mihai Neagoe Jan 28 '15 at 19:29
  • But there's not a validation rule concerning an Active Contact. I have no idea where or why it's asking for an 'Active' contact other than I made it the default value on the Contact record. – user3708392 Jan 28 '15 at 19:34
  • 1
    Mihai Neagoe, thank you for your help! It was a trigger that was causing the issue! Not sure why it was saying it was a validation rule. – user3708392 Jan 29 '15 at 14:11

Your Answer

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

Browse other questions tagged or ask your own question.