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
Rolando EstevesRolando Esteves 

Problem with testClass

Hi I found this class and I made some adjustments in order to use it.

 

Here is my class:

 

Public Class SlideShow
{

    public String notiDescription { get; set; }
    public String notiID { get; set; }

        //1st Product Photo
    public List<Product__c> Prodphoto {get; set;}
    public String P_imageTag {get; set;}
    public Integer P_strt=0,P_ed=1,P_len=0,P_i=0,P_indx=0;
    public String photo_tmp {get; set;}
    
    //2nd Product Photo
    public List<Product__c> Prodphoto_two {get; set;}
    public String P_imageTag_two {get; set;}
    public Integer P_strt_two=0,P_ed_two=1,P_len_two=0,P_i_two=0,P_indx_two=0;
    public String photo_tmp_two {get; set;}
    
    //3rd Product Photo
    public List<Product__c> Prodphoto_tree {get; set;}
    public String P_imageTag_tree {get; set;}
    public Integer P_strt_tree=0,P_ed_tree=1,P_len_tree=0,P_i_tree=0,P_indx_tree=0;
    public String photo_tmp_tree {get; set;}

    //slideShow Photos
    public List<Noticia__c> photo {get; set;}
    public String imageTag {get; set;}
    public String photoUrl {get; set;}
    public Integer strt=0,ed=1,len=0,i=0,indx=0;
    public String tmp {get; set;}
    
        //slideShow Photos
    public List<Metrica__c> metrica_photo {get; set;}
    public String metrica_imageTag {get; set;}
    public String metrica_photoUrl {get; set;}
    public Integer metrica_strt=0,metrica_ed=1,metrica_len=0,metrica_i=0,metrica_indx=0;
    public String metrica_photo_tmp {get; set;}
    

   public void SlideShow()
   {
      strt=0;
      ed=1;
      len=0;
      i=0;
      indx=0;
      Integer count_foto = [SELECT COUNT() FROM Noticia__c];
      Integer rand_foto = Math.floor(Math.random() * count_foto).intValue();  
      // Retrieve first noticia Image          
      String sql = 'SELECT image__c,Descripcion__c, Id FROM Noticia__c A LIMIT 1 OFFSET ';
      sql = sql+rand_foto;
      photo = Database.query(sql);
      imageTag = photo.get(indx).image__c;
      notiDescription = photo.get(indx).Descripcion__c;
      notiID = photo.get(indx).Id;
      strt = imageTag.indexOf('src=',0) + 5;
      ed = imageTag.lastIndexOf('alt=') - 2;
      tmp = imageTag.substring(strt,ed);
   }
      public void SlideShowDesc()
   {
      notiDescription = photo.get(indx).Descripcion__c;
      notiID = photo.get(indx).Id;
   }
    public  SlideShow()
    {
    
      Integer count_foto = [SELECT COUNT() FROM Noticia__c];
      Integer rand_foto = Math.floor(Math.random() * count_foto).intValue();  
      // Retrieve first noticia Image          
      String sql = 'SELECT image__c,Descripcion__c, Id FROM Noticia__c A LIMIT 1 OFFSET ';
      sql = sql+rand_foto;
      photo = Database.query(sql);
      imageTag = photo.get(indx).image__c;
      notiDescription = photo.get(indx).Descripcion__c;
      notiID = photo.get(indx).Id;
      strt = imageTag.indexOf('src=',0) + 5;
      ed = imageTag.lastIndexOf('alt=') - 2;
      tmp = imageTag.substring(strt,ed);
      
      // Retrieve active Metrica          
      String metrica_sql = 'SELECT graphic__c FROM Metrica__c A WHERE A.Active__c =' + 'true';
      metrica_photo = Database.query(metrica_sql);
      metrica_imageTag = metrica_photo.get(metrica_indx).graphic__c;
     
      metrica_strt = metrica_imageTag.indexOf('src=',0) + 5;
      metrica_ed = metrica_imageTag.lastIndexOf('alt=') - 2;
      metrica_photo_tmp = metrica_imageTag.substring(metrica_strt,metrica_ed);
     
      // Retrieve 1st Random Product
      Integer count = [SELECT COUNT() FROM Product__c];
      Integer rand = Math.floor(Math.random() * count).intValue();      
      String foto_sql = 'SELECT Product_Logo__c FROM Product__c LIMIT 1 OFFSET '; 
      foto_sql = foto_sql + rand;
      Prodphoto = Database.query(foto_sql);
      P_imageTag = Prodphoto.get(P_indx).Product_Logo__c;
     
      P_strt = P_imageTag.indexOf('src=',0) + 5;
      P_ed = P_imageTag.lastIndexOf('alt=') - 2;
      photo_tmp = P_imageTag.substring(P_strt,P_ed);
      
      // Retrieve 2nd Random Product
      Integer count_two = [SELECT COUNT() FROM Product__c];
      Integer rand_two = Math.floor(Math.random() * count_two).intValue();      
      String foto_sql_two = 'SELECT Product_Logo__c FROM Product__c LIMIT 1 OFFSET '; 
      foto_sql_two = foto_sql_two + rand_two;
      Prodphoto_two = Database.query(foto_sql_two);
      P_imageTag_two = Prodphoto_two.get(P_indx_two).Product_Logo__c;
     
      P_strt_two = P_imageTag_two.indexOf('src=',0) + 5;
      P_ed_two = P_imageTag_two.lastIndexOf('alt=') - 2;
      photo_tmp_two = P_imageTag_two.substring(P_strt_two,P_ed_two);
      
      // Retrieve 3rd Random Product
      Integer count_tree = [SELECT COUNT() FROM Product__c];
      Integer rand_tree = Math.floor(Math.random() * count_tree).intValue();      
      String foto_sql_tree = 'SELECT Product_Logo__c FROM Product__c LIMIT 1 OFFSET '; 
      foto_sql_tree = foto_sql_tree + rand_tree;
      Prodphoto_tree = Database.query(foto_sql_tree);
      P_imageTag_tree = Prodphoto_tree.get(P_indx_tree).Product_Logo__c;
     
      P_strt_tree = P_imageTag_tree.indexOf('src=',0) + 5;
      P_ed_tree = P_imageTag_tree.lastIndexOf('alt=') - 2;
      photo_tmp_tree = P_imageTag_tree.substring(P_strt_tree,P_ed_tree);
    }          
   
    public void prev()
    {
      if(indx != 0)
      {
        indx = indx - 1;
        String sql =  'SELECT image__c,Descripcion__c, Id FROM Noticia__c A';
        photo = Database.query(sql);
        imageTag = photo.get(indx).image__c;
        notiDescription = photo.get(indx).Descripcion__c;
        notiID = photo.get(indx).Id;
     
        strt = imageTag.indexOf('src=',0) + 5;
        ed = imageTag.lastIndexOf('alt=') - 2;
        tmp = imageTag.substring(strt,ed);
      }
    }   

    public void nxt()
    {  
      len = [SELECT COUNT() FROM Noticia__c];
      if(indx < (len-1))
      {
        indx = indx + 1;
        String sql =  'SELECT image__c,Descripcion__c, Id FROM Noticia__c A';
        photo = Database.query(sql);
        imageTag = photo.get(indx).image__c;
        notiDescription = photo.get(indx).Descripcion__c;
        notiID = photo.get(indx).Id;
     
        len =imageTag.length();
        strt = imageTag.indexOf('src=',0) + 5;
        ed = imageTag.lastIndexOf('alt=') - 2;
        tmp = imageTag.substring(strt,ed);
      } 
    }       
}

 

Here is my test class that for some reason only covers 27% of the class:

 

@isTest
private class SlideShow_TestMethod // Name of the test class, Can be any name
{         
    static testmethod void SlideShow_TestMethod()   // Name of the test method. Must be the same as the class name 
    {
        SlideShow ss = new SlideShow();   // Initialize variable for original class 
        ss.SlideShowDesc();
        ss.prev();
        ss.nxt();
        ss.SlideShow();
    }   
     
}

 

Thanks for all the help in advance.

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Rolando:

 

1. You have a method public void SlideShow() {..} . This is not the constructor and doesn't appear to get called.

 

BTW - testmethods can be named anything; they do not have to be named, nor should be named, the same as the test class. This way you can have many testmethods in a single test class

 

2. The best practice in debugging these test coverage issues is to look at the statements that are never executed (you can get this from Run Tests in either the UI or Eclipse IDE); then insert System.debug(..) statements to try and figure out why sections of your code aren't being called.  You can also use anonymous Apex execution to invoke specific methods to gain a better understanding of how your code operates