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
affuaffu 

Test coverage

Hi,

I have written a Trigger and Testclass. Im getting 59% coverage plz refer the code

* Urgent

 

Trigger -

trigger ePeople_ApartmentBlock on Opportunity (after update) {
    Map<Id,Id> opptyAppMap1 = new Map<Id,Id>();
    Map<Id,Id> opptyAppMap2 = new Map<Id,Id>();
    Map<Id,Id> opptyAppMap3 = new Map<Id,Id>();
    Map<Id,Id> opptyAppMap4 = new Map<Id,Id>();
    List<Apartment__c> testApp1 = new List<Apartment__c>();
    List<Apartment__c> testApp2 = new List<Apartment__c>();
    List<Apartment__c> testApp3 = new List<Apartment__c>();
    List<Apartment__c> testApp4 = new List<Apartment__c>();
    List<Apartment__c> testApp5 = new List<Apartment__c>();
    Set<Id> oldAppSetId = new Set<Id>();
    Set<Id> delOppAppId = new Set<Id>();
    for(Opportunity opp:Trigger.new){
        if(opp.Apartment__c != null && (opp.StageName == 'Site Visit' || opp.StageName == 'Closed Lost')){
            opptyAppMap1.put(opp.Apartment__c, opp.Id);
        }
        else if(opp.Apartment__c != null && opp.StageName == 'Booking'){
            opptyAppMap2.put(opp.Apartment__c, opp.Id);
        }
        else if(opp.Apartment__c != null && (opp.StageName == 'Blocking' || opp.StageName == 'Negotiation/Review')){
            opptyAppMap3.put(opp.Apartment__c, opp.Id);
        }
        else if(opp.Apartment__c != null && opp.StageName == 'Agreement Done'){
            opptyAppMap4.put(opp.Apartment__c, opp.Id);  
        }
        else if(System.Trigger.oldMap.get(opp.Id).Apartment__c != null && opp.Apartment__c != System.Trigger.oldMap.get(opp.Id).Apartment__c)
        {
            Opportunity oldOpp =  Trigger.oldMap.get(opp.Id);
            oldAppSetId.add(oldOpp.Apartment__c);
        }
           
    for(Apartment__c c:[select Id, Status__c from Apartment__c where Id IN:opptyAppMap1.keySet()]){
        if(c.Status__c != 'Available'){
            c.Status__c = 'Available';
        }   
        testApp1.add(c);
    }
    update testApp1;
    for(Apartment__c c:[select Id, Status__c from Apartment__c where Id IN:opptyAppMap2.keySet()]){
        if(c.Status__c != 'Booked'){
            c.Status__c = 'Booked';
        }   
        testApp2.add(c);
    }
    update testApp2;
    for(Apartment__c c:[select Id, Status__c from Apartment__c where Id IN:opptyAppMap3.keySet()]){
        if(c.Status__c != 'Blocked'){
            c.Status__c = 'Blocked';
        }    
        testApp3.add(c);
    }
    update testApp3;
    for(Apartment__c c:[select Id, Status__c from Apartment__c where Id IN:opptyAppMap4.keySet()]){
        if(c.Status__c != 'Sold'){
            c.Status__c = 'Sold';
        }    
        testApp4.add(c);
    }
     update testApp4;
     
    for(Apartment__c c : [select Id, Status__c from Apartment__c where Id IN:oldAppSetId]){
        if(c.Status__c != 'Available'){
            c.Status__c = 'Available';
        }    
        testApp5.add(c);
    }
     update testApp5;
    
    }
    for(Opportunity opp:Trigger.old){
     if(Trigger.isDelete){ 
       Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
       system.debug('OpportunityMap'+oldOpp);
       delOppAppId.add(oldOpp.Apartment__c);
     } 
    }
}

 Testclass -

