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
sruthi.458sruthi.458 

Test class

Hi Friends,

 

 If iam trying to write a test code for Apex class it will not save because of the class name already exists..

      Example:

public class myclass
{
    account a;
    public account getAcc() 
    {
     a=[select id,name from account where id=:apexpages.currentpage().getparameters().get('id')];
        return a;
    }
    public PageReference del() 
    {
       delete a;
        return null;
    }


    public PageReference save()
     {
        return null;
    }

 

 

 

How can i write a test code for the above Apex class....?

 

 

Regards,

Srikanth

yvk431yvk431
@istest

public class testMyClass

{

        public stratic testmethod void testmethod1()

        {
              //create an account record
Account Acc= new Account(Name = 'Test Account',assign other mandatory fields);
Insert Acc;
myclass mc = new myclass();
  //set you page reference
PageReference pageRef = Page.yourpage;
Test.setCurrentPage(pageRef);
ApexPages.currentPage().getParameters().put('id', Acc.Id);
Account A = mc.getAcc();
PageReference pg = mc.del();
PageReference pg2 = mc.Save();
 } }

 

--yvk

sruthi.458sruthi.458

Hi Bro,

 

   The Apex class name and test class name are different. But how the test code matches to apex code. and test code coverage is greater than 75% but Apex code coverage is 0%...  

 

how to match that test code to Apex code?

 

Regards,

Srikanth.

yvk431yvk431

I guess you didnt know whats a test class is for ,  read before you do any thing .

 

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

 

 

-- yvk

sruthi.458sruthi.458

Bro,

 

 I know about test classes but iam having a small doubts on Apex test code..and i know how to write a test code for triggers, Apex class. 

 

     My doubt is we are writing a test code for Apex class if i mention a same class name in test class  it shows an error like the class name already exists.. If we are using with different class name the test code is successful and when i run the code the test code is 100% but the Apex code is 0%.

 

 

Regards,

Srikanth 

 

 

Apex code



yvk431yvk431

I dont know what you are referring to ' test code is 100%' .

 

Test controller do not have any coverage, as all the controllers code will be excluded from the organisation code limit.

After you run the test method, the original class will get displayed in the list of controllers with its code coverage with a link, if you click on the it will display the original controller in an floating window with all the code covered in blue uncovered in red.

 

 

--yvk