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
Amol DixitAmol Dixit 

Unable to execute Test Method

Hi,

 

I am trying to write test method form a class but having issue. Although I have given correct values the coverage is upto 40 % only. I dont know what is the problem. Please help.

 

public class DetailTrainingPlanController {
Public String message = System.currentPagereference().getParameters().get('msg');

    public DetailTrainingPlanController(editTrainingPlan controller) {

    }
public String message{get;set;}

 public double totalHours{get;set;}
  
  
public List<Training_Plan__c> TP{get;set;}
   

     public List<Submodule__c> moduleHour{get;set;}
   
   
     public Map<String, List<Submodule__c>> moduleMap {get;set;}
    
     public Map<String, double> hourMap {get;set;}
     public List<Submodule__c> submoduleList{get;set;}
     public List<String> submoduleNamesList{get;set;}
     public List<Module__c> moduleList;
     public List<double> hourlist{get;set;}
     Integer flag{get;set;}
    
     public DetailTrainingPlanController(){
         moduleMap = new Map<String, List<Submodule__c>>();
         TP=new List<Training_Plan__c>();
         TP=Database.Query('select  Selected_hours__c from Training_Plan__c where Name__c=:message');
         totalHours=0;
     }
     
     public PageReference back(){
         return new PageReference('/apex/CRUDTrainingPlan');
    }
 
 

    public void populatedata(){
    try{
   
        List<Training_Plan__c> trainingPlanList=new List<Training_Plan__c>();
        trainingPlanList=Database.query('Select unselected__c from Training_Plan__c where Name=:message');
        String submoduleNames=trainingPlanList[0].unselected__c;
        submoduleNamesList= new List<String>();
        if(submoduleNames!=null)
            submoduleNamesList=submoduleNames.split('/',100);
        flag=0;
        submoduleList=Database.query('Select selected__c,name from Submodule__c');
        for(Integer i=0;i<submoduleList.size();i++)
        {
              flag=0;
              for(Integer j=0;j<submoduleNamesList.size();j++)
              {
                    if(submoduleList[i].name==submoduleNamesList[j])
                    {
                        flag=1;
                    }
              }
              if(flag==0)
              {
                    submoduleList[i].selected__c=true;
              }
              else
                    submoduleList[i].selected__c=false;
      
              upsert submoduleList;
        }
        upsert submoduleList;
             hourlist=new List<Double>();
             hourmap=new map<String,Double>();
     Double hours=0;
        List<Module__c> lst=new List<Module__c>();
        lst=Database.query('select Name,name__c from Module__c');
        for(Integer i=0;i<lst.size();i++)
        {
        hours=0;
        String mname=lst[i].name;
        List<Submodule__c> sub=new List<Submodule__c>();
        sub=Database.query('select Name,Hours__c,selected__c from Submodule__c where Module__r.name=:mname');
        for(Integer j=0;j<sub.size();j++)
        {
            if(sub[j].selected__c==true)
            {
                hours=hours+Double.valueof(sub[j].Hours__c);
              
            }
        }
  
            hourlist.add(hours);
            hourmap.put(lst[i].name__c,hours);
             totalHours=totalHours+hours;
        }
      }
      catch(Exception e){}
      }

      public static TestMethod void checkTrainingPlan()
  {
   
    Training_Plan__c tp=new Training_Plan__c();
    tp.Name__c='Abc';
    tp.Selected_hours__c=Double.valueOf('10');
    tp.Unselected__c='SM-00001/SM-00002/SM-00003/SM-00005/';
    insert tp;
        PageReference pageref = Page.DetailTrainingPlan;
        pageref.getParameters().put('msg',tp.Name);// msg is not setting
              Test.setCurrentPage(pageref);
      
      DetailTrainingPlanController dtpc=new DetailTrainingPlanController();
       System.debug('Msg is '+dtpc.message+'EEEEEEEEEEE');
 
      dtpc.populateData();
   
  }
  public static TestMethod void testGetPopulate()
  {
//  Tried This as well but msg value is not set in class..
  PageReference pageref = Page.DetailTrainingPlan;
        pageref.getParameters().put('msg','TP-0074');
              Test.setCurrentPage(pageref);
                DetailTrainingPlanController dtpc=new DetailTrainingPlanController();
                dtpc.populateData();
  }
 

}

 

