+ Start a Discussion
Thomas CailletThomas Caillet 

Code Coverage 4% (1/23)

Hello,

I tried to code a test trigger to put my triiger on production. I would like to know what is parenthesis on code coverage (1/23) and how to have 75% of code coverage
Here is my trigger:

trigger CreateBillingCase on Case (after update) {

if(trigger.new[0].Status=='closed' && (trigger.old[0].Status=='closed') && (trigger.new[0].Subject=='New hotel : Kick off') && (trigger.new[0]. Opportunity_Case__c != null))
                {
                
                                List<Features__c> Feat =new List<Features__c>();
                                List<Existing_feature__c> Existing =new List<Existing_feature__c>();
        
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, UnitPrice, ServiceDate from OpportunityLineItem where opportunityId =: trigger.new[0].Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        case c1=new case(
                                        Subject='Incoiving :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = trigger.new[0].AccountId, 
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Amount__c = OLI.UnitPrice);
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Account__c = trigger.new[0].AccountId);
                                        
                                        Feat.add(f1); 
                                     }
                                        insert c1;
                                     }
                                     
                                insert Feat;

                }
}

and my test :

@isTest
private class TestCreateBillingCaseonCase {

    @isTest static void TestCreateBillingCaseonCaseWithOpportunity() {
        // Test data setup
        // Create an account with an opportunity, and then try to delete it
        Test.startTest();        
        Case ca = new Case(Subject='New hotel : Kick off',Status='In progress');
        insert ca;
                
        Opportunity opp = new Opportunity(Name='Opportunity TEST',
                                       StageName='Closed Won',
                                       CloseDate=System.today().addMonths(1),
                                       AccountId=ca.AccountId);
        insert opp;

        // Perform test

        ca.Status='Closed';
        ca.Opportunity_Case__c=opp.Id;
        update ca;
        
         List<Features__c> Feat =new List<Features__c>();
         List<Existing_feature__c> Existing =new List<Existing_feature__c>();
        
                                for(OpportunityLineItem OLI : [SELECT PricebookEntry.Product2.Name, UnitPrice, ServiceDate from OpportunityLineItem where opportunityId =: ca.Opportunity_Case__c])
                                     {
                                    
                                        String ProductName= String.ValueOf(OLI.PricebookEntry.Product2.Name);
                                        case c1=new case(
                                        Subject='Incoiving :'+' '+ProductName,
                                        OwnerId='00G20000001HboH', 
                                        AccountId = ca.AccountId, 
                                        Accounting_first_invoicing_date__c = OLI.ServiceDate,
                                        Amount__c = OLI.UnitPrice);
                                        
                                for(Existing_feature__c EXF: [SELECT Name, FeatureId__c, Product__c from Existing_feature__c where Product__c =: OLI.PricebookEntry.Product2Id])
                                     {
                                        String FeatureName= String.ValueOf(EXF.Name);
                                        Features__c  f1=new Features__c(
                                        Name = FeatureName,
                                        Feature_Id__c = string.valueof(EXF.FeatureId__c),
                                        To_be_invoiced__c = true,
                                        Account__c = ca.AccountId);
                                        
                                        Feat.add(f1); 
                                     }

                                     }
        Test.stopTest();
    }
    
}

Thanks to you 
Thomas