You need to sign in to do that
Don't have an account?
Mahesh 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.
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 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)