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
bbradybbrady 

Unable to create testMethods in Eclipse

I'm using the Execute Anonymous window in Eclipse (3.3.2) in an attempt to test a testMethod.  I get the following error: "Only top-level class methods can be declared static."

 

 

public class myClass {
public static testMethod void myTest() {
system.debug('test');
}
}

 

When I use the Force.com builder to add this class, it compiles and runs just fine. Does 'Execute Anonymous' break with classes that contain testMethod?!

 

I suspect I may be missing something obvious.

 

Thanks,

Bill

 

Best Answer chosen by Admin (Salesforce Developers) 
JonPJonP

I found this under "Anonymous Blocks" in the Apex developer's guide, which explains the behavior you're seeing:

 

The content of an anonymous block (the code String that is passed into executeAnonymous()) can include user-defined methods and exceptions. It cannot include the keyword static. You do not have to manually commit any database changes made by an anonymous block. If your Apex script completes successfully, any database changes are automatically committed. If your Apex script does not complete successfully, any changes made to the database are rolled back.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_anonymous_block.htm|SkinName=webhelp

All Answers

JonPJonP

Why are you trying to use the Execute Anonymous window for defining a class or calling a test method?  I don't think you can define a class inside anonymous Apex, and there are better ways to invoke a test method within the Force.com IDE.

 

To create a new class, create a Force.com Project and use the File > New > Apex Class command to launch the New Apex Class wizard.

 

To run the test methods in a class, right-click on the .cls file in the Project Explorer and choose Force.com > Run Tests.  Or you can perform the same action on the "{project}/src/classes" folder to run the test methods in all the classes in your project.

bbradybbrady

Thanks for the quick reply, Jon.

 

I create classes using the path you describe.

 

I don't use Execute Anonymous to 'create' or 'test' a class but to experiment with the code until I get the logic right, before I deploy-to-server in my developer org. I've written a number of classes but I am only just now learning to write the testMethods. I understand the recommended approach is to write the testMethods in tandem with the classes/methods but I wanted to be sure I was going to keep the code before writing the testMethods for it.

 

I'm guessing that my use of Execute Anonymous sounds a bit unorthodox, and I may be using it for a purpose other than originally intended. However it does let me declare a class, create an instance of it, experiment with its methods. etc. - just not for testMethods it seems. Maybe its not supposed to.

 

 

JonPJonP

I found this under "Anonymous Blocks" in the Apex developer's guide, which explains the behavior you're seeing:

 

The content of an anonymous block (the code String that is passed into executeAnonymous()) can include user-defined methods and exceptions. It cannot include the keyword static. You do not have to manually commit any database changes made by an anonymous block. If your Apex script completes successfully, any database changes are automatically committed. If your Apex script does not complete successfully, any changes made to the database are rolled back.

 

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_anonymous_block.htm|SkinName=webhelp

This was selected as the best answer
bbradybbrady

That it does!

Thanks a ton!