+ Start a Discussion

How to write testMethod for Remoting method?

Hi All,


I wanted to write a test method for my Javascript Remoting controller.

global class remoteTest {
global static Contact[] findContacts(string Name) {
Name = '%'+Name+'%';
Contact[] c = [SELECT ID, Name, Phone, Email from Contact where NAME LIKE :Name ];
return c;

 It would be appreciative if any one could assist regarding this, thanks in advance.





You would perform the test the same as any other:


global class globalTest {
    global static Contact[] findContacts(String name) {
        return [select id,firstname,lastname,email from contact where name = :name];
    global static testMethod void testClass() {
        Contact c = new Contact(FirstName='▼',LastName='▼');
        insert c;
        Contact[] cc = globalTest.findContacts('▼ ▼');
        System.assertEquals(c.id,cc[0].id,'Failed assertion');

It's normal Apex Code, and as such, it can be covered like any other Apex Code. Note that as a matter of simplification of the test, the name of the contact was set to something incredibly unlikely to found in any database (▼ is the control character U+001F).