You need to sign in to do that
Don't have an account?
Krutarth Soni
how to cover IF.. ELSE condition in test class
Hello,
Help me to get 100% code coverage of this code,
I tried this test class, but it only covers else part. it does not cover if (condition) block.
Help me to achieve 100% code coverage.
--
Thank-You!
Krutarth Soni
Help me to get 100% code coverage of this code,
public class Overview_Controller { public string baseurl = URL.getSalesforceBaseUrl().toExternalForm(); public string create = '/one/one.app#/alohaRedirect/'; public string Framework = '/one/one.app#/sObject/CGR__CG_Framework__c/list'; public PageReference CreateFramework(){ if(UserInfo.getUiTheme()== 'Theme4d') { string FrameworkPrefix = CGR__CG_Framework__c.sObjectType.getDescribe().getKeyPrefix(); PageReference p = new PageReference (baseurl+create+FrameworkPrefix+'/e' ); p.setRedirect(true); return p; } else { string FPre = CGR__CG_Framework__c.sObjectType.getDescribe().getKeyPrefix(); PageReference p1 = new PageReference ('/'+FPre+'/e' ); p1.setRedirect(true); return p1; } } }
I tried this test class, but it only covers else part. it does not cover if (condition) block.
@isTest public class Overview_Controller_Test { private static testMethod void testOverview() { Overview_Controller oc = new Overview_Controller (); oc.CreateFramework(); } }
Help me to achieve 100% code coverage.
--
Thank-You!
Krutarth Soni
1) https://salesforce.stackexchange.com/questions/183520/how-to-set-ui-theme-in-test-salesforce1
Please update your Apex class like below And test like below
Let us know if this will help you
All Answers
Please check once below code:
Thanks
Varaprasad
Please check once above code also.
Thank you for the quick reply! but both the code is not working, I mean still IF block is not cover.
--
Thank-You!
Krutarth Soni
You Need to create user in your test class and need to use System.runAs(user) in your test to cover that if in your test class.
Thank you for the reply! but the code is not working, I mean still IF block is not cover.
--
Thank-You!
Krutarth Soni
In your class either we can cover if or else condition only.
Reason :
You can not change the theme of salesforce CRM UI, however you can select the options to change theme as latest, which insures that latest changes in UI theme will reflect in your org. For that go to setup-> Customize-> UserInterface-> select the checkbox (Enable New User Interface Theme) and click on save.
To Cover IF condition in your code :
Please execute below line on a developer console anonymous window and check theme name is Theme4d or not.
System.debug('Current UserInfo Theme: ' + UserInfo.getUiTheme());
If UI theme is Theme4d your if condition will work.
Otherwise else will work.
If you want to cover if class then change theme based on debug logs.
I hope it helps you.
Thanks
Varaprasad
1) https://salesforce.stackexchange.com/questions/183520/how-to-set-ui-theme-in-test-salesforce1
Please update your Apex class like below And test like below
Let us know if this will help you
Please check once below code :
Hope it helps you.It will cover 100% code coverage.
Thanks
Varaprasad
--
Thank-You!
Krutarth Soni