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
Mahesh Babu 187Mahesh Babu 187 

Test Class Getting No Code Coverage

I have written a Apex class which is getting called in a trigger. Now I have written a test class for the Apex class. But my code coverage is 0%.

APEX CLASS

public class ChangeOwnerClass{

    public static void changeOwner(Quote quote){
        
        Opportunity opp = [Select ID, CreatedById, OwnerId from Opportunity where Id =: quote.OpportunityId];
        System.debug(opp.OwnerId);
        if(opp.OwnerId != null) {}
            User user = [Select Id,ProfileId from User where Id=: opp.OwnerId];
            System.debug(user.Id);
            if(user!= null && user.ProfileId != null) {
                Profile profile = [Select name from Profile where Id =: user.ProfileId ];
                System.debug(profile.name);
                if(profile !=null && profile.name != null && profile.name == 'TriLink Sales & Service') {
                    quote.ownerId = opp.CreatedById;
                }            
            }
        }
    }

TRIGGER

trigger ChangeOwner on Quote (before insert) {
    
        for(Quote q: Trigger.new)
        {
            ChangeOwnerClass.changeOwner(q);
        }        
    }

TEST CLASS

@isTest
public class ChangeOwnerTest {

    @isTest static void changeOwnerTest(){
        Opportunity opp = new Opportunity();
        opp.Name = 'Test';
        opp.LeadSource = 'Test';
        opp.StageName = 'Identification';
        opp.CloseDate = Date.newInstance(2020, 12, 14);
        opp.OwnerId = '0056A00000264YcQAI';
        insert opp;
        if(opp.OwnerId != null) {}
        User user = [Select Id,ProfileId from User where Id=: opp.OwnerId];
        if(user!= null && user.ProfileId != null){
            Profile profile = [Select name from Profile where Id =: user.ProfileId ];
            if(profile !=null && profile.name != null && profile.name == 'TriLink Sales & Service'){
                Quote q = new Quote();
                q.Id = opp.Id;
                q.ownerId = opp.CreatedById;
                insert q;
            }
        }
    }
}

Please help me to modify it to get the code coverage.
Best Answer chosen by Mahesh Babu 187
Kritika RajKritika Raj
When the test class runs it will run in the context of current user. Now Lets say your profile is System Administrator and you execute this test class , then the Owner of the Opportunity will be you . Here , before inserting the Quote you are checking whether the Profile = 'TriLink Sales & Service' , which will be false because the profile returned will be 'System Admin' and hence no Quote will be inserted and your trigger will not fire. Create a Test User with the 'TriLink Sales & Service'  profile and then run the test class in context of that user using System.runAs(Test User)
@isTest
public class ChangeOwnerTest {


@isTest static void changeOwnerTest() {

Profile p = [SELECT Id,Name FROM Profile Where Name = 'TriLink Sales & Service'];

 User u = new User();
u.ProfileId = p.Id;
// populate the required field
insert u;

System.runAs(u) {

   //all your logic of Test Method that you mentioned above
}

}
}