+ Start a Discussion
NatureGodNatureGod 

Apex Class 71% :( ...only 4 to 75!

Hi Team,

I have the following trigger:

trigger UpdateHoursinCase on Case(after insert, after update)

{
  Map<ID, Contract> parentCont= new Map<ID, Contract>();
  List<Id> listIds = new List<Id>();

  for (Case childObj : Trigger.new) {
    listIds.add(childObj.Contract__c);
  }

  parentCont= new Map<Id, Contract>([SELECT id, Hours__c FROM Contract WHERE ID IN :listIds]);

  for (Case Cas: Trigger.new){
     Contract myParentCont = parentCont.get(Cas.Contract__c);
     myParentCont.Hours__c +=   Cas.HoursofWork__c;
  }

  update parentCont.values();
}

which counts hours from the "Child" Case to the Parent Contract.
I works fine.
The Class is:

@istest
public class UpdateLocationStatusonLocationClass{
    Private Static testmethod void TesttrgOnArticulo(){
        Test.startTest();
        Contract  CONT = new Contract ();

        CONT.RecordTypeId  = '012D0000000kZsP';
        CONT.AccountId = '001D0000010dW4H';
//        CONT.AccountId = '001L000000KmBfF';
       
        CONT.CustomerSignedId = '003D0000017D7y8';
//        CONT.CustomerSignedId = '003L000000HQ131';
       
        CONT.Status = 'Draft';   
        String datea = '2000-11-01';
        CONT.StartDate = Date.valueOf(datea);
        CONT.ContractTerm = 9;
      
        insert CONT;
        List<Case>Lstcas=new List<Case>();
        for(integer i=0; i<5; i++){
       
            Case CAS = new Case();
            CAS.Contract__c = CONT.id;
            CAS.Location__c = 'Remote';           
            CAS.Charge__c = 'PER CALL';           
            CAS.AccountId = '001D0000010dW4H';
            CAS.ContactId = '003D0000017D7y8';           
            CONT.Status = 'Closed';
            CAS.Priority= 'EXECUTIVE';           

            Lstcas.add(CAS);
        }
        insert Lstcas;
       
        CONT.RecordTypeId  = '012D0000000kZsP';
        CONT.AccountId = '001D0000010dW4H';
        CONT.CustomerSignedId = '003D0000017D7y8';
        CONT.Status = 'Draft';    
        CONT.StartDate = Date.valueOf(datea);
        CONT.ContractTerm = 9;
       
        update CONT;
       
        Test.stopTest();
    }
}

And it gets only 71% !
Any suggestions to add 4% ?
Best Regards,
Stefanos
Best Answer chosen by NatureGod
Sri549Sri549
Hello Stefans,
As per your requirment in test class

@IsTest
public class Test_UpdateHoursinCase
{
public static testmethod void UpdateHoursinCase()
{
   Account a=new Account();
   a.Name='Test';
   Insert a;
   Contract co=new Contract();
   co.AccountId=a.id;
   co.Hours__c=3;
   Insert co;
   Case c=new Case();
   c.HoursofWork__c=3;
   c.Contract__c=co.id;
   Insert c;
  
}
 
}

i have taken only some fields please add remaining fields and check it and small suggestion is like dont use hard coded Ids please, it provides problem when you migrate.

if you still get issue with coverage please ping me.

it Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer



All Answers

Sri549Sri549
Hello Stefans,
As per your requirment in test class

@IsTest
public class Test_UpdateHoursinCase
{
public static testmethod void UpdateHoursinCase()
{
   Account a=new Account();
   a.Name='Test';
   Insert a;
   Contract co=new Contract();
   co.AccountId=a.id;
   co.Hours__c=3;
   Insert co;
   Case c=new Case();
   c.HoursofWork__c=3;
   c.Contract__c=co.id;
   Insert c;
  
}
 
}

i have taken only some fields please add remaining fields and check it and small suggestion is like dont use hard coded Ids please, it provides problem when you migrate.

if you still get issue with coverage please ping me.

it Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Srinivas
SFDC Certified Developer



This was selected as the best answer
sfdc_ninjasfdc_ninja
Just a fair warning, but even if you get the 75% coverage, this test code should be reworked.  You are hardcoding ID's which is a big no-no, and will likely cause issues when you try to deploy.  You also are doing no assertions, which really means you aren't testing the logic, but just making sure the code doesnt crash.

Take a look at these

http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests

http://wiki.developerforce.com/page/Apex_Code_Best_Practices

NatureGodNatureGod
Hi Srinivas,

Your suggestion really counted and worked fine.
Definately a Solution.
Thank you very much.
Stefanos