You need to sign in to do that
Don't have an account?
Nicholas Sewitz 9
Apex Test Class for Google Authentication Controller
Hey I am trying to write a test class for my google Authentication Controller. Even though it compiles when I try and run it, it doesn't work. I may be way off here to begin with if I could please get some help.
Controller Class
Test Controller
Controller Class
public with sharing class googleAuthorization_Controller { public string googleEmail {get;set;} //to store our code for dynamic rendering public string code {get;set;} //to store our user record public User u {get;set;} public googleAuthorization_Controller() { googleEmail = userInfo.getUserEmail(); } //page action public pagereference doOnLoad(){ //retrieve current page Pagereference p = ApexPages.currentPage(); //does it have a code as parameter? code = p.getParameters().get('code'); //no? then stop if (string.isBlank(code)) return null; //it had one! get the state, aka email we passed //note you don't want to use googleEmail here //since we came back to the page, it reloaded and //the controller was reinstantiated, overriding our //input with the user's email string passedEmail = p.getParameters().get('state'); //query for the user, with token fields so we can modify u = [select id, Google_Access_Token__c, Google_Refresh_Token__c from User where id = :userInfo.getUserId()]; //call our api method to get tokens parsed into user u = googleCalendar_API.obtainAccessToken(u, code, googleCalendar_API.SF_AUTH_PAGE); //if we had no error if (u.Google_Access_Token__c != 'error'){ //set the google email u.google_email__c = passedEmail; //update the user and display success message update u; ApexPages.addMessage(new ApexPages.message(ApexPages.severity.confirm,'Authorized Successfully!')); } else{ //had an error? well then let us know <sadface> ApexPages.addMessage(new ApexPages.message(ApexPages.severity.error,'Authorization Error.')); } //stay here, not going anywhere! return null; } public pagereference requestAuthorization(){ return googleCalendar_API.loginRequestPage( googleCalendar_API.SF_AUTH_PAGE, googleEmail); } }
Test Controller
@istest public class googleControllerTest { public String body { get; set; } public String method { get; set; } public String postParam { get; set; } public String url { get; set; } public String message { get; set; } public List<SelectOption> methodList { get { if(methodList==null) { methodList = new List<SelectOption>(); methodList.add(new SelectOption('GET','GET')); methodList.add(new SelectOption('POST','POST')); methodList.add(new SelectOption('PUT','PUT')); } return methodList; } set; } private Map<String,User> oauthServices { get { if(oauthServices==null) { oauthServices = new Map<String,User>(); for(User u : [ SELECT name, id, Google_Access_Token__c, Google_Refresh_Token__c FROM User]) { oauthServices.put(u.name,u); } } return oauthServices; } set; } public String selectedService { get { if(selectedService==null && oauthServices.size()>0) { selectedService = oauthServices.values()[0].name; } return selectedService; } set; } public List<SelectOption> services { get { services = new List<SelectOption>(); for(User obj : oauthServices.values()) { services.add(new SelectOption(obj.name,obj.name)); } return services; } set; } public PageReference execute() { System.debug('Method: '+method+', Service: '+selectedService+'. URL: '+url); Http h = new Http(); HttpRequest req = new HttpRequest(); req.setMethod(method); req.setEndpoint(url); if(method=='POST' || method=='PUT') { if(postParam!=null & postParam!='') { req.setBody(postParam); req.setHeader('Content-Type','application/x-www-form-urlencoded'); } else { req.setBody(body); } } System.debug('Sending request...'); HttpResponse res = h.send(req); body = res.getBody(); System.debug('Received response ('+res.getStatusCode()+' '+res.getStatus()+')'); message = ''; return null; } }
From the above code, I understand that you never called your controller method in your test class.
Please do initialize your controller and then call controller method.
You can make use of assert method to validate test results.
Your results can look like: Hope this helps.
Mark this post as solved if it helps.
Best Regards,
Nagendra.P