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
karthikarthi 

in test class getting only 50% coverage,and getting test failure

i'm not aware of writing test classes. i tried my level here. i think i have to include some hard coded values to get the coverage. i'm not sure what to do. plz help me in this.

hi here is my apex class: 

public class removerecord{
    public ID userid;
    public string UserList{get;set;}
    public string SelId;
    Public List<String> priID_SecIDStr = new List<String>();
      Public List<ID> IdSel = new List<ID>();
    public GE_PW_User_Sales_Hierarchy_Association__c pri = new GE_PW_User_Sales_Hierarchy_Association__c();
    public List<GE_PW_User_Sales_Hierarchy_Association__c> sec = new List<GE_PW_User_Sales_Hierarchy_Association__c>();

    public removerecord()
    {
        UserList = ApexPages.currentpage().getParameters().get('id');
        SelId = System.currentPageReference().getParameters().get('loc'); 
 
   }
  
   Public pageReference RemoveLocationIDs()
   {
      
       //**** Check for the split size
         if(SelId.split(',').size() == 1)
          {
          ID LocID = SelId;
          pri=[Select ID,GE_PW_Location_ID__c,GE_PW_Location_Level_Name__c,GE_PW_Parent_Location_ID__c,GE_PW_Parent_Location_Level_Name__c,GE_PW_Primary_OwnerShip_Flag__c from GE_PW_User_Sales_Hierarchy_Association__c where id =:LocID];
          Delete pri;   
          }  
         else 
          {
          priID_SecIDStr = SelId.split(',');
                 for(String s : priID_SecIDstr )
            {
           ID spliID = s;
           IdSel.add(spliID);
           sec=[Select ID,GE_PW_Location_ID__c,GE_PW_Location_Level_Name__c,GE_PW_Parent_Location_ID__c,GE_PW_Parent_Location_Level_Name__c,GE_PW_Primary_OwnerShip_Flag__c from GE_PW_User_Sales_Hierarchy_Association__c where id IN:IdSel];    
           Delete sec;
            }
          }
       PageReference NewPage=new PageReference('/apex/ManageLocationid?id='+UserList);
       NewPage.setRedirect(true);
       return NewPage;
           
     }
}     

 

 

test class which i have wrote is:

@isTest
private class Test_RemoveRecord{
    static testmethod void testRemoveRecord(){
    try 
    {
      Test.startTest();

        removerecord obj = new removerecord();       
        obj.RemoveLocationIDs();    
           
        Test.StopTest();       
      } 
    catch(Exception e)
        {
            system.debug('Error Message:::: '+ e.getmessage());
            system.assert(false,e.getmessage());
        }

    }
}

Best Answer chosen by Admin (Salesforce Developers) 
MandyKoolMandyKool

Hi,

 

You can pass your SellId to your page in following format.

xxxxxx,xxxxxxx

 

So your code will look like as follows:

 

 

/*

    Your existing code snippet.

     Keep it as it is.

*/

System.currentPageReference().getParameters().put​('loc',obj1.id);

removerecord obj = new removerecord();
obj.RemoveLocationIDs();

 

/* Replace 'xxxxxxx,xxxxxxxx' with proper values

    And add this code to your existing test code.

*/

ApexPages.currentPage().getParameters().put(SellId', xxxxxx,xxxxxxx);
PageReference pageRef = Page.YourVFPageName; //Add your VFPageName       

Test.setCurrentPage(pageRef);                        

removerecord obj = new removerecord();
obj.RemoveLocationIDs();

 

Hope this will help you to cover your else part.

All Answers

MandyKoolMandyKool

Hi,

 

I can help u write the test method, please tell me in brief what your class does?

karthikarthi

my class is for removing the record from the table. so for getting all the values from obj with checkbox validation i used wrapper class there. so for remove again i have to split the records by using split functionality. so in that class i'm splitting the records in the table and then deleting

karthikarthi

hi can you get that?

MandyKoolMandyKool

Hi,

 

For writing any test method you will need to prepare the data an insert it.

Also the data should be such that it should satisfy all your conditions.

 

once your data is set you will need to set your page as current page.

 

Following is the code for that.
PageReference pageRef = Page.YourVFPageName; //Add your VFPageName
Test.setCurrentPage(pageRef); 
ApexPages.currentPage().getParameters().put('id', objProgram.Id);
PageReference pageRef = Page.YourVFPageName; //Add your VFPageName        Test.setCurrentPage(pageRef);                         ApexPages.currentPage().getParameters().put('id', objProgram.Id);//Passing parameters 2 page

Then u can start calling your methods.

This way your code will be covered.

 

Hope this will help you.

 

MandyKoolMandyKool

Hi,

 

If its really urgent you can reach me at mandar19.kulkarni@gmail.com

kiranmutturukiranmutturu

i got 75%....executing your else block is some what hard.. you can use the test.isrunningtest() method to overcome this... but u have to use that method in your source class... If you have a chance to chage the source code then you can get more than 75%

 

 

@istest
private class testremoverecord{
    static testmethod void testb(){
    
        GE_PW_User_Sales_Hierarchy_Association__c obj1 = new GE_PW_User_Sales_Hierarchy_Association__c();
        obj1.name = 'test';
        insert obj1;
        
        System.currentPageReference().getParameters().put('loc',obj1.id);
        removerecord obj = new removerecord();
        
        obj.RemoveLocationIDs();
    }
    
    
}

 

karthikarthi
yes by trying this i'm getting 75% coverage. is there any way to increase the coverage with the same source code?
MandyKoolMandyKool

Yes you can improve on u r test code coverage.

You will have to find out which parts of your code is not getting covered and then prepare the data in such way that your code should cover that part.

 

As an example in your above code if else part is not getting executed then try to send the SellId in such a way that you if condition should be bypassed and the else part should be executed.

 

For that you will need to pass the such id so tht your else part is executed. In test method u can pass the id to your page twice -  once for if part and then for else part.

 

Hope this will help u!!

karthikarthi

how to pass the id seperately for both if part and else part. as i'm new for development i'm not aware of that

MandyKoolMandyKool

Hi,

 

You can pass your SellId to your page in following format.

xxxxxx,xxxxxxx

 

So your code will look like as follows:

 

 

/*

    Your existing code snippet.

     Keep it as it is.

*/

System.currentPageReference().getParameters().put​('loc',obj1.id);

removerecord obj = new removerecord();
obj.RemoveLocationIDs();

 

/* Replace 'xxxxxxx,xxxxxxxx' with proper values

    And add this code to your existing test code.

*/

ApexPages.currentPage().getParameters().put(SellId', xxxxxx,xxxxxxx);
PageReference pageRef = Page.YourVFPageName; //Add your VFPageName       

Test.setCurrentPage(pageRef);                        

removerecord obj = new removerecord();
obj.RemoveLocationIDs();

 

Hope this will help you to cover your else part.

This was selected as the best answer
karthikarthi
can we create the instance of class twice? i'm getting error as duplicate variable
MandyKoolMandyKool

Dear o dear,

 

I am just trying to help u out..

 

You can have other variable name and u can resolve that issue. :P

 

karthikarthi

hey thanks a lot for your guidance. I passed the selid as you told and now i'm getting 100%coverage.