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
B TulasiB Tulasi 

Write test Class for LoginPage

Hi All,

I am getting error in this . How to write test class for Login Page.

VisualForce Page Code: 
<apex:page controller="Sample">
<apex:form >
<apex:pageBlock >

    <br>UserName&nbsp;<apex:inputText label="center" value="{!userName}"/></br>
    <br>Password &nbsp;&nbsp;<apex:inputsecret value="{!password}"/></br>
    <apex:commandButton value="Login" action="{!loginPage}" reRender="Error"/>
    
    <apex:pageMessages id="Error"></apex:pageMessages> 
    
</apex:pageBlock>
</apex:form>
</apex:page>

Apex Class Code :

public class Sample {      
    public String userName{get;set;}
    public String password{get;set;}
    public List<Account> returns {get;set;}
    
    public Sample(){
    
      returns = new List<Account>();
    }    
    
    public PageReference loginPage(){
    
        returns = [select ID, Name, Sic from Account Where Name=:userName and sic=:password];
        if(!returns.isEmpty()) {
            Pagereference pr = new PageReference('/'+returns[0].Id);
            pr.setReDirect(true);
            return pr;
        }
        else {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Invalid UserName or Password'));
                return null;
        }
        //Pagereference pr = new PageReference('/'+returns[0].Id);
        //pr.setRedirect(true);
        //return pr;
    }

}






Test Class Code :

@isTest
public class SampleTest{
    public static testMethod void loginPageTestMethod(){
        Sample s=new Sample();
        Account acc=new Account(Name='Kokila');
        insert acc;
        s.loginPage();          
        System.assertEquals(s.pr,'Kokila');
        
    }
}



Thanks in advance
Thulasi
Best Answer chosen by B Tulasi
Amit Chaudhary 8Amit Chaudhary 8
Try below code.
@isTest
public class SampleTest
{
	public static testMethod void LoginFail()
	{
		Sample  tp=new Sample ();
		tp.loginPage();
	}
	public static testMethod void loginPass()
	{
		Account acc = new Account();
		acc.Name ='mecrin';
		acc.sic = '12345';
		insert acc;
		
		Sample  tp=new Sample ();
		tp.userName ='mecrin';
		tp.password  ='12345';
		
		tp.loginPage();
	}
}
Please let us k now if this will help you
 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try below code. I hope that will help you
@isTest
public class SampleTest
{
	public static testMethod void LoginFail()
	{
		Sample  tp=new Sample ();
		tp.loginPage();
	}
	public static testMethod void loginPass()
	{
		Account acc = new Account();
		acc.Name ='mecrin';
		acc.sic = '12345';
		insert acc;
		
		Sample  tp=new Sample ();
		tp.userName ='12345';
		tp.password  ='mecrin';
		
		tp.loginPage();
	}
}
Please let us know if this will help you
 
Amit Chaudhary 8Amit Chaudhary 8
Try below code.
@isTest
public class SampleTest
{
	public static testMethod void LoginFail()
	{
		Sample  tp=new Sample ();
		tp.loginPage();
	}
	public static testMethod void loginPass()
	{
		Account acc = new Account();
		acc.Name ='mecrin';
		acc.sic = '12345';
		insert acc;
		
		Sample  tp=new Sample ();
		tp.userName ='mecrin';
		tp.password  ='12345';
		
		tp.loginPage();
	}
}
Please let us k now if this will help you
 
This was selected as the best answer
B TulasiB Tulasi
Hi Amit,
    
      It's working perfectly . thank u so much