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
venkateshyadav1243venkateshyadav1243 

need test class

hi

 

am very new to test class plz help for the apex class for test class

 //Class for User(Contact) Authentication
public class Cls_ContactAuthentication {
    public String Username{get; set;}
    public String password{get; set;}
    public ID ContactID;
    public ID UserID;
    public Boolean LoginFlag{get; set;}
    public List<Contact> contacts;
    public Session__c session;
    
    public CLS_ContactAuthentication (){
        
    }
    
    public PageReference Login(){
       
        contacts= new List<Contact>();
        session = new Session__c();
        try{
            contacts= [Select
                        id,username__c,password__c
                        from
                        Contact
                        where
                        username__c=:Username
                        AND
                        password__c=:password
                        
                      ];
                      
         
          System.debug('#####'+contacts);      
        }Catch(Exception e){
            System.debug(e);
        }
        if(contacts.size() > 0){
            for(Contact a : contacts){
               UserID = a.ID;
               session.Name = a.id;
            }
                upsert session;
                LoginFlag = false;
               if(Username=='' || Password=='')
               {
             ApexPages.Message myMsg = new ApexPages.Message(ApexPages.severity.FATAL, 'Username or Password is missing.......');
             ApexPages.addmessage(myMsg);
             return new PageReference('/apex/VF_ContactAuthentication');
             }
            
               else
               {
                PageReference page = new PageReference('/apex/VF_ContactHomePage');
                page.getParameters().put('ID',UserID);
                page.setRedirect(true);
                return page;
                }
        }else{
                LoginFlag = true;
                return null;
        }
    }
}

 

thanks to all

RollNo1RollNo1

Coverage -> 80 %

 

@isTest(SeeAllData=true)
private class TestCLS_ContactAuthentication
{
    static testMethod void TestCLS_ContactAuthentication()
    {
        boolean LoginFlag = true;
       
        CLS_ContactAuthentication ca = new CLS_ContactAuthentication();
        ca.Login();
        
    }