+ Start a Discussion
maiyakumaiyaku 

For Loop not work in Test Class

 

Coverage 8% - - 

Test Class is not work a red zone area 

 

1 Public Class Tomoring {
2
3    public static void CreateTomoring() {
4    
5    Integer EHODaybyMonth = 0;
6    Integer i;
7    
8    DateTime DateNow = System.now();
9
10    List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from 11Time_Attendance__c Where Check_out__c = null];
12    
13        if((DateNow.Hour() + DateNow.Minute()) == 0){
14                     
15**            for(Time_Attendance__c TA : lstTA){
                TA.Check_out__c = System.now() - 1;
                update TA;
                
                Time_Attendance__c TAC = new Time_Attendance__c ();
                TAC.Check_in__c = System.Now();              
                DateTime Ci = TAC.Check_in__c ;
                DateTime Co = TAC.Check_Out__c ;
                TAC.Name = TA.Agent_Name__c  +  ' ' + Co.Day() + '/' + Co.Month() + '/' + Co.Year() + ' ' + Co.Hour() + ':' + Co.Minute();
                TAC.RecordFinish__c = True;
                
                Double D,M,Y,H,MN = 0;
                
                if(Ci.Minute() > Co.Minute()){
                    if(Ci.Day() > Co.Day()){
                        if(Ci.Month() > Co.Month()){
                                Integer CalMonth =  Co.Month() - Ci.Month();
                                
                                Integer CalDay = Ci.Month();
                                
                                for(i =  Ci.Month();i < Co.Month();i++){                                
                                        
                                                IF(i == 1){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 2){ EHODaybyMonth = 28*24;}
                                                Else IF(i == 3){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 4){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 5){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 6){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 7){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 8){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 9){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 10){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 11){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 12){ EHODaybyMonth = 31*24;}
                                                
                                                CalDay = CalDay + EHODaybyMonth ;
                                
                                }
                                
                                D = (Co.Day() -  Ci.Day()) * 24;
                                H = ((Co.Hour() -  Ci.Hour()) + D + CalDay) - 1;
                                MN = ((Co.Minute() + 60) - Ci.Minute());
                         }
                         Else{ //CoMonth less than CiDay
                                D = (Co.Day() -  Ci.Day()) * 24;
                                H = ((Co.Hour() -  Ci.Hour()) + D) - 1;
                                MN = ((Co.Minute() + 60) - Ci.Minute());
                         }
                    }
                    Else{  //CoDay less than CiDay          
                         if(Co.Month() > Ci.Month()){    
                             
                                H = ((Co.Hour() -  Ci.Hour()) ) - 1;
                                MN = ((Co.Minute() + 60) - Ci.Minute());      
                         }
                         
                   } 
                }
                Else{
                     if(Co.Day() > Ci.Day()){
                             if(Co.Month() > Ci.Month()){
                            
                                    Integer CalMonth =  Co.Month() - Ci.Month();
                                    Integer CalDay = Ci.Month();
                                    
                                    for(i =  Ci.Month();i < Co.Month();i++){                                
                                            
                                                    IF(i == 1){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 2){ EHODaybyMonth = 28*24;}
                                                    Else IF(i == 3){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 4){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 5){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 6){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 7){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 8){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 9){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 10){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 11){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 12){ EHODaybyMonth = 31*24;}
                                                    
                                                    CalDay = CalDay + EHODaybyMonth ;
                                    
                                    }
                                    
                                    D = (Co.Day() -  Ci.Day()) * 24;
                                    H = ((Co.Hour() -  Ci.Hour()) + D + CalDay);
                                    MN = (Co.Minute() - Ci.Minute()) ;
                                //System.AssertEquals('','Else ' + D + ': ' + M);
                                }
                                Else{
                                    D = (Co.Day() -  Ci.Day()) * 24;
                                    H = ((Co.Hour() -  Ci.Hour()) + D);
                                    MN = (Co.Minute() - Ci.Minute()) ;
                                }
                        }
                        Else{
                                H = ((Co.Hour() -  Ci.Hour()));
                                MN = (Co.Minute() - Ci.Minute()) ;
                        }
                       
                }
                
                
                TAC.Time_Calculate__c = H+(MN/100);
                
                insert TAC;
            }           
        }   
    }

 this Line 15 more is a red zone not work

 

