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
Dilip SinghDilip Singh 

How to write unit test for update method

Hi Guys I am new in salesforce1 need some help..
I want to write unit test for given below method.....
 @RemoteAction     global static Boolean UpdateAccount(String accountID,string cellPhone,string email)      {        Contact contact = [Select Id,Name,Email,MobilePhone  from Contact WHERE Account.Id= :accountID];        //contact.Name = name;        contact.Email = email;        contact.MobilePhone = cellPhone;        update contact;                   //Account account= [Select  Id,Name, BillingStreet,Geo_Location__Latitude__s,Geo_Location__Longitude__s from Account WHERE Id= :accountID];        //account.Status= status;               //update account;         return true;     }

I have try as bellow but did not get success

@isTest
private class TestAccountDetailController  {
    
    public Static testMethod void Test_UpdateAccount()
    { 
        Boolean bStr = AccountDetailController.UpdateAccount('001j000000EloztAAB','(650)867-23223','dilip@droisys.com');
      
       // List<Contact> Actual_lst=[Select Id,Email,MobilePhone From Contact Where AccountId='001j000000EloztAAB'];
        System.assertEquals([Select Id,Email,MobilePhone From Contact Where AccountId='001j000000EloztAAB'].Id,'003j0000008CPswAAG');
        
    }
}
Error:::::
Time Started 3/13/2015 4:46 AM
Class TestAccountDetailController
Method Name Test_UpdateAccount
Pass/Fail Fail
Error Message System.QueryException: List has no rows for assignment to SObject
Stack Trace Class.AccountDetailController.UpdateAccount: line 31, column 1
Class.TestAccountDetailController.Test_UpdateAccount: line 19, column 1

Waqar Hussain SFWaqar Hussain SF
http://help.salesforce.com/HTViewSolution?id=000159853&language=en_US (http://help.salesforce.com/HTViewSolution?id=000159853&language=en_US)
David ZhuDavid Zhu

Basically, you need to build test data at the begining of the test method.
Then run the static method.
Code snipet below for your reference.

 public Static testMethod void Test_UpdateAccount()
    { 
       Account a = new Account(....);
       insert a;
       Contact c = new Contact(accountid=a.id, .....);
       insert c;        

        Boolean bStr = AccountDetailController.UpdateAccount(a.id,'(650)867-23223','dilip@droisys.com');
      
       // List<Contact> Actual_lst=[Select Id,Email,MobilePhone From Contact Where AccountId='001j000000EloztAAB'];
        System.assertEquals([Select Id,Email,MobilePhone From Contact Where AccountId=:a.id].Id,'003j0000008CPswAAG');
        
    }