@isTest
private class ePeople_AptOpportunityTest{
    static testMethod void AptTest()
    {
      Account a = new Account(name='Ramesh');
      insert a;
      Project__c pr = new Project__c(Name='VV');
      insert pr;
      Block__c b = new Block__c(Name='Block 1',Project__c=pr.id);
      insert b;
      Apartment__c c = new Apartment__c(Name='A1',Block__c=b.id, Status__c = 'Blocked');
      insert c;
      Apartment__c c1 = new Apartment__c(Name='A1',Block__c=b.id, Status__c = 'Booked');
      insert c1;
      Opportunity o=new Opportunity(Name='ram test',AccountId=a.Id, CloseDate=Date.today(),StageName='Blocking', Apartment__c = c.id);
      insert o; 
      a.Name='Ramesh';
      update a;
      o.StageName ='Booking' ;
      o.StageName = 'Blocking';
      c.Status__c = 'Available';
      update c;
      update o;
    }
}q

 

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference of test method:
@isTest
private class ePeople_AptOpportunityTest{
static testMethod void AptTest()
{
Account a = new Account(name='abc');
insert a;
Project__c pr = new Project__c(Name='VV');
insert pr;
Block__c b = new Block__c(Name='Block 1',Project__c=pr.id);
insert b;
Apartment__c c = new Apartment__c(Name='A1', Block__c=b.id,Status__c = 'Blocked');
insert c;
Apartment__c c1 = new Apartment__c(Name='A1', Block__c=b.id,Status__c = 'Booked');
insert c1;
Opportunity o=new Opportunity(Name='ram test',AccountId=a.Id, CloseDate=Date.today(),StageName='Blocking', Apartment__c = c.id);
insert o;
a.Name='xyz';
update a;
o.StageName ='Booking' ;
o.StageName = 'Blocking';
c.Status__c = 'Available';
update c;
update o;
o.StageName ='Site Visit';
update o;
o.StageName ='Booking';
update o;
o.StageName ='Agreement Done';
update o;


}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,


Try the below code as reference of test method:
@isTest
private class ePeople_AptOpportunityTest{
static testMethod void AptTest()
{
Account a = new Account(name='abc');
insert a;
Project__c pr = new Project__c(Name='VV');
insert pr;
Block__c b = new Block__c(Name='Block 1',Project__c=pr.id);
insert b;
Apartment__c c = new Apartment__c(Name='A1', Block__c=b.id,Status__c = 'Blocked');
insert c;
Apartment__c c1 = new Apartment__c(Name='A1', Block__c=b.id,Status__c = 'Booked');
insert c1;
Opportunity o=new Opportunity(Name='ram test',AccountId=a.Id, CloseDate=Date.today(),StageName='Blocking', Apartment__c = c.id);
insert o;
a.Name='xyz';
update a;
o.StageName ='Booking' ;
o.StageName = 'Blocking';
c.Status__c = 'Available';
update c;
update o;
o.StageName ='Site Visit';
update o;
o.StageName ='Booking';
update o;
o.StageName ='Agreement Done';
update o;


}
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

This was selected as the best answer
Sam27Sam27

ofcourse it doesn't look like you are fulfilling different conditions you provided in your trigger.........

 

in test method you are updating only one Opportunity which certainly have one Apartment__c reference.....I guess you need to alter that Apartment__c and then update it and then the opportunity againg and then it would be matched to the conditions in trigger.

 

After each update....change the StageName and update it again.

 

hope it helps............

affuaffu

Its working...Thank you very much Navatar. You are Awesome.....

affuaffu

Thank u Jain

affuaffu

Hai Jain, I have one more Testclass troubling me. If u dont mind can you solve this too...

 

Trigger -

trigger AptAfterDelete on Opportunity (after delete){
    List<Apartment__c> testApp1 = new List<Apartment__c>();
    Set<Id> delOppAppId = new Set<Id>();
    for(Opportunity opp:Trigger.old){
        if(Trigger.isDelete){ 
            Opportunity oldOpp = Trigger.oldMap.get(opp.Id);
            system.debug('OpportunityMap'+oldOpp.Id);
            delOppAppId.add(oldOpp.Apartment__c);
        for(Apartment__c c : [select Id, Status__c from Apartment__c where Id IN:delOppAppId ]){
            system.debug('Apartmentvvvvv'+c);
        if(c.Status__c != 'Available'){
            c.Status__c = 'Available';
        } 
        testApp1.add(c);   
    }  
    update testApp1; 
        }
    }
}