Thank you.

Rajesh SriramuluRajesh Sriramulu

Hi

 

Try This

I think u ised here 2 constructors one default adn another is parameterised constructors

 

public static TestMethod void checkTrainingPlan()
  {
   
    Training_Plan__c tp=new Training_Plan__c();
    tp.Name__c='Abc';
    tp.Selected_hours__c=Double.valueOf('10');
    tp.Unselected__c='SM-00001/SM-00002/SM-00003/SM-00

005/';
    insert tp;

  here insert the Submodule__c,Module__c,according to ur if condition


        PageReference pageref = Page.DetailTrainingPlan;
        pageref.getParameters().put('msg',tp.Name);// msg is not setting
              Test.setCurrentPage(pageref);
      editTrainingPlan plan1;
      DetailTrainingPlanController dtpc=new DetailTrainingPlanController(plan1);

     DetailTrainingPlanController dtpc1=new DetailTrainingPlanController
       System.debug('Msg is '+dtpc.message+'EEEEEEEEEEE');
 
      dtpc.populateData();
   dtpc1.populateData();

 

Here u have to pass the other methods through this object


  }

 

 

Regards,

Rajesh.

Amol DixitAmol Dixit

Hi,

Thank you for your guidance.I have tried it but although i have done   

 

PageReference pageref = Page.DetailTrainingPlan;
pageref.getParameters().put('msg',tp.Name);
Test.setCurrentPage(pageref);

 

still 'msg'  message value is not setting value, Plese help

 

 

 

public void populatedata(){
    try{
   
        List<Training_Plan__c> trainingPlanList=new List<Training_Plan__c>();
        trainingPlanList=Database.query('Select unselected__c from Training_Plan__c where Name=:message'); //Still Problem persist here.
        String submoduleNames=trainingPlanList[0].unselected__c;
        submoduleNamesList= new List<String>();
        if(submoduleNames!=null)
            submoduleNamesList=submoduleNames.split('/',100);
        flag=0;
        submoduleList=Database.query('Select selected__c,name from Submodule__c');
        for(Integer i=0;i<submoduleList.size();i++)
        {
              flag=0;
              for(Integer j=0;j<submoduleNamesList.size();j++)
              {
                    if(submoduleList[i].name==submoduleNamesList[j])
                    {
                        flag=1;
                    }
              }
              if(flag==0)
              {
                    submoduleList[i].selected__c=true;
              }
              else
                    submoduleList[i].selected__c=false;
      
              upsert submoduleList;
        }
        upsert submoduleList;
             hourlist=new List<Double>();
             hourmap=new map<String,Double>();
     Double hours=0;
        List<Module__c> lst=new List<Module__c>();
        lst=Database.query('select Name,name__c from Module__c');
        for(Integer i=0;i<lst.size();i++)
        {
        hours=0;
        String mname=lst[i].name;
        List<Submodule__c> sub=new List<Submodule__c>();
        sub=Database.query('select Name,Hours__c,selected__c from Submodule__c where Module__r.name=:mname');
        for(Integer j=0;j<sub.size();j++)
        {
            if(sub[j].selected__c==true)
            {
                hours=hours+Double.valueof(sub[j].Hours__c);
              
            }
        }
  
            hourlist.add(hours);
            hourmap.put(lst[i].name__c,hours);
             totalHours=totalHours+hours;
        }
      }
      catch(Exception e){}
      }

 

Thank you.

 

Amol Dixit.

Rajesh SriramuluRajesh Sriramulu

Hi

 

 

See this link:

 

http://www.forcetree.com/2009/06/passing-parameters-to-visualforce-page.html

 

Hope this will help.

 

Regards,

Rajesh.

Abhay AroraAbhay Arora

Test.setCurrentPageReference(new PageReference('Page.YOUR PAGE NAME'));
System.currentPageReference().getParameters().put('MSG', 'MY MESSAGE');

I am using above so its  tested and functional