You need to sign in to do that
Don't have an account?
Test Method for Custom controller
I have the following code and need to write a test for the constructor and the submitNotify method but am having a hard time trying to figure out how to do that since the server is set from a page parameter:
public with sharing class ServerNotificationsController { public Server__c server { get; set; } public String notification {get; set;} public String emailBody {get; set;} public Boolean showFormPanel {get; set;} public Boolean showResultsPanel {get; set;} public String[] emailToList = new String[]{}; public String feedbackMessage {get; set;} public List<Contact> recipientsForDisplay = new List<Contact>{}; public List<Contact> getRecipientsForDisplay() { return (List<Contact>) recipientsForDisplay; } public List<Account> accounts { get { if(accounts==null) { accounts = [SELECT Id, (SELECT Id, Name, Email, Server_Down_Notification__c, Change_Control_Notification__c FROM Contacts) FROM Account WHERE Server__c = :server.Id AND Server__c != NULL]; } return accounts; } private set; } private ApexPages.StandardController controller; public ServerNotificationsController(ApexPages.StandardController controller) { this.controller = controller; if(server == null) { this.server = [select id, name from Server__c where id = :ApexPages.currentPage().getParameters().get('id')]; } this.showFormPanel = true; notification = 'ServerDown'; BuildSingleNotificationRecipientList(); } public PageReference submitNotify() { if(emailToList.size() > 0) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = emailToList; mail.setToAddresses(toAddresses); mail.setReplyTo('support@domain.com'); mail.setSenderDisplayName('Support'); mail.setSubject(notification + ' Notification - ' + server.Name); mail.setBccSender(false); mail.setUseSignature(false); mail.setPlainTextBody(emailBody); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } this.showFormPanel = false; this.showResultsPanel = true; return null; } }
I am thinking the test needs to run the code in the constructor but not sure how to set the id parameter on the page and does it need to be a real object id or one I create in the test:
static testMethod void testServerNotificationsController () {
ApexPages.StandardController tempController = new ApexPages.StandardController(tempServer);
ServerNotificationsController serverController = new ServerNotificationsController(tempController);
System.assert(serverController.server != null);
}
Hi
static testMethod void testServerNotificationsController () {
Server__c ser = new ();
ser....
ser.
here try to insert server according to if conditions
insert ser;
here try to insert the contact and Account..........
ApexPages.StandardController tempController = new ApexPages.StandardController(ser);
ServerNotificationsController serverController = new ServerNotificationsController(tempController);
call the methods in a class with serverController
System.assert(serverController.server != null);
}
Regards,
Rajesh.