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
KlivingstonKlivingston 

More test help

Hello guys i am reasonably new to the whole apex devlopment thing and had no idea that i had to write test classes before i was allowed to migrate classes to production. the code does work, it is just a question of getting the coverage needed o allow it to be migrated.

 

I am now trying to do this and am struggling to do it. i have been reading all the help guides and various blogs but I am struggling to get even 1% coverage.

 

The code I use quieries a custom object and returns records match a criteria. the apex page itself then displays this in a dashboard.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class retrieveBramhallLog 
 {
  private ApexPages.StandardSetController controller; 
  
    public String getTerminalName() 
    {
        return 'Bramhall';
        return 'Time_Flag__c';
    }

    public List<Daily_Log__c> getDailyLog()
    {
                return [SELECT Id, Name, Issue_Type__c, Time_created__c FROM Daily_Log__c
                        WHERE Terminal__r.Name = 'Bramhall' order by Time_created__c desc limit 10];
    }
 
     public List<Daily_Log__c> getOutbaseLog()
    {
                return [SELECT Id, Name, Issue_Type__c, Time_created__c FROM Daily_Log__c
                        WHERE Terminal__r.Name = 'Bramhall' AND (Time_Flag__c = 1 OR weekend_check__c = 1) order by Time_created__c desc limit 10];
    }
  }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

the test class that i have had most success with

 

@isTest 
private class try2{
    Static testMethod void t1()
    {
    Terminal__c term = new Terminal__c(Name ='test');
    insert term;
    Daily_Log__c dayLog = new Daily_Log__c(Terminal__c = 'a0AR0000005YLTC', Issue_Type__c = 'DBS');
    insert dayLog;
    try2 myclass = new try2();
    List<Daily_Log__c> l = myclass.getDailyLog();
    system.assert(l.size()>0);
    }


    public List<Daily_Log__c> getDailyLog()
    {
                return [SELECT Id, Name, Issue_Type__c, Time_created__c FROM Daily_Log__c
                        WHERE Terminal__r.Name = 'Bramhall' order by Time_created__c desc limit 1];
    }      
 }

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

I have got a test there which i think test if a list is created. Could anyone give me help on how to test the other parts of code so that I can test it fully?

This is the only way I have even got a test to run so if there is a more efficient way of doing it.

Best Answer chosen by Admin (Salesforce Developers) 
kiranmutturukiranmutturu

try this

 

@istest
private class testmiii{
    static testmethod void  mjuj(){
        Terminal__c obj1= new Terminal__c();
        obj1.name = 'Bramhall';
        insert obj1;
        
        retrieveBramhallLog obj =  new retrieveBramhallLog();
        obj.getDailyLog();
        obj.getOutbaseLog();
        obj.getTerminalName();
        
    }
}

but in your code ....in this method getTerminalName.. its not the way of returning the value, once the first return is executed it never look in to the next statement .. so no use of placing the second return there.... u can't test that also...Hope you understand my point 

 

All Answers

kiranmutturukiranmutturu

try this

 

@istest
private class testmiii{
    static testmethod void  mjuj(){
        Terminal__c obj1= new Terminal__c();
        obj1.name = 'Bramhall';
        insert obj1;
        
        retrieveBramhallLog obj =  new retrieveBramhallLog();
        obj.getDailyLog();
        obj.getOutbaseLog();
        obj.getTerminalName();
        
    }
}

but in your code ....in this method getTerminalName.. its not the way of returning the value, once the first return is executed it never look in to the next statement .. so no use of placing the second return there.... u can't test that also...Hope you understand my point 

 

This was selected as the best answer
KlivingstonKlivingston

 

 

Perfect, thank you very much.

 

U've also helped me understand the testing a bit better now as well. I was initally trying to test certain values in every field rather than just testing if the objects exist.