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
bhanu_prakashbhanu_prakash 

test class issue on below class

Hi have created test class for below class
Class
public class DispatcherNewCaseController {

   public DispatcherNewCaseController(ApexPages.StandardController controller) {
        this.controller = controller;
        system.debug('in controller');
        //getRedir();
    }
    
    public PageReference getRedir() {

        User u = [Select firstname, IsPortalEnabled__c From user Where Id = :UserInfo.getUserId()];
        system.debug('user first name=='+u.IsPortalEnabled__c);// added by santosh
        PageReference redir;

        if (u.IsPortalEnabled__c == false) {
           
            redir  = new PageReference('/500/e?retURL=/500/o');
            redir.getParameters().put('nooverride','1');
               }


         else {
             redir = new PageReference('/apex/casecreation');
           
        }

       redir.setRedirect(true);
       return redir ;
    }

   
     private final ApexPages.StandardController controller;
}

Testclass:
@istest
public class DispatcherNewCaseController{
static testMethod void DispatcherNewCaseController(){
      
     Account testAccount = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');
     insert testAccount;
    
    Account testAccount2 = new Account(name='Test Company Name',Billing_Account_Number__c='CONTROLLER');   
    
     DispatcherNewCaseController dc = new DispatcherNewCaseController();
     dc.acctNum='CONTROLLER';
    
     Case ca = new Case(Subject='Test Controller Acct Case');
     ca.Product ='FinancialForce Accounting';
     ca.Product Area = 'Admin Screens';
     ca.Priority = '3-Major Problem';
    
     dc.c = ca;    
     dc.submitcase();    

insert testAccount2;
 dc.submitcase();    
}
}

Iam facing issue on test class please help me to create test class on above apex class ;;



 
Best Answer chosen by bhanu_prakash
Prathyu bPrathyu b
Ok...
Try like something below. Create test user and run the tets class with that user context.

@isTest
public class DispatcherNewCaseControllerTest{
    static testMethod void DispatcherNewCaseController(){
        Profile sysProfle = [Select Id, name from Profile where name='System Administrator'];
        User newUser = new User(firstname = 'Denise',IsPortalEnabled__c= false,Username = 'martin@test.com.dev', LastName= 'Martin', Email='martin@test.com', Alias='dm',
                                CommunityNickname='dmc', TimeZoneSidKey='(GMT-07:00) Pacific Daylight Time', LocaleSidKey='English', EmailEncodingKey='General US & Western Europe (ISO-8859-1, ISO-LATIN-1)', 
                                ProfileId=sysProfle.Id, LanguageLocaleKey='EN-US');
        insert newUser;
        System.runAs(newUser){
            Account testAccount = new Account(name='Test Company Name');
            insert testAccount;
            ApexPages.StandardController sc = new ApexPages.standardController(testAccount);
            DispatcherNewCaseController dc = new DispatcherNewCaseController(sc);
            Case ca = new Case(Subject='Test Controller Acct Case');     
            ca.Priority = '3-Major Problem';
            insert ca;
            dc.getRedir();    
        }
    }
}

you may get some error on user creation. So, provide correct test data to avoid teh errors and let me know.

Thanks
Prathyusha

All Answers

Prathyu bPrathyu b
Hi Bhanu,
Just try the test class with below content and run it.

@isTest
public class DispatcherNewCaseControllerTest{
    static testMethod void DispatcherNewCaseController(){
        Account testAccount = new Account(name='Test Company Name');
        insert testAccount;
        ApexPages.StandardController sc = new ApexPages.standardController(testAccount);
        DispatcherNewCaseController dc = new DispatcherNewCaseController(sc);
        Case ca = new Case(Subject='Test Controller Acct Case');     
        ca.Priority = '3-Major Problem';
        insert ca;
        dc.getRedir();           
    }
}

But before going for that try below salesforce trailhead on test class to get more knowledge.
https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro

Let me know if need any help

Thanks
Prathyusha
 
bhanu_prakashbhanu_prakash
Thank for your quick update, Iam facing issue with User insatalization in test class @Prathyusha

User u = [Select firstname, IsPortalEnabled__c From user Where Id = :UserInfo.getUserId()]; ( on Apex class )
Prathyu bPrathyu b
Ok...
Try like something below. Create test user and run the tets class with that user context.

@isTest
public class DispatcherNewCaseControllerTest{
    static testMethod void DispatcherNewCaseController(){
        Profile sysProfle = [Select Id, name from Profile where name='System Administrator'];
        User newUser = new User(firstname = 'Denise',IsPortalEnabled__c= false,Username = 'martin@test.com.dev', LastName= 'Martin', Email='martin@test.com', Alias='dm',
                                CommunityNickname='dmc', TimeZoneSidKey='(GMT-07:00) Pacific Daylight Time', LocaleSidKey='English', EmailEncodingKey='General US & Western Europe (ISO-8859-1, ISO-LATIN-1)', 
                                ProfileId=sysProfle.Id, LanguageLocaleKey='EN-US');
        insert newUser;
        System.runAs(newUser){
            Account testAccount = new Account(name='Test Company Name');
            insert testAccount;
            ApexPages.StandardController sc = new ApexPages.standardController(testAccount);
            DispatcherNewCaseController dc = new DispatcherNewCaseController(sc);
            Case ca = new Case(Subject='Test Controller Acct Case');     
            ca.Priority = '3-Major Problem';
            insert ca;
            dc.getRedir();    
        }
    }
}

you may get some error on user creation. So, provide correct test data to avoid teh errors and let me know.

Thanks
Prathyusha
This was selected as the best answer
bhanu_prakashbhanu_prakash
thanks for your replay