Static testmethod void TestTomoring () {
            
            
            CreateTomoring();
            
            Profile p = [select id from profile where name = 'System Administrator'];
            
            User Ua = New User();
            Ua.Assignment_Group_Active__c = True;
            Ua.CountClick__c = 2;
            Ua.EditbySup__c = False;
            Ua.profileid = p.Id;
            Ua.Username = 'Test@sccc.co.th';
            Ua.FirstName = 'hello';
            Ua.LastName = 'Test';
            Ua.Email = 'Test@sccc.co.th';
            Ua.Alias = 'HTA';
            Ua.isActive = true;
            Ua.emailencodingkey='UTF-8';
            Ua.timezonesidkey='America/Los_Angeles';
            Ua.CommunityNickname = 'OKTEST';
            Ua.localesidkey='en_US';
            Ua.languagelocalekey='en_US';
            insert Ua;
            
            Time_Attendance__c TAC = New Time_Attendance__c();
            TAC.Check_in__c = System.Now();              
            TAC.Name = 'Test' + System.Now();
            TAC.RecordFinish__c = True;
            TAC.Time_Calculate__c = 12.3;
            insert TAC;
            
            TAC.Check_out__c = System.now() - 1;
            Update TAC;
            
            List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from Time_Attendance__c Where Check_out__c = null];
            
            for(Time_Attendance__c TA : lstTA){
                
                TA.Check_out__c = System.now() - 1;

                       Update TA;

                        Time_Attendance__c TAC1 = New Time_Attendance__c();
                        TAC1.Check_in__c = System.Now();              
                        TAC1.Name = 'Test' + System.Now();
                        TAC1.RecordFinish__c = True;
                        TAC1.Time_Calculate__c = 12.3;
                        insert TAC1;
                
            }
            
            
            
    }
    
}

Thank you.

Happy new year !

colemabcolemab

I think you are calling your class method (CreateTomoring) *before* creating your test data and because of this, the list named lstTA is empty.  Thus the for loop does not execute.

maiyakumaiyaku

I'm Change calling class method. but not workใ

For example, do you have for this case.

 

this change

 

Static testmethod void TestTomoring () {
            
            
            Test.StartTest();
            
            Profile p = [select id from profile where name = 'System Administrator'];
            
            User Ua = New User();
            Ua.Assignment_Group_Active__c = True;
            Ua.CountClick__c = 2;
            Ua.EditbySup__c = False;
            Ua.profileid = p.Id;
            Ua.Username = 'Test@sccc.co.th';
            Ua.FirstName = 'hello';
            Ua.LastName = 'Test';
            Ua.Email = 'Test@sccc.co.th';
            Ua.Alias = 'HTA';
            Ua.isActive = true;
            Ua.emailencodingkey='UTF-8';
            Ua.timezonesidkey='America/Los_Angeles';
            Ua.CommunityNickname = 'OKTEST';
            Ua.localesidkey='en_US';
            Ua.languagelocalekey='en_US';
            insert Ua;
            
            Time_Attendance__c TAC = New Time_Attendance__c();
            TAC.Check_in__c = System.Now();              
            TAC.Name = 'Test' + System.Now();
            TAC.RecordFinish__c = True;
            TAC.Time_Calculate__c = 12.3;
            insert TAC;
            
            TAC.Check_out__c = System.now() - 1;
            Update TAC;
            
            
            
            List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from Time_Attendance__c Where Check_out__c = null];
            
            Test.StopTest();
            
            CreateTomoring();
            
            for(Time_Attendance__c TA : lstTA){
                
                TA.Check_out__c = System.now() - 1;

                       Update TA;

                        Time_Attendance__c TAC1 = New Time_Attendance__c();
                        TAC1.Check_in__c = System.Now();              
                        TAC1.Name = 'Test' + System.Now();
                        TAC1.RecordFinish__c = True;
                        TAC1.Time_Calculate__c = 12.3;
                        insert TAC1;
                
            }
            
            
            
    }

 Thank you


colemabcolemab

Ok so now you are calling the test method in the correct order but your loop still isn't executing due to the list being empty.

 

The list is empty because you don't have any test data that matches the SOQL query.  I think you aren't satisfying the 'Where Check_out__c = null' part of your query because you are setting check out in this line: TAC.Check_out__c = System.now() - 1;

maiyakumaiyaku

Thank for answer colemab but not work. I delete TAC.Check_out__c = System.now() - 1; It nothing happens.

 

After Del TAC.Check_out__c = System.now() - 1;

Time_Attendance__c TAC = New Time_Attendance__c();
            TAC.Check_in__c = System.Now();        
            TAC.Name = 'Test' + System.Now();
            TAC.RecordFinish__c = True;
            TAC.Time_Calculate__c = 12.3;
            insert TAC;
            
                   
            List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from Time_Attendance__c Where Check_out__c = null];
            
            
            
            CreateTomoring();
            
            for(Time_Attendance__c TA : lstTA){
                

                        Time_Attendance__c TAC1 = New Time_Attendance__c();
                        TAC1.Check_in__c = System.Now();              
                        TAC1.Name = 'Test' + System.Now();
                        TAC1.RecordFinish__c = True;
                        TAC1.Time_Calculate__c = 12.3;
                        insert TAC1;
                
            }

 Thank you so much.

colemabcolemab

It looks like you have a typo in the query:

List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from 11Time_Attendance__c Where Check_out__c = null];

 

Shouldn't that be:

List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from Time_Attendance__c Where Check_out__c = null];

 Notice the leading 11 was removed from the object in the SOQL search (i.e. Time_attendance__c)

maiyakumaiyaku

No 11 in Actual Class this is sample

colemabcolemab

If you don't post your actual code, I don't know that I can help in a meaningful way.

maiyakumaiyaku

Sorry this Actual Class . it not run line number 1-15

 

