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
Sam WardSam Ward 

How do I write a test class for updating a record

I'm new to apex code and I've got everything working its just the test class i'm struggling on. 

Here is my Apex Class
public class ramassistAPI {

@future  (callout=true)
    public static void updateopp() {
    
        DVLA_Lookup_Opportunity__c DVLA = [SELECT id
                                          FROM
                                          DVLA_Lookup_Opportunity__c
                                          WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Pending'];
        DVLA.SentToAPI__c = 'Sent';
        update DVLA;
    }
}

My test class is:
@isTest
private class RamassistApiTest {
    static testMethod void TestOppUpdating() {
        
        Account acct = new Account(Name='Test');     
        insert acct;
        
        Contact con = new Contact(LastName = 'Bloggs',FirstName = 'Joe',Phone = '01234567890',Email = 'test@test.com',AccountId = acct.Id);
        insert con;
        
        DVLA_Lookup_Opportunity__c dvlaOpp = new DVLA_Lookup_Opportunity__c(Account__c = acct.Id, Stage__c = 'Closed Won',Close_Date__c = System.today(),SentToAPI__c = 'Pending',Contact__c = con.Id);            
        insert dvlaOpp;
        
        dvlaOpp = [SELECT Id, SentToAPI__c FROM DVLA_Lookup_Opportunity__c WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Sent' AND Id =: dvlaopp.Id];         
        dvlaOpp.SentToAPI__c = 'Sent';
        update dvlaOpp;   	
               
        // Do the test
        test.startTest();
        System.assertEquals('Sent', dvlaOpp.SentToAPI__c);
        test.stopTest();
    }     
}

Can anyone give me some advice to where i might be going wrong? 

Thanks
 
CharuDuttCharuDutt
Hii Sam
Try Below Code
@isTest
private class RamassistApiTest {
    static testMethod void TestOppUpdating() {
        
        Account acct = new Account(Name='Test');     
        insert acct;
        
        Contact con = new Contact(LastName = 'Bloggs',FirstName = 'Joe',Phone = '01234567890',Email = 'test@test.com',AccountId = acct.Id);
        insert con;
        
        DVLA_Lookup_Opportunity__c dvlaOpp = new DVLA_Lookup_Opportunity__c(Account__c = acct.Id, Stage__c = 'Closed Won',Close_Date__c = System.today(),SentToAPI__c = 'Pending',Contact__c = con.Id);            
        insert dvlaOpp;
        
        dvlaOpp = [SELECT Id, SentToAPI__c FROM DVLA_Lookup_Opportunity__c WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Sent' AND Id =: dvlaopp.Id];         
        dvlaOpp.SentToAPI__c = 'Sent';
        update dvlaOpp;   	
               
        // Do the test
        test.startTest();
        ramassistAPI.updateopp();
        System.assertEquals('Sent', dvlaOpp.SentToAPI__c);
        test.stopTest();
    }     
}
Please Mark It As Best Answer If It Helps
Thank You!

 
Sam WardSam Ward
Thanks for the response but it doesn't seem to work still. I left the trigger out on my previous post, check updated still not working:

Apex Class
public class ramassistAPI {
    
    @future  (callout=true) 
    public static void makePostCallout() {
        
        string sfid = [Select Account_Id__c FROM DVLA_Lookup_Opportunity__c where Stage__c = 'Closed Won' AND SentToAPI__c = 'Pending'].Account_Id__c;
        string data = '{'
    + '"token":"123", '
    +  '"sfid":' 
    +  '"' + sfid + '"' +
  '}';
        
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.com');
        request.setMethod('POST');
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody(data);
        Http.send(request);
        HttpResponse response = Http.send(request);
        // Parse the JSON response
         if (response.getStatusCode() != 201) {  
             System.debug('Salesforce Id submitted: ' +
                sfid + 'Body Data: ' + data);
        } else {
            System.debug(response.getBody());
        }
        
    } 
    
    @future  (callout=true)
    public static void updateopp() {
    
        DVLA_Lookup_Opportunity__c DVLA = [SELECT id
                                          FROM
                                          DVLA_Lookup_Opportunity__c
                                           WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Pending'];
        DVLA.SentToAPI__c = 'Sent';
        update DVLA;
    }
}

Apex Trigger
trigger RAMAssistHTTP on DVLA_Lookup_Opportunity__c (after update) {
    for(DVLA_Lookup_Opportunity__c DVLA : Trigger.new) {
        try {
            //Try block of code
            //Perform logic here
            if(DVLA.Stage__c == 'Closed Won' && DVLA.SentToAPI__c == 'Pending') {
                ramassistAPI.makePostCallout();
                ramassistAPI.updateopp();
            }  
        } catch (Exception e) {
            // First catch block for specific exception 
            // It checks Exception in the order they are in the code
            // Exception must be the last Catch as its a general exception
            System.debug(e);
            //Perform logic to handle exception  
        }
        
    }
}

Apex TestClass
 
@isTest
private class RamassistApiTest {
    static testMethod void checkPending() {
        
        Account acct = new Account(Name='Test');     
        insert acct;
        
        Contact con = new Contact(LastName = 'Bloggs',FirstName = 'Joe',Phone = '01234567890',Email = 'test@test.com',AccountId = acct.Id);
        insert con;
        
        DVLA_Lookup_Opportunity__c dvlaOpp = new DVLA_Lookup_Opportunity__c(Account__c = acct.Id, Stage__c = 'Closed Won',Close_Date__c = System.today(),SentToAPI__c = 'Pending',Contact__c = con.Id);            
        insert dvlaOpp;
        
        Integer pending = 0;
        /*      for(dvlaOpp) {
try {
    //Try block of code
    //Perform logic here
if(dvlaOpp.Stage__c == 'Closed Won' && dvlaOpp.SentToAPI__c == 'Pending') {
System.debug('Found results');
}  
} catch (Exception e) {
System.debug(e);
}
*/        
        // Do the test
        test.startTest();
        System.assertEquals(pending, 0);
        test.stopTest();
        
    }
   
static testMethod void TestOppUpdating() {
        
        Account acct = new Account(Name='Test');     
        insert acct;
        
        Contact con = new Contact(LastName = 'Bloggs',FirstName = 'Joe',Phone = '01234567890',Email = 'test@test.com',AccountId = acct.Id);
        insert con;
        
        DVLA_Lookup_Opportunity__c dvlaOpp = new DVLA_Lookup_Opportunity__c(Account__c = acct.Id, Stage__c = 'Closed Won',Close_Date__c = System.today(),SentToAPI__c = 'Pending',Contact__c = con.Id);            
        insert dvlaOpp;
        
        dvlaOpp = [SELECT Id, SentToAPI__c FROM DVLA_Lookup_Opportunity__c WHERE Stage__c = 'Closed Won' AND SentToAPI__c = 'Sent' AND Id =: dvlaopp.Id];         
        dvlaOpp.SentToAPI__c = 'Sent';
        update dvlaOpp;   	
               
        // Do the test
        test.startTest();
        ramassistAPI.updateopp();
        System.assertEquals('Sent', dvlaOpp.SentToAPI__c);
        test.stopTest();
    } 
}

I get the below coverage 

ramassistAPI 64%
RAMassistHTTP 83%

Any advice again would be good thank you.