Any one can help me how to cover the commented lines?
    Between the commented lines of code is not covered...

Apex class:
    public with sharing class Hoursprojectpopupcls{
    public boolean ccRequst{get;set;}
    public musqot__Costcentre__c ccRec{get;set;}
    public Hoursprojectpopupcls(ApexPages.StandardController controller) { 
            hrs = new Allocation_resources__c ();

    public void ccUserRec(){
    string sObjName =Id.valueof(ccId).getSObjectType().getDescribe().getName();
    ccRec=[SELECT Id,  owner.name, OwnerId, Name 
                           FROM  Costcentre__c 
                           where Id =:ccId];
                    if(ccRec.OwnerId== UserInfo.getUserId()){                                      

    Apex test class:
    public class HoursprojectpopupclsTest{
        static testMethod void Hoursprojectpopup(){
          Costcentre__c objCC=new Costcentre__c(); 
          objCC.Headline__c ='present';
          insert objCC;
          System.assertEquals('test', objCC.name);
          ApexPages.StandardController sc = new ApexPages.standardController(objCC);
            Hoursprojectpopupcls hrsprojpopup = new Hoursprojectpopupcls(sc);
          hrsprojpopup .ccUserRec();

2 possible answers:

  1. You are passing a record of object type of Costcentre__c from the test class but in the function you are checking with 'musqot__Costcentre__c'.

  2. In the test class, the line objCC.cc_Owner__c=u2.id; is setting some field value, I hope this doesnt affect the standard OwnerId field of the record. Plus create 2 records with different OwnerId and call the function twice to cover the inner if else conditions(ccRec.OwnerId== UserInfo.getUserId())

Add this in your test class.

User otherOwner = [SELECT Id from User where isActive = true and Id != :UserInfo.getUserId() LIMIT 1]; //This will give you another active user other than you.
    //Now when creating records in test class, 
    Costcentre__c objCC=new Costcentre__c(); 
              objCC.Headline__c ='present';
              objCC.OwnerId = UserInfo.getUserId(); // you are the owner
              insert objCC;
    Costcentre__c objCC2=new Costcentre__c(); 
              objCC2.Headline__c ='present';
              objCC2.OwnerId = otherOwner.Id// Other user is the owner.
              insert objCC2;

Then call the function twice with 2 different CC records just created.

  • cc_Owner__c filed is lookup to user object.
    – fsales572
    Nov 18 '15 at 9:35
  • That's I can see from your code. Did you check the 1st point? Try replacing musqot__Costcentre__c with only Costcentre__c Nov 18 '15 at 9:36
  • can you suggest me how to create 2 records with different ownerid's?
    – fsales572
    Nov 18 '15 at 10:12
  • I have updated my code. Nov 18 '15 at 10:18
  • Dont forget to mark your test class SeeAllData=true like this. @isTest(SeeAllData=true) Nov 18 '15 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.