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
k sfdck sfdc 

How to test below login page class?

public class LoginController {

    public PageReference doUsername() {
   
    pageReference ref=new pageReference('/apex/usernamepage');
        return ref.setRedirect(true);
    }


    public String password { get; set; }

    public String username { get; set; }
   
    public List<objject__c> lstcon{get; set;}
 
    public LoginController()
    {
    lstcon=new List<object__c>();
    }

    public PageReference login() {
       
     try{
            Contact con = [Select id, username__c, password__c from object__c
                                    where username__c=:username AND password__C=:password];
                                  
                               
           
            if(password == con.Password__c  && username == con.username__C ){
                pagereference ref = new pagereference('/apex/Fieldssearchpage?conid='+con.id);          
                     return ref.setRedirect(true);
            }
            else {
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Fatal,'Invalid Credentials..');
                ApexPages.addMessage(msg);
            }
        
   }    
       
        catch(exception e){
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Fatal,'No Record Exists in our database...');
            ApexPages.addMessage(msg);
       
    }   
        return null;
    }

}

please help me.............
Vatsal KothariVatsal Kothari
Hi,

you can refer below code:
@istest
public class LoginControllerTest{

	public static testmethod void testLogin ()
	{
		Account acc = new Account();
		acc.Name = 'test account';
		insert acc;
		
		Contact con = new Contact();
		con.Username__c = 'testuser';
		con.Password__c = 'testpwd';
		con.AccountId = acc.Id;
		con.Lastname = 'test';
		insert con;
		
		LoginController loginObj = new LoginController();
		loginObj.Username = 'testuser';
		loginObj.Password = 'testpwd';
		loginObj.Login();
		
		loginObj.Username = 'testuser123';
		loginObj.Password = 'testpwd123';
		loginObj.Login();
	}
}
If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal