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
Callum WhitehouseCallum Whitehouse 

Help needed in creating simple test class

I've tried to complete the Trailhead for Apex test classes however I've not been able to apply the knowledge to our specific scenario. For our Salesforce Community, external users will have access to some or all of our marketing toolkits. This access is dependent on whether they have a certain field checked as true in their profile (e.g. BiRetix_Toolkit__c).

Apex class to test:

public class FileRepositoryController {
    @AuraEnabled
    
       public static List<Contact> getItems() {
        List<Contact> TK = [SELECT BiRetix_Toolkit__c, AesthetiCare_Cleansers_Toolkit__c, 
                            EDS_Toolkit__c, Endocare_CELLPRO_Toolkit__c, Endocare_Concentrates_Toolkit__c,
                            Endocare_Growth_Factor_Facials__c, Endocare_Tensage_Toolkit__c, Endocare_C_Ferulic_Toolkit__c,
                            ENDYMED_Toolkit__c, G_T_Peels_Toolkit__c, Heliocare_360_Toolkit__c, NeoRettin_Toolkit__c, 
                            NeoRetin_Rejuvemax_Toolkit__c, Retriderm_Toolkit__c FROM Contact WHERE AccountId IN 
                            (SELECT AccountId FROM User WHERE username=:UserInfo.getUsername())];
        return TK;
    }
}

Would someone be able to assist in helping me write a correct test class please?
Best Answer chosen by Callum Whitehouse
Hemant_SoniHemant_Soni
I got the point Please below one this time you will get complete coverage.
Id p = [select id from profile where name='Customer Community Login User'].id;
        
        Account ac = new Account(name ='Test') ;
        insert ac; 
        
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
        
        User user = new User(alias = 'test', email='test123@Test.com', lastname='Testing', languagelocalekey='en_US',
                             localesidkey='en_US', profileid = p, country='United States',IsActive =true,ContactId=con.Id,
                             timezonesidkey='America/Los_Angeles', username='tester@noemail.com',EmailEncodingKey='ISO-8859-1');
        
        insert user;
        system.runAs(user) {
            FileRepositoryController.getItems();
        }
Sorry for Attampts.
Thanks
 

All Answers

Hemant_SoniHemant_Soni
Hi,
Please Try Below Code.
Id p = [select id from profile where name='System Administrator'].id;
	
	Account ac = new Account(name ='Test') ;
	insert ac; 
   
	User user = new User(alias = 'HemantTest', email='test123@Test.com', lastname='Testing', languagelocalekey='en_US',
			localesidkey='en_US', profileid = p, country='United States',IsActive =true,
			timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
   
	insert user;
	system.runAs(user) {
		FileRepositoryController.getItems();
	}
Please let me know if you are getting any issue.
Thanks
Hemant
 
Callum WhitehouseCallum Whitehouse
Hi Hemant,

I've tried the following code however the code coverage is 0%, 0/4 lines.
 
@isTest
private class FileRepositoryControllerTest {
    @isTest static void testFileRepository() {
        Id p = [select id from profile where name='System Administrator'].id;
        
        Account ac = new Account(name ='Test') ;
        insert ac; 
       
        User user = new User(alias = 'HemantTest', email='test123@Test.com', lastname='Testing', languagelocalekey='en_US',
                localesidkey='en_US', profileid = p, country='United States',IsActive =true,
                timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
       
        insert user;
        system.runAs(user) {
            FileRepositoryController.getItems();
        } 
    }   
}

Any corrections would be much appreciated, thanks.
Hemant_SoniHemant_Soni
Try Below one
Id p = [select id from profile where name='System Administrator'].id;
	
	Account ac = new Account(name ='Test') ;
	insert ac; 
   
	User user = new User(alias = 'HemantTest', email='test123@Test.com', lastname='Testing', languagelocalekey='en_US',
			localesidkey='en_US', profileid = p, country='United States',IsActive =true,AccountId=ac.Id,
			timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
   
	insert user;
	system.runAs(user) {
		FileRepositoryController.getItems();
	}

 
Callum WhitehouseCallum Whitehouse
This produced the following error:
Field is not writeable: User.AccountId
Hemant_SoniHemant_Soni
Id p = [select id from profile where name='System Administrator'].id;
	
	Account ac = new Account(name ='Test') ;
	insert ac; 
	
	Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
   
	User user = new User(alias = 'HemantTest', email='test123@Test.com', lastname='Testing', languagelocalekey='en_US',
			localesidkey='en_US', profileid = p, country='United States',IsActive =true,ContactId=con.Id,
			timezonesidkey='America/Los_Angeles', username='tester@noemail.com');
   
	insert user;
	system.runAs(user) {
		FileRepositoryController.getItems();
	}
Use This one, if still you are getting error then let me know.
Thanks
Hemant
 
Callum WhitehouseCallum Whitehouse
Error is fixed but code coverage is still 0%
Hemant_SoniHemant_Soni
I got the point Please below one this time you will get complete coverage.
Id p = [select id from profile where name='Customer Community Login User'].id;
        
        Account ac = new Account(name ='Test') ;
        insert ac; 
        
        Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
        insert con;  
        
        User user = new User(alias = 'test', email='test123@Test.com', lastname='Testing', languagelocalekey='en_US',
                             localesidkey='en_US', profileid = p, country='United States',IsActive =true,ContactId=con.Id,
                             timezonesidkey='America/Los_Angeles', username='tester@noemail.com',EmailEncodingKey='ISO-8859-1');
        
        insert user;
        system.runAs(user) {
            FileRepositoryController.getItems();
        }
Sorry for Attampts.
Thanks
 
This was selected as the best answer
Callum WhitehouseCallum Whitehouse
Perfect it has worked, thanks for all of your time and effort!
Hemant_SoniHemant_Soni
If you want More Salesforce Support In Future You Can Contact US On "http://bertechllc.com".
Please mark this solved As well.
Thanks