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
AyanHoreAyanHore 

Apex test class for different profiles

Hi,

I'm currently working on an Apex test class. However, the controller I'm trying to test is divided into several sections, each meant to be for diffferent profiles.
As a result, I've to test each profile using a single runAs() method for each profile users, but those were only able to test parts of the controller and not completely.
My test class has different methods for each profiles and they cover roughly 25-30 % of the code, so it is nowhere near 75% (let alone 100%).

I've tried to club different runAs() method calls in a single test method, but I'm hitting the maximum number of SOQL limit of 100.

Right now I'm stuck with 40% code coverage of my controller. Any help/suggestion will be very welcome..!!

Thanks,
~Ayan
Best Answer chosen by AyanHore
Daniel B ProbertDaniel B Probert
hey just so long as you know the names of the profiles you just need to do something like this:

@isTest
public class Test_Pageasdifferentprofiles{
    public static testMethod void TestProfileType1(){
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        
        User u1 = new User(Alias = 'standt1',Country='United Kingdom',Email='demo1@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='dprobertdemo1@camfed.org');
        insert u1;
        
        // now insert your test data

       System.runAs(u1){
            // you test for your controller
        }
 public static testMethod void TestProfileType2(){
        Profile p = [SELECT Id FROM Profile WHERE Name='Standard User'];
        
        User u1 = new User(Alias = 'standt1',Country='United Kingdom',Email='demo1@randomdemodomain.com',EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',LocaleSidKey='en_US',ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='dprobertdemo1@camfed.org');
        insert u1;
        
        // now insert your test data

       System.runAs(u1){
            // you test for your controller
        }
}

if that's right then mark this is as the best answer - cheers dan