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
V AnandV Anand 

Compile Error: Option already set: TestMethod

I got this error  when  I including TestMethod with in the apex class like this...

 

 

public class nsorcon {
    public nsorcon(ApexPages.StandardSetController controller) {

    }

     private  list<contact> conlist;
     id uid=userinfo.getuserid();
     user u=[select id,contactid from user where id=:uid];  
     public list<contact> getcontacts(){
          
           conlist=[select ....................];
         return conlist;
    }
  @istest
  static testmethod void testmehod1(){ 
  PageReference pg = Page.mysponsors; 
  Test.setCurrentPage(pg);
  
   contact[] cc=new contact[]{
   new contact(lastname='sample'),new contact(lastname='sample1')};
       
    ApexPages.StandardsetController strcontroller = new ApexPages.StandardsetController(cc);
     
   nsorcon qcc = new nsorcon(strcontroller);
    qcc.getcontacts();
    
   }
  
}

 If I remove @istest in apex class No error. Should I use apex TestMethod in apex class directly or any other  restrictions ...

Best Answer chosen by Admin (Salesforce Developers) 
kibitzerkibitzer

First - choose either @istest or testmethod - you don't need (and can't use) both.

 

It really is common practice to put test methods in a separate class. There are architectural advantages to it as your application grows. For example: you can create shared methods to perform various test support tasks such as creating objects. If they are in a test class they can only be accessed by other test methods and they don't count as part of the code coverage requirement because they are in a test class.

 

You can also attribute all of the tests in a test class easily. For example - a test method in a standard class must use @istest(oninstall=true) if you want it to run on package installation - that matters if you're creating a package.

 

Dan

 

All Answers

jungleeejungleee

Remove the @isTest Annotation if you're creating the testmethod in the same class. I think it is better that you create a seperate test class and use the @isTest Annotation there.

 

Regards

Sam

maja madi

V AnandV Anand

Thanks for your reply...

 

My apex code is very less  and If I create separate class for testing then I have to deploy both apex class and test class in production .....so that ,I am trying to combine both apex class and test class in a single class..

Is it good for coding or any other effects?

 

 

 

jungleeejungleee

I am not a pro :) , but I dont think it should be problem..

 

Regards

Sam

maja madi

kibitzerkibitzer

First - choose either @istest or testmethod - you don't need (and can't use) both.

 

It really is common practice to put test methods in a separate class. There are architectural advantages to it as your application grows. For example: you can create shared methods to perform various test support tasks such as creating objects. If they are in a test class they can only be accessed by other test methods and they don't count as part of the code coverage requirement because they are in a test class.

 

You can also attribute all of the tests in a test class easily. For example - a test method in a standard class must use @istest(oninstall=true) if you want it to run on package installation - that matters if you're creating a package.

 

Dan

 

This was selected as the best answer
SamuelDeRyckeSamuelDeRycke

Personally I prefer to have my test code at the bottom of my class too,specially when it is not a lot of code. But you need to be aware that there is a limit on how much apex code you can have for your organisation. All test code in a non @IsTest annotated class contributes to that limit. But extracting your test logic to separate @IsTest annotated classes, will not contribute to your total apex limit. So be aware of this.

If you're going to develop larger projects and want more structure, or do test driven development it would also make more sense to have your tests in separated files for a cleaner structure. (It can be a good idea to have a different person develop and an other do the test code.)

edit: kibitzer beat me to it :D

V AnandV Anand

Thanks for your reply sdry.......I got it.