You need to sign in to do that
Don't have an account?
abhilash reddy 49
how to write test classess?
Hi, I wrote Test Class for below Apex Class But I am getting only 67% code coverage how can i achieve 100% code coverage...Apex class and corresponding test class given below....
global class SiteLoginController1 {
public String username {get; set;}
public String password {get; set;}
public String EmpName {get;set;}
global SiteLoginController1 () {}
public boolean ErrorMsg{get;set;}
public PageReference CheckUserPswd() {
system.debug('----username----'+ username);
system.debug('----password----'+ password);
List<Employee__c> empList = [Select id, Name, User_Name__c,Password__c from Employee__c where User_Name__c =: username];
system.debug('----empList----'+ empList);
if(empList.size()>0){
if(empList[0].Password__c == password){
ErrorMsg = False;
system.debug('----'+ErrorMsg);
EmpName=empList[0].name;
system.debug('----'+EmpName);
//create a new page to enter task and associate here and also do the log off
PageReference pr=new PageReference('/apex/TimeSheetManagement?name='+EmpName);
system.debug('-----'+pr);
pr.setRedirect(true);
return pr;
}
else{
ErrorMsg = true;
return null;
}
}
else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'No Active Employee record found with provided username.Please contact your administrator'));
return null;
}
}
Public Pagereference UpdatePasswordPage(){
PageReference newPage;
newPage = new PageReference('/apex/Forgot_Password_VF_Page');
newPage.setRedirect(true);
return newPage;
}
Public Pagereference ChangePassword(){
PageReference newPage;
EmpName=username ;
system.debug('----'+EmpName);
if(String.isEmpty(EmpName)){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter The User Name'));
return null;
}else{
List<Employee__c> empList = [Select id, Name, User_Name__c,Password__c from Employee__c where User_Name__c =: EmpName];
system.debug('----'+empList);
if(empList.size()==0){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter The Valid User Name'));
return null;
}else{
EmpName=empList[0].name;
system.debug('----'+EmpName);
newPage = new PageReference('/apex/Change_Password_Vf_Page?name='+EmpName);
newPage.setRedirect(true);
return newPage;
}
}
}
}
----------------------------------------------------------------------My Test Class is-------------------------------------
@isTest
private class SiteLoginController1Test {
@isTest
private static void testSiteLogin() {
Employee__c e = new Employee__c() ;
e.Name ='Test' ;
// Add Pther required fields if any
insert e ;
SiteLoginController1 controller = new SiteLoginController1();
controller.CheckUserPswd();
controller.UpdatePasswordPage();
controller.ChangePassword();
}
}
global class SiteLoginController1 {
public String username {get; set;}
public String password {get; set;}
public String EmpName {get;set;}
global SiteLoginController1 () {}
public boolean ErrorMsg{get;set;}
public PageReference CheckUserPswd() {
system.debug('----username----'+ username);
system.debug('----password----'+ password);
List<Employee__c> empList = [Select id, Name, User_Name__c,Password__c from Employee__c where User_Name__c =: username];
system.debug('----empList----'+ empList);
if(empList.size()>0){
if(empList[0].Password__c == password){
ErrorMsg = False;
system.debug('----'+ErrorMsg);
EmpName=empList[0].name;
system.debug('----'+EmpName);
//create a new page to enter task and associate here and also do the log off
PageReference pr=new PageReference('/apex/TimeSheetManagement?name='+EmpName);
system.debug('-----'+pr);
pr.setRedirect(true);
return pr;
}
else{
ErrorMsg = true;
return null;
}
}
else{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'No Active Employee record found with provided username.Please contact your administrator'));
return null;
}
}
Public Pagereference UpdatePasswordPage(){
PageReference newPage;
newPage = new PageReference('/apex/Forgot_Password_VF_Page');
newPage.setRedirect(true);
return newPage;
}
Public Pagereference ChangePassword(){
PageReference newPage;
EmpName=username ;
system.debug('----'+EmpName);
if(String.isEmpty(EmpName)){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter The User Name'));
return null;
}else{
List<Employee__c> empList = [Select id, Name, User_Name__c,Password__c from Employee__c where User_Name__c =: EmpName];
system.debug('----'+empList);
if(empList.size()==0){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter The Valid User Name'));
return null;
}else{
EmpName=empList[0].name;
system.debug('----'+EmpName);
newPage = new PageReference('/apex/Change_Password_Vf_Page?name='+EmpName);
newPage.setRedirect(true);
return newPage;
}
}
}
}
----------------------------------------------------------------------My Test Class is-------------------------------------
@isTest
private class SiteLoginController1Test {
@isTest
private static void testSiteLogin() {
Employee__c e = new Employee__c() ;
e.Name ='Test' ;
// Add Pther required fields if any
insert e ;
SiteLoginController1 controller = new SiteLoginController1();
controller.CheckUserPswd();
controller.UpdatePasswordPage();
controller.ChangePassword();
}
}
Blue part means your test class covered that code and red part means you have to cover that one in test class. If you are writing one test method than at one time you can cover either IF condition or Else condition. You can't cover both if and else for one test method.
So go and check the part still covered as red. Create one new method in test class and provide the data which is restricting that red part of the code to be covered. Once you will create the data it will cover rest of the code.
Ex: You had written below condition-
if(String.isEmpty(EmpName)){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Please Enter The User Name'));
return null;
}
But in test class you had given- e.Name ='Test' ; So your test class will not cover this if condition it will goto else block.
So create one method and blank out e.name(Simply means create whatever data test class needed) it will cover this particular if. Like that you can cover rest of the code.