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
Dev Rana 16Dev Rana 16 

Test class code coverage is 69

Hello every one I have one problem I write my test class and it gives me 69% of code coverage, and i need 75% how can i get that. please help me

this is my class
trigger WishChildDuplicatePreventer on Opportunity (before insert,before update) {
     
     Map<String, Opportunity> OpportunityMap = new Map<String,Opportunity>();
     
     for (Opportunity Opportunity : System.Trigger.new)
           {
                if ((Opportunity.Form_No__c!= null) && 
                       (System.Trigger.isInsert || (Opportunity.Form_No__c!= System.Trigger.oldMap.get
                           (Opportunity.Id).Form_No__c)))
                  { 
                          
                    // This is check that new wish child isn't also a duplicate
                      if (OpportunityMap.containsKey(Opportunity.Form_No__c))
                       {
                         Opportunity.Form_No__c.addError('Another new Wish child has the' + 'same wish child.');
                       } 
                  else {
                          OpportunityMap.put(Opportunity.Form_No__c, Opportunity);
                       }
                } 
     }
     
          for (Opportunity Opportunity: [SELECT Form_No__c FROM Opportunity WHERE Form_No__c IN :OpportunityMap.KeySet()])
           {
               Opportunity newOpportunity = OpportunityMap.get(Opportunity.Form_No__c);
               newOpportunity.Form_No__c.addError('A Wish child with this Form No already exists ');
           }

}
And this is my Test class
@isTest
public class TestsWishChildDuplicatePreventer{
       static testMethod void testLeadDupPreventer() {
           
          Opportunity o = new Opportunity();
           o.Name = 'Raj';
           o.Form_No__c = '0001';
           o.Father_Name__c= 'Abc';
           o.Mobile__c='9898458211';
           o.StageName ='ToHave';
           o.CloseDate =date.parse('10/10/15');
           insert o;
           
            division__c  d = new division__c();
            d.name = 'Bangalore';
            d.landline__c = '+919234567890';
            d.mobile__c = '+919234567890';
            d.email__c= 'vivek@gmail.com';
            d.City__c = 'Hyd';
            d.state__c = 'Karnataka';
            d.pin_code__c = '560037';
            d.street__c = 'Whitefield';
            d.country__c = 'India';
            insert d;
            
            account a = new account();
               a.name = 'NIMS';
               a.phone = '+919234567890';
               a.email__c = 'vivek@gmail.com';
               a.BillingStreet = 'Marathahalli';
               a.BillingCity = 'Bangalore';
               a.BillingState = 'Karnataka';
               a.BillingPostalCode = '560037';
               a.BillingCountry = 'India';
               a.website__c= 'www.lntinfotech.com';    
               a.division__c= d.ID;
               insert a;
               
                o.name='Pratik';
                o.Form_No__c = '0002';
                o.Father_Name__c= 'XYZ';
                o.Mobile__c='9898458312';
                
                update o;
                
                 Map<String, Opportunity > OpportunityMap = new Map<String, Opportunity>();
                  for (Opportunity Opportunity: [SELECT Form_No__c FROM Opportunity WHERE Form_No__c IN :OpportunityMap.KeySet()])
                  {
                       Opportunity newOpportunity = OpportunityMap.get(Opportunity.Form_No__c);
                       newOpportunity.Form_No__c.addError('A Wish child with this Form No already exists ');
                  }
                
                delete o;
               
       }

}

Thanks in advance.
 
EmilienGuichardEmilienGuichard
Hello,
You can open your class in the developper console and clock on the button Code Coverage, this will display in red the lines that are not covered by you test class.
Dev Rana 16Dev Rana 16
Thanks EmilienGuichard
    I get That line, please give me some suggation how to recover that line in test class.?
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below test class
@isTest
public class TestsWishChildDuplicatePreventer
{
    static testMethod void testLeadDupPreventer() 
	{
	

		division__c  d = new division__c();
		d.name = 'Bangalore';
		d.landline__c = '+919234567890';
		d.mobile__c = '+919234567890';
		d.email__c= 'vivek@gmail.com';
		d.City__c = 'Hyd';
		d.state__c = 'Karnataka';
		d.pin_code__c = '560037';
		d.street__c = 'Whitefield';
		d.country__c = 'India';
		insert d;

		account a = new account();
		a.name = 'NIMS';
		a.phone = '+919234567890';
		a.email__c = 'vivek@gmail.com';
		a.BillingStreet = 'Marathahalli';
		a.BillingCity = 'Bangalore';
		a.BillingState = 'Karnataka';
		a.BillingPostalCode = '560037';
		a.BillingCountry = 'India';
		a.website__c= 'www.lntinfotech.com';    
		a.division__c= d.ID;
		insert a;


		Opportunity o = new Opportunity();
		o.Name = 'Raj';
		o.Form_No__c = '0001';
		o.Father_Name__c= 'Abc';
		o.Mobile__c='9898458211';
		o.StageName ='ToHave';
		o.CloseDate =date.parse('10/10/15');
		insert o;


		Opportunity opp = new Opportunity();
		opp.Name = 'Raj';
		opp.Form_No__c = '0001';
		opp.Father_Name__c= 'Abc';
		opp.Mobile__c='9898458211';
		opp.StageName ='ToHave';
		opp.CloseDate =date.parse('10/10/15');
		
		try
		{
			insert opp;
		}
		Catch(Exception ee)
		{
			
		}
    }
}

Let us know if this will help you

 
Dev Rana 16Dev Rana 16
Hey Amit Chaudhary thanks for your help as per your suggation i change my code and i get 83% code coverage. thanks a lot.