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
Venkateswarlu PVenkateswarlu P 

Test Class for SOQL Select Query

public class SOQL_1 {
    public List<Account> accList     {set;get;}
    //Contructor
    public SOQL_1(){
        accList=[SELECT Name,Phone,Industry,Rating from Account];
    }
}
-----------------------------
Test Class
-----------------------------
@isTest
private class Test_Class_3 {	
	@isTest
    static void accList(){
        SOQL_1 s1=new SOQL_1();
        Account a = new Account();
        a.name='Ravi';
        a.phone='12345';
        insert a;
        
        Account a2=[Select Id,Name,Phone from Account where name='Ravi' LIMIT 1];
        System.assert(a2!=null);        
    }    
}
Is this Correct.
 
Best Answer chosen by Venkateswarlu P
Harish RamachandruniHarish Ramachandruni
Hi,


Can you change line number 4 in test class :


    static testMethod void accList(){


// this is " testMethod" required if test class is running all methods is not going to run only "testMethod" will run.

https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro
 
@isTest
private class Test_Class_3 {	
	@isTest
    static testMethod void accList(){
        SOQL_1 s1=new SOQL_1();
        Account a = new Account();
        a.name='Ravi';
        a.phone='12345';
        insert a;
        
        Account a2=[Select Id,Name,Phone from Account where name='Ravi' LIMIT 1];
        System.assert(a2!=null);        
    }    
}






Thanks,
Harish R.

All Answers

Steven NsubugaSteven Nsubuga
Close, you need to create the test data before you run the test.
@isTest
private class Test_Class_3 {	
	@isTest
    static void accList(){

        Account a = new Account();
        a.name='Ravi';
        a.phone='12345';
        insert a;

        Test.startTest();

        SOQL_1 s1=new SOQL_1();
        Account a2=[Select Id,Name,Phone from Account where name='Ravi' LIMIT 1];

        Test.stopTest();
        
        System.assert(a2!=null);        
    }    
}

 
Harish RamachandruniHarish Ramachandruni
Hi,


Can you change line number 4 in test class :


    static testMethod void accList(){


// this is " testMethod" required if test class is running all methods is not going to run only "testMethod" will run.

https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro
 
@isTest
private class Test_Class_3 {	
	@isTest
    static testMethod void accList(){
        SOQL_1 s1=new SOQL_1();
        Account a = new Account();
        a.name='Ravi';
        a.phone='12345';
        insert a;
        
        Account a2=[Select Id,Name,Phone from Account where name='Ravi' LIMIT 1];
        System.assert(a2!=null);        
    }    
}






Thanks,
Harish R.
This was selected as the best answer