Public Class Tomoring {

    public static void CreateTomoring() {
    
    Integer EHODaybyMonth = 0;
    Integer i;
    
    DateTime DateNow = System.now();

    List<Time_Attendance__c> lstTA = [Select id,Agent_Name__c,Check_in__c,Check_out__c,RecordFinish__c,Time_Calculate__c  from Time_Attendance__c Where Check_out__c = null];
    
        if((DateNow.Hour() + DateNow.Minute()) == 0){
                     
            for(Time_Attendance__c TA : lstTA){
                TA.Check_out__c = System.now() - 1;
                update TA;
                
                Time_Attendance__c TAC = new Time_Attendance__c ();
                TAC.Check_in__c = System.Now();              
                DateTime Ci = TAC.Check_in__c ;
                DateTime Co = TAC.Check_Out__c ;
                TAC.Name = TA.Agent_Name__c  +  ' ' + Co.Day() + '/' + Co.Month() + '/' + Co.Year() + ' ' + Co.Hour() + ':' + Co.Minute();
                TAC.RecordFinish__c = True;
                
                Double D,M,Y,H,MN = 0;
                
                if(Ci.Minute() > Co.Minute()){
                    if(Ci.Day() > Co.Day()){
                        if(Ci.Month() > Co.Month()){
                                Integer CalMonth =  Co.Month() - Ci.Month();
                                
                                Integer CalDay = Ci.Month();
                                
                                for(i =  Ci.Month();i < Co.Month();i++){                                
                                        
                                                IF(i == 1){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 2){ EHODaybyMonth = 28*24;}
                                                Else IF(i == 3){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 4){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 5){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 6){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 7){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 8){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 9){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 10){ EHODaybyMonth = 31*24;}
                                                Else IF(i == 11){ EHODaybyMonth = 30*24;}
                                                Else IF(i == 12){ EHODaybyMonth = 31*24;}
                                                
                                                CalDay = CalDay + EHODaybyMonth ;
                                
                                }
                                
                                D = (Co.Day() -  Ci.Day()) * 24;
                                H = ((Co.Hour() -  Ci.Hour()) + D + CalDay) - 1;
                                MN = ((Co.Minute() + 60) - Ci.Minute());
                         }
                         Else{ //CoMonth less than CiDay
                                D = (Co.Day() -  Ci.Day()) * 24;
                                H = ((Co.Hour() -  Ci.Hour()) + D) - 1;
                                MN = ((Co.Minute() + 60) - Ci.Minute());
                         }
                    }
                    Else{  //CoDay less than CiDay          
                         if(Co.Month() > Ci.Month()){    
                             
                                H = ((Co.Hour() -  Ci.Hour()) ) - 1;
                                MN = ((Co.Minute() + 60) - Ci.Minute());      
                         }
                         
                   } 
                }
                Else{
                     if(Co.Day() > Ci.Day()){
                             if(Co.Month() > Ci.Month()){
                            
                                    Integer CalMonth =  Co.Month() - Ci.Month();
                                    Integer CalDay = Ci.Month();
                                    
                                    for(i =  Ci.Month();i < Co.Month();i++){                                
                                            
                                                    IF(i == 1){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 2){ EHODaybyMonth = 28*24;}
                                                    Else IF(i == 3){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 4){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 5){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 6){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 7){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 8){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 9){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 10){ EHODaybyMonth = 31*24;}
                                                    Else IF(i == 11){ EHODaybyMonth = 30*24;}
                                                    Else IF(i == 12){ EHODaybyMonth = 31*24;}
                                                    
                                                    CalDay = CalDay + EHODaybyMonth ;
                                    
                                    }
                                    
                                    D = (Co.Day() -  Ci.Day()) * 24;
                                    H = ((Co.Hour() -  Ci.Hour()) + D + CalDay);
                                    MN = (Co.Minute() - Ci.Minute()) ;
                                //System.AssertEquals('','Else ' + D + ': ' + M);
                                }
                                Else{
                                    D = (Co.Day() -  Ci.Day()) * 24;
                                    H = ((Co.Hour() -  Ci.Hour()) + D);
                                    MN = (Co.Minute() - Ci.Minute()) ;
                                }
                        }
                        Else{
                                H = ((Co.Hour() -  Ci.Hour()));
                                MN = (Co.Minute() - Ci.Minute()) ;
                        }
                       
                }
                
                
                TAC.Time_Calculate__c = H+(MN/100);
                
                insert TAC;
            }           
        }   
    }

 

colemabcolemab

Ok now can you post your current / full test method?

colemabcolemab

I think your test method should have code like this:

 

Time_Attendance__c TAC = New Time_Attendance__c();
TAC.Check_in__c = System.Now();              
TAC.Name = 'Test' + System.Now();
TAC.RecordFinish__c = True;
TAC.Time_Calculate__c = 12.3;
insert TAC;

Tomoring MyObject = new Tomoring();

MyObject.CreateTomoring();

 

maiyakumaiyaku

Error: Compile Error: Static methods cannot be invoked through an object instance: CreateTomoring()

 

CreateTomoring is Void methods 

colemabcolemab

It isn't the void, it is the static causing the eror.   Try test code that calls the method directly instead of via an object:

 

Time_Attendance__c TAC = New Time_Attendance__c();
TAC.Check_in__c = System.Now();              
TAC.Name = 'Test' + System.Now();
TAC.RecordFinish__c = True;
TAC.Time_Calculate__c = 12.3;
insert TAC;

CreateTomoring();

 

kiranmutturukiranmutturu

static method should be used with class name only....

like

  classname.methodname();

maiyakumaiyaku

Coverage

 

Nothing happens.

this Coverage 8 now

 

Thank Kiran

colemabcolemab

Based on you screen shot, your issue is with the if statement and the for loop never executes.

 

The If statement I am talking about is: if((DateNow.Hour() + DateNow.MInute() == 0)

maiyakumaiyaku

Very Good Answer thank colemab. I comment if((DateNow.Hour() + DateNow.MInute() == 0) Coverage change to 18% , but a new problem 

 

System.NullPointerException: Attempt to de-reference a null object

 

Class.Tomoring.CreateTomoring: line 22, column 55

Class.Tomoring.TestTomoring: line 162, column 13 External entry point

 

Thank you so much.

colemabcolemab

Ok so what is on line 22?  I think this:

 

TAC.Name = TA.Agent_Name__c  +  ' ' + Co.Day() + '/' + Co.Month() + '/' + Co.Year() + ' ' + Co.Hour() + ':' + Co.Minute();

 

Coulple of thoughts here, the first is did you set an agent_name__c in your test data? 

 

Secondly, what is at column 55 in that line?  Did you set the check_out__c in your test data?  Isn't that what Co is built from? wouldn't CO be null then?