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
Marco Pollastri 1Marco Pollastri 1 

test class 66% coverage

Hi, I have tested my trigger, but the coverage of the code reached 66% even if the test of the class was successfully, how can I reach 75%? test 1ithis was the class:

@isTest
private class ErrorMessagetestclass {
    
    Static testmethod void myTest(){
        Opportunity opp = new Opportunity ();
        opp.Name = 'Test';
        opp.StageName = 'Qualification';
        opp.CloseDate = Date.parse('12/10/2021');
        opp.Type = 'Supplier';
                insert opp;
}
    }
Thanks 
Michael MMichael M
Hi Marco, I am not able to see the trigger code, but triggers only need 1% coverage in order to move to production (only classes need 75% coverage)
CharuDuttCharuDutt
Hii Marco
Try Below Code
trigger Error_Opp on Opportunity (before insert) {
  for( Opportunity oppsupplier: trigger.new ){
       if(oppsupplier.Opp_Count__c > 1 && oppsupplier.type == 'Supplier'){
                 oppsupplier.addError('This is a Supplier Account, it can have only one Opportunity.');
       }
   }
}

Test Class

@isTest
private class ErrorMessagetestclass {
    
    Static testmethod void myTest(){
        list<Opportunity> lstOpp = new list<Opportunity>();
        for(Integer i=0;i<5;i++){
        Opportunity opp = new Opportunity ();
        opp.Name = 'Test Opp ' + i;
        opp.StageName = 'Qualification';
        opp.CloseDate = system.today();
        opp.Type = 'Supplier';
            lstOpp.Add(opp);
        }
                insert lstOpp;
        
       
    }
Please Mark It As Best Answer If It Helps
Thank You!

 
Marco Pollastri 1Marco Pollastri 1
Hi, thanks for your answer, the problem was that I didn't insert the Account ID.
This was the code that gave me 100% coverage:


@isTest
private class ErrorMessagetestclass {
    
    Static testmethod void myTest(){
        Opportunity opp = new Opportunity ();
        opp.Name = 'Test';
        opp.AccountID= '0042d0001064Oo9BAE';
        opp.StageName = 'Qualification';
        opp.CloseDate = Date.parse('12/10/2021');
        opp.Type = 'Supplier';
                insert opp;

}
    }

Thanks again @CharuDutt :)