function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion 

Unable to get coverage for SOQL query in Apex trigger

I have implemnted a trigger on custom object which hold the opportunity Id and Opportunity Name. I need to access  a custom field from oportunity object for which I used a SOQL query but not able to get coverage for that . so need help on this .

My Apex  Trigger :

trigger SendImplementaionStageOnPortal on Implementation__c (after update) { // TEST CLASS TestSendImplementationStageOnPortal
         for (Implementation__c imp : {
                // make the asynchronous web service callout
                HttpResponse r= new Httpresponse();
                 SendDataToPortal s = new SendDataToPortal();
                 String ImpStage = NULL;

             /******************************** REPLACE SALESFORCE ADMIN UserID ON PRODUCTION ***********************************/
             if(imp.Opportunity_ID__c!=null && Trigger.OldMap.get(imp.Id).Stage__c!= imp.Stage__c && imp.Stage__c=='Complete'){
                 //Query Opportunity
                List<Opportunity>lstOppts = [select Subscriber_id__c From Opportunity WHERE  Id=:imp.Opportunity_ID__c limit 1];
                        System.debug('Subscriber_id__c-MY: '+ lstOppts[0].Subscriber_id__c);
                      // List<Account>lstAccounts = [select id,Name,(select id,FirstName,LastName FROM Contacts ) From Account WHERE Id =:lstOppts[0].id];
                 ImpStage = string.valueof(imp.Stage__c);
                  //r = s.sendImplementaionData(lstOppts[0].Subscriber_id__c,imp.Stage__c); 
                   r = s.sendImplementaionData(lstOppts[0].Subscriber_id__c,'complete'); 


My Apex Test Class :

public class TestSendImplementationStageOnPortal {
    static testMethod void sendImplementaionDataTest() {  
        Account a = new Account(Name = 'SDToPortalImpTest',Type = 'Customer',;
        insert a;
        Contact newContact = new Contact(Lastname='testLastName', AccountId = a.Id ,Email='',phone ='56465464' , LeadSource='Inbound', Lead_Source_Detail__c = 'Website');
        insert newContact;
           String opportunityName = 'SDToPortalImpTest';
        Opportunity o1 = new Opportunity(AccountId=a.Id, name='OPP test1',Type ='New Business',Susbcription_Status__c = 'Active', Subscriber_ID__c =3651,Client_ID__c=1258, StageName='Closed Won',CloseDate = );
        insert o1;
        OpportunityContactRole ocr1 = new OpportunityContactRole(OpportunityId = o1.Id, ContactId = newContact.Id, Role='Decision Maker',Isprimary=true );
        insert ocr1;
         Implementation__c imp = new Implementation__c(name='IMP test1',Opportunity_Name__c=o1.Id,Stage__c='New');
         insert imp;
        List<Opportunity> myList = new List<Opportunity>(); // Define a new list
          Opportunity opp = new Opportunity(Subscriber_id__c=3651,Id= imp.Opportunity_ID__c);
       //List<Opportunity>lstOppts = [select Subscriber_id__c From Opportunity WHERE  Id='0061F00000AOBTkQAP'];

        imp.Stage__c ='Complete';
        update imp;
       List<Opportunity>lstOppts = [select Subscriber_id__c From Opportunity WHERE  Id=:imp.Opportunity_ID__c limit 1];
         System.assert(lstOppts != null);


I am not able to find coverage for this :


    List<Opportunity>lstOppts = [select Subscriber_id__c From Opportunity WHERE  Id=:imp.Opportunity_ID__c limit 1];

Andrew GAndrew G
i'm curious to why you do this in your test class:
        imp.Stage__c ='Complete';
        update imp;
By looking at this , it seems you are rewriting the Id and the Oppty lookup field.  Why?
Surely all you need to do is:
        imp.Stage__c ='Complete';
        update imp;
It would appear that by re-writing the Opp ID in the Implementation__c  record, it isn't going to be returned as the code has no reference to what ever opportunity is 0061F00000AOBTkQAP

As a side note, you have an SQL query inside a for loop - but practice for bulkification.  And it appears that you are doing a callout without having a invoking a test Mock Interface.