+ Start a Discussion
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12 

test class for method called by js button

Hi folks
I have a method called by a simple js button. 
Questions:
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).
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} 
var result = sforce.apex.execute("PubVersionNumber","buttonVersionNumber",{}); 
alert("App is Published." ); 
window.location.reload();

 
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:
 
@isTest
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.
jonathanbernddf20131.387825590468351E12jonathanbernddf20131.387825590468351E12
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)
Test.stopTest();
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;
    
  }
}