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
nashnanashna 

Can someone please help me on how to write test class for the code below

public class CC {
@AuraEnabled
    public static Contact UpdateCC(Id recId) {
        if(recId==null){
            return null;
        }
        Contact c=[Select Id,checkbox__c FROM Contact WHERE Id=:recId LIMIT 1];
        c.checkbox__c =c.checkbox__c ==false ? true : false;
        update c;
        system.debug('cnt** '+cnt);
        return c;
    }

}
Best Answer chosen by nashna
Agustin BAgustin B
Hi Nashna, do something like this (put your class as global or with static:
@IsTest
public void test(){
        
        Contact c = new Contact(
          LastName='test contact',checkbox__c = false);
        insert c;

Contact newC = CC.UpdateCC(c.Id);
system.assert(newC.checkbox__c);
}


If it helps you please mark this answer as correct, it may help others

All Answers

Agustin BAgustin B
Hi Nashna, do something like this (put your class as global or with static:
@IsTest
public void test(){
        
        Contact c = new Contact(
          LastName='test contact',checkbox__c = false);
        insert c;

Contact newC = CC.UpdateCC(c.Id);
system.assert(newC.checkbox__c);
}


If it helps you please mark this answer as correct, it may help others
This was selected as the best answer
MoonpieMoonpie
Hi nashna,

Start off by asking yourself, "What is this code supposed to do?"

Is it supposed to create new records?
Is it supposed to send a message?
Is it supposed to update a field?
etc.

Often for triggers, the code is supposed to prevent something from happening, so you have to be mindful of that.

If the code is supposed to update a field, as it seems your code is, then you need to ask yourself, "What are the conditions that cause this field to be updated?" AND don't forget to ask yourself, "What are the (or some other) conditions that would cause this field to properly not be updated?"

You'll want to write test methods that meet these conditions.

----
Let's walk through your code:
public static Contact UpdateCC(Id recId)
Obviously this method takes one Id, so you'll need to call this method from your test class (more than once, but let's take it slowly) and pass it a Contact Id.

(NOTE that you have no code checking whether the Id passed in is, indeed, a Contact Id.  So you might want to consider adding that check, or changing your code to take a Contact record as a parameter.  But you can skip that for now as we are trying to get your testing class up and running.)

That also means that somewhere in your test class you will need to create some test Contact records to use.
 
if(recId==null){
    return null;
}
So you are doing a check just in case a null Id argument is passed.  That means one test method (or part of one) will be to purposefully create a null Id variable and pass it into this code; then check for and assert that the result returned to your test method is actually null.
 
Contact c=[Select Id,checkbox__c FROM Contact WHERE Id=:recId LIMIT 1];
Ok, so you are doing a SOQL query, so this means that not only in your test class will you need to create test Contact records, you also need to insert some.
And since you are querying the checkbox__c field, you will want to create and insert some with the checkbox value as true and some as false, to insure that you are fully testing.

(NOTE that your code here could fail/throw an error if you do pass in a non-Contact Id.  Again, this is outside of creating your test class & methods right now - I just wanted to point that out.
 
c.checkbox__c =c.checkbox__c ==false ? true : false;
update c;
Here you are essentially just "flipping" the value of your checkbox field to the opposite of what it was - regardless of what value it originally was - and writing that back to the database.

This means that your test method(s) will need to send the Id of an inserted Contact record where you know the checkbox__c value is false, and then in the test method do a SOQL query for that specific record and verify and assert that the checkbox__c value is now true.
Then reverse that and also do the same for originally true changing to false.


(We are not worried about the System.debug() lines.)
 
return c;
Since this method has a return, in those test methods you not only want to do the SOQL queries, but you also want to verify and assert that the checkbox__c value in the returned Contact record has also been flipped - both from true to false as well as false to true.

Also, make sure that even though your code only accepts one record at a time, in your test class you should create and insert a number of records, and then call your code in a loop to send those Contact Ids in rapid fire to your code.

---
That should get you far!

If you are unsure how to begin writing a test class and test methods, take a look at the Tesing Apex section of the Apex Developer Guide (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm).
Especially note the What Are Apex Unit Tests?, Testing Best Practices and Testing Example sub-sections.

Or better yet, start with the Apex Testing (https://trailhead.salesforce.com/en/content/learn/modules/apex_testing) module on Trailhead - it's a good introduction.
MoonpieMoonpie
Oh - and of course, once you've given it a try, let us know how it is working and if you have any issues that you need specific help with!
nashnanashna
@moonpie Thank you for the reply, I have already written the test class but i could not firgure how to call the class and its method to update the record