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
Mecrin Luvis 10Mecrin Luvis 10 

Test Class2

Hi Guys,

I have created VF page and Apex class for Login Page. I want to write a test class for that login page.Please help .
VF Page:
<apex:page showHeader="false" controller="TestPage">

  <center>
  <apex:form >
  <apex:panelGrid >
  <apex:pageBlock title="LoginPage" >
  <apex:pageMessages id="msg"></apex:pageMessages>

  <apex:pageBlockSection >
 <p><b>UserName</b><br /><apex:inputText required="true" id="username" value="{!username}"/></p>
  <p><b>Password</b><br/><apex:inputSecret id="password" value="{!password}" /><br/></p>
              
  </apex:pageBlockSection>
  <apex:pageBlockButtons location="bottom"><apex:commandButton action="{!login}" value="login" id="login"/></apex:pageBlockButtons>
  </apex:pageBlock></apex:panelGrid>
  </apex:form>
  </center>
</apex:page>


Apex Class:
public  class TestPage {

   public String password { get; set; }
    public String username { get; set; }
    
      public PageReference login() 
      {
      
      if (username == null)

      {
        PageReference newPage = new PageReference('/apex/ThankYou');
        newPage.setRedirect(true);
        
        return newPage;
       }
    
    
    else
    {

    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));

    return null;
    }


   
   }
}


I want to write a test class for that login Page. I dont know how to write test class.Please help me to reach out to that target.


Regards,
M.D.Luvis.
Best Answer chosen by Mecrin Luvis 10
Mecrin Luvis 10Mecrin Luvis 10
Hi,
I got the
Error: Compile Error: Illegal modifier on local variable at line 6 column 21
For This,
@isTest
public class test1
{
public static testMethod void testExample()
{
public String password;
    public String username;
    TestPage tp=new TestPage();
    tp.login();
}
}

I resolved that, We should not define the acess modifire inside  of test method.
@isTest
public class test1
{
public static testMethod void testExample()
{
      String password;
      String username;
      TestPage tp=new TestPage();
       tp.login();
}
}
 

All Answers

srinu vassrinu vas
Hi Mecrin Luvis 10,

try below code......it may help u..


@isTest
public class test1
{
public static testMethod void testExample()
{
public String password;
    public String username;
    TestPage tp=new TestPage();
    tp.login()
}
}

 
Mecrin Luvis 10Mecrin Luvis 10
Hi ,

I got this Error.

[Error] Error: Compile Error: expecting a semi-colon, found '}' at line 10 column 0

Regard,
Mecrin.
srinu vassrinu vas
Hi Please replace tp.login() with tp.login();
Mecrin Luvis 10Mecrin Luvis 10
Hi,
 I have replaced that. But I gor the same Error for that.

Error: Compile Error: Illegal modifier on local variable at line 6 column 15
@isTest
public class test1
{
public static testMethod void testExample()
{
public String password;
    public String username;
    TestPage tp=new TestPage();
    tp.login();
}
}
Mecrin Luvis 10Mecrin Luvis 10
Hi,
I got the
Error: Compile Error: Illegal modifier on local variable at line 6 column 21
For This,
@isTest
public class test1
{
public static testMethod void testExample()
{
public String password;
    public String username;
    TestPage tp=new TestPage();
    tp.login();
}
}

I resolved that, We should not define the acess modifire inside  of test method.
@isTest
public class test1
{
public static testMethod void testExample()
{
      String password;
      String username;
      TestPage tp=new TestPage();
       tp.login();
}
}
 
This was selected as the best answer