You need to sign in to do that
Don't have an account?

How to increase code coverage for apex class
Hi All,
I have written the test class for apex class code coverage. But i am getting only 56% code coverage for the class. Below are the apex class and test class.
I am not getting how to increase the code coverage for this class.Kindly help how to increase code coverage.
Thanks in Advance.
I have written the test class for apex class code coverage. But i am getting only 56% code coverage for the class. Below are the apex class and test class.
Apex class: @RestResource(urlMapping='/portalUserAuthentication/*') global with sharing class portalUserAuthenticationService { @HttpPost global static String checkPortalUser(){ RestRequest req = RestContext.request; RestResponse res = Restcontext.response; String Endpoint=req.requestURI; string userName=endpoint.substring(endpoint.lastIndexOf('/')+1); system.debug('**userName**'+userName); if(userName!='' || userName!=null){ system.debug('**inside userName**'+userName); List<User> users=[select id,contactId from User where (email=:userName OR username =:userName) and IsActive=true]; if(users.size()>0){ system.debug('**users**'+users); List<Account> acc = [Select Id,Status__c,Account_Type__c From Account where PersonContactid=:users[0].contactId and IsPersonAccount=true]; if(acc.size()>0){ system.debug('**acc**'+acc); if(acc[0].Account_Type__c=='Member' && acc[0].Membership_Status__c=='Approved'){ system.debug('**true acc type**'+acc[0].Account_Type__c+'**mem status**'+acc[0].Membership_Status__c); return 'true'; } else return 'false'; } else return 'false'; } else return 'false'; } else return 'false'; } }
Test Class: @isTest private class TestportalUserAuthenticationService { public static testmethod void testportaluser() { Account acc1 = new Account(); acc1.Name = 'newAcc1'; insert acc1; Contact con = new Contact(); con.AccountId = acc1.id; con.LastName = 'portalTestUser'; insert con; User u = new User(); u.username = 'newUser@yahoo.com'; u.email = 'pb@ff.com'; u.emailencodingkey = 'UTF-8'; u.localesidkey = 'en_US'; u.languagelocalekey = 'en_US'; u.timezonesidkey = 'America/Los_Angeles'; u.alias='nuser'; u.lastname='lastname'; u.contactId = con.id; insert u; System.RestContext.request = new RestRequest(); RestContext.request.requestURI = '/portalUserAuthentication/*'; String portalu = portalUserAuthenticationService.checkPortalUser(); List<Account> accdetails = [Select Id,Status__c,Account_Type__c From Account where PersonContactid=:u.contactId and IsPersonAccount=true]; System.assert(accdetails.size()==0); } }
I am not getting how to increase the code coverage for this class.Kindly help how to increase code coverage.
Thanks in Advance.
Hope that helps!