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
sgsssgss 

Test class reqires for soql urgent

SOQL query to find all account records where billing state is not tokyo and california. Order the result by billing state in descending order with null value at the end.Display first 1000 record only

///Can anyone provide Test Class for following code?

public with sharing class BillingStateClass {
    public void checkBillingState(){
        List<Account> listAccount = [SELECT 
                                        Id,
                                            Name 
                                    FROM 
                                        Account
                                    WHERE 
                                        Account.BillingState 
                                    NOT IN 
                                        ('tokyo','california') 
                                    ORDER BY 
                                        Account.BillingState DESC
                                    NULLS 
                                        LAST 
                                    LIMIT 
                                        10000];
        System.debug(listAccount);
    }    
}
mritzimritzi
@isTest
public class BillingStateClassTest{
    @isTest public static void test_checkBillingState(){
        List<Account> accountList = new List<Account>();
        for(Integer i=0; i<100; i++){
            Account account = new Account(
                Name = 'Test Account ' + i,
                BillingState = 'Arizona',
                BillingCountry = 'USA'
                //add more fields, if required
            );
            accountList.add(account);
        }
        //if other fields are mandatory in your org, or required by some validation rules, assign values for those fields as well before inserting data 
        insert accountList;
        
        BillingStateClass billingStateClass = new BillingStateClass();
        Test.startTest();
        billingStateClass.checkBillingState();
        Test.stopTest();
        //since the function neither return anything nor does it changes any record, no assert is required.
    }
}

please mark this as BEST ANSWER, if this solves your problem
Ajay K DubediAjay K Dubedi
Hi Supriya,
Hope this will help you & please remember to use system.assertEquals always because it verifies your test classes
 
@isTest
public class BillingStateClass_Test {
   
    @isTest static void checkBillingState_Test(){
        List<Account> accountList = new List<Account>();
        for(Integer i=0; i<50; i++){
            Account accObj = new Account();          
            accObj.Name = 'Test' + i;
            accObj.BillingState = 'Delhi'; 
            accObj.BillingCountry  = 'India';
            accountList.add(accObj);
        }
        insert accountList;
        
        test.startTest();
        BillingStateClass.checkBillingState();  // called this way as the function is static
        system.assertEquals(50, accountList.size());
        test.stopTest();
        
    }    
}
Please mark it as Best Answer if it helps you.

Thank You
Ajay Dubedi
sgsssgss
Thanks

and can you please mention what changes are required if the method returns value.

public with sharing class BillingStateClass {
    public static List<Account> checkBillingState(){
        List<Account> listAccount = [SELECT 
                                        Id,
                                            Name 
                                    FROM 
                                        Account
                                    WHERE 
                                        Account.BillingState 
                                    NOT IN 
                                        ('maharashtra','Kerala') 
                                    ORDER BY 
                                        Account.BillingState DESC
                                    NULLS 
                                        LAST 
                                    LIMIT 
                                        10000];
        System.debug(listAccount);
        return listAccount;
    }    
}
mritzimritzi
@isTest
public class BillingStateClassTest{
    @isTest public static void test_checkBillingState(){
        List<Account> accountList = new List<Account>();
        for(Integer i=0; i<100; i++){
            Account account = new Account(
                Name = 'Test Account ' + i,
                BillingState = 'Arizona',
                BillingCountry = 'USA'
                //add more fields, if required
            );
            accountList.add(account);
        }
        //if other fields are mandatory in your org, or required by some validation rules, assign values for those fields as well before inserting data 
        insert accountList;
        
        BillingStateClass billingStateClass = new BillingStateClass();
		List<Account> listAccount;
        Test.startTest();
        listAccount = billingStateClass.checkBillingState();
        Test.stopTest();
        System.assertNotEquals(null, listAccount);
    }
}
You have to collect the data being returned in a variable and use assertEquals/assertNotEquals (as per use case) after stopTest.

please mark this as BEST ANSWER, if this solves your problem
 
Akshay_DhimanAkshay_Dhiman
Hi Supriya,

Try the below code.

List<Account> accList=new List<Account>();
 accList=[SELECT name FROM Account WHERE BillingState!='tokyo' OR BillingState!='california' ORDER BY BillingState DESC LIMIT 1000];
System.debug(accList);

Thanks.
​Akshay