+ Start a Discussion

test class for method called by js button

Hi folks
I have a method called by a simple js button. 
1) How do I write a test class to simulate clicking the button?
2) In surfing the web there appears to be a question as to whether test coverage is necessary? If it isn't doesn't that count against code coverage?

Simple button script is below from which you can see the name of the method (All works fine. Just need to sort out test coverage question).
var result = sforce.apex.execute("PubVersionNumber","buttonVersionNumber",{}); 
alert("App is Published." ); 

James LoghryJames Loghry
1.  For unit tests, you're not simulating the actual clicking of the button, but rather that the code underneath the button performs as expected.  In otherwords, you would write your test to execute the "PubVersionNumber / buttonVersionNumber" method.  If you define parameters for the method, then you should test for each possible combination of the parameters as well.  In your case though, you're looking at like a single unit test method for the buttonVersionMember call.

Your test method / class might look like the following:
private class PubVersionNumberTest{
    static testmethod void testButtonVersionNumber(){
         //Call your button version number method.

        //Use System.assert, System.assertEquals, System.assertNotEquals to validate your method is working as expected.

2)  Test code coverage is absolutely necessary.  Not only is it an excellent idea to unit test your application, but somewhat unique to Salesforce, you have to maintain 75% code coverage when you deploy your code to production.  Ideally that number should be close to 100%, but you cannot even deploy without at least 75.  Furthermore, you need code coverage across each trigger to deploy as well.
Thanks for the quick answer. Sorry to be an ignorant newbie still. My method simply creates a child record when the button is clicked. THere is no particular condition (other than a personal decision 'I want to publish') - so I'm look for a way to say this in my test (written in pseudo code)
Test.startTest ();
execute buttonVersionNumber(); (using my factory below)
System.assertEquals.... does child record exist with proper values etc.

The other question I have is that I'd like to insert records from a testfactory, so I started my test like this
@isTest public class PubVersionNumberTest {
    testMethod static void publishAppMaster() { 
        App_Master__c testAppMaster = PubVersionTestFactory.buildTestAppMaster(1);
        insert testAppMaster;