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
Rakesh SamalRakesh Samal 

How to write test class for soql query ??

This is my apex class: Anyone pls help. I am new to test classes

public class StudentMasterClass {
    public static list<Student_Master__c> StudentMasterList {get;set;}
    
    public StudentMasterClass()
    {
        StudentMasterList = [SELECT Name,Last_Name__c,Company__c,City__c,Phone__c FROM Student_Master__c];
    }
}
SwethaSwetha (Salesforce Developers) 
HI Rakesh,
Try below code to cover the above class
 
@istest
public class testClassForTrigger{
     static testmethod void triggerTest(){
        Student_Master__c sm = new Student_Master__c();
         sm.name=‘test’;
         insert sm; //Insert other fields Last_Name__c,Company__c,City__c,Phone__c  data here 
         StudentMasterClass sc= new StudentMasterClass();
    }
}

If this information helps, please mark the answer as best. Thank you
Rakesh SamalRakesh Samal
Hi Swetha,
I am written this one only. But after running the test it  is showing nothing in apex class.
SwethaSwetha (Salesforce Developers) 
@Rakesh,
Do you mean the code coverage in the apex class? Which lines aren't being covered. Thanks
Rakesh SamalRakesh Samal
User-added image

Look there is nothing
SwethaSwetha (Salesforce Developers) 
Strange. This code should be giving you 100% coverage. I checked in my colleague's machine with the same code and it gave 0% coverage initially. After running the test multiple times, it gave 100% coverage. 

Can you logout,re- login and try to run test class and see if it fixes the issue?
Thank you
Rakesh SamalRakesh Samal
Haha. I am doing everything regard this. But not working. 
SwethaSwetha (Salesforce Developers) 
HI Rakesh,
If you are still seeing the same behaviour, can you try "compile all classes". 
Click path: Setup> Apex classes> Compile all classes link

Related: https://salesforce.stackexchange.com/questions/149949/compile-all-classes

If this information helps, please mark the answer as best. Thank you
Rakesh SamalRakesh Samal
Hello Swetha,
Thanks for ur help. I already cleared that test class. There are many things which I wrote in those test class.