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
SkyBlue2007SkyBlue2007 

How to test some Exceptions in a class!

Hi all,

 

I tried to test my custome controller, but I don' t know How to test some Exceptions in a class.

 

Here's a controller.

public class SampleController {
   private User user {get; set;}
    
   public SampleController () {
      String userid = UserInfo.getUserId();
      user = [select testfield__c from User where id = :userid];
   }    
   
   public PageReference login() {
      try {
         String testfield = user.testfield__c;
         if(testfield != null && testfield.length() > 0) {
            ・・・・・
         }
         else {
            SampleException e;
            throw new GsException('SampleException threw', e);
         }
      }
      catch (SampleException e) {
         String summary = e.getMessage();
         ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, summary)); 
      }
      return null;
   }
}

Here's a Exception Class.

public class SampleException extends Exception {
}

 

Here's a test class.

@isTest
private class SampleControllerTests {
   public static testMethod void testMyController() {
      Profile p = [select id from profile where name='Standard User'];
      User u = new User(alias = 'standt', email='standarduser@testorg.com', emailencodingkey='UTF-8',lastname='Testing',langua​gelocalekey='en_US', localesidkey='en_US', profileid = p.Id,timezonesidkey='America/Los_Angeles', username='standarduser@testorg.com');   
      
      System.runAs(u) {
         PageReference pageRef = Page.SamplePage;
         Test.setCurrentPage(pageRef);

         SampleController controller = new SampleController();

         try {
            PageReference nextPagePR = controller.login();
         }
         catch (Exception e) {
            String TypeName = e.getTypeName();
            System.assertEquals('SampleException', TypeName);
         }
      }    
   }
}

 

I have two questions.

One is how I confirm if Exception TypeName is "SampleException".

The other is how I confirm if  Message is "SampleException".

 

Thanks,

SkyBlue2007

kiranmutturukiranmutturu

just changed the class in this way

 

public class SampleController {
   private User user {get; set;}
    
   public SampleController () {
      String userid = UserInfo.getUserId();
      user = [select testfield__c from User where id = :userid];
   }    
   
   public PageReference login() {
      try {
         String testfield = user.testfield__c;
         if(testfield != null && testfield.length() > 0) {
           system.debug('&&&&');
         }
         else {
            Exception e;
            throw new GsException();
         }
      }
      catch (Exception e) {
         String summary = e.getMessage();
         ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, summary)); 
      }
      return null;
   }
}

 

and the test class for the above is

 

 

 

@istest

private class testexceon{

    static testmethod void tesexe(){

        SampleController obj = new SampleController ();

        obj.login();

    }

}

 

 

 

hope you got it....