You need to sign in to do that
Don't have an account?
adeetz54724
How to write test class for pageReference using external ID in URL
I am trying to get code coverage for the class below:
public class redirectToWebinarPageController{ public pageReference redirectURL(){ //get the external id from the URL String externalId = ApexPages.currentPage().getParameters().get('externalId'); //query the record. Webinar__c webinarRecord = [select id from Webinar__c where Webinar_Count__c =: externalId]; //redirect to detail page of the above record. return new pageReference('/'+webinarRecord.id); } }Here is the test I have and am getting 75% coverage but I would prefer to know how to get 100%
@isTest public class redirectToWebinarPageControllerTest { public static testMethod void testredirectURL() { test.startTest(); PageReference pageRef = Page.redirectToWebinarDetailPage; //Your test data logics Account testAccount = new Account(); testAccount.Name = 'Account Name'; insert testAccount; Contact testContact = new Contact(); testContact.LastName = 'Contact'; testContact.FirstName = 'Test'; testContact.Email = 'testContact@testing.com'; insert testContact; Webinar__c w = new Webinar__c(); w.Name = 'Test Webinar'; w.Account__c = testAccount.id; w.Communication_Type__c = 'Client 4'; w.Delivery_Method__c = 'Self Study'; w.Contact__c = testContact.id; insert w; String testExternalId = w.Webinar_Count__c; Test.setCurrentPage(pageRef); pageRef.getParameters().put('externalId',testExternalId); redirectToWebinarPageController controller = new redirectToWebinarPageController(); controller.redirectURL(); test.stopTest(); } }Thank you in advance, I am really new to writing test classes and would love to know what I am missing here.
1. I am not seeing any input value for Webinar_Count__c field in test class. So i guess it will be automatically generated by trigger/some automation.If this statement is correct then use query to get the Webinar_Count__c from db.
2. If Webinar_Count__c needs manual input then set the value in test class.
3. use the ApexPages.CurrentPage() instead of pageRef.getParameters() in test class:
Thanks,
Maharajan.C
All Answers
Greetings!
I would suggest you to check which lines are covered and which are not covered makes easy for your to improve the code coverage.
How to check:Go to Setup>Open Developer console>Open the Apex class and Test class in two tabs
>Please run the test class in Developer console
>Go back to the apex class of the test class.
>Click on the arrow besides the Code coverage which shows the test class name with the percentage and the uncovered lines will be in red color.
In this way,you can work on the uncovered code to make sure you have 100% code coverage and aslo,it is not neccessary that you have 100% code coverage or every class in salesforce.
Kindly mark it as best answer if it helps so that it can help others in the future.
Warm Regards,
Shirisha Pathuri
1. I am not seeing any input value for Webinar_Count__c field in test class. So i guess it will be automatically generated by trigger/some automation.If this statement is correct then use query to get the Webinar_Count__c from db.
2. If Webinar_Count__c needs manual input then set the value in test class.
3. use the ApexPages.CurrentPage() instead of pageRef.getParameters() in test class:
Thanks,
Maharajan.C