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
Taz BoparaiTaz Boparai 

help writing simple test class for SOQL Query controller

simple controller with a SOQL query returning data. Need help writing the test class for this! An admin with minimal development experience...

 
public with sharing class CommunitiesNotificationsList {
@AuraEnabled
    
public static List <Communities__c> notifications(String Name, String Position){

        return [SELECT Full_Description__c, Related_Product__c FROM Communities__c 
                WHERE RecordType.Name = 'Notification' AND Related_Product__c =: Name LIMIT 1];
        
}
}

 
$hwet@$hwet@
Hi Taz,

Please find below the test class:

@isTest
private class CommunitiesNotificationsListTest{
     static testmethod void TestRecordCreate(){
         Id devRecordTypeId = Schema.SObjectType.Communities__c.getRecordTypeInfosByName().get('Notification').getRecordTypeId();
         Communities__c com = new Communities__c(Full_Description__c='test',name='testtest',Related_Product__c='testtestew',RecordTypeid=devRecordTypeId );
         insert com ;
         CommunitiesNotificationsList.notifications('testtestew','tttt');
     }
}
 
Ajay K DubediAjay K Dubedi
Hi Taz Boparai,
Try this code for test class:

Id RecordTypeIdContact = Schema.SObjectType.Communities__c.getRecordTypeInfosByName('Notification').get('RecordTypeContactClientOnly').getRecordTypeId();
Communities__c comObj = new Communities__c();
comObj.Name = 'Test';
comObj.recordtypeid = RecordTypeIdContact;
//All required field.
insert comObj;
CommunitiesNotificationsList.notifications('Test', 'Any Position');

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi