You need to sign in to do that
Don't have an account?
SkyBlue2007
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',languagelocalekey='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
just changed the class in this way
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....