+ Start a Discussion
Padmini S 26Padmini S 26 

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.
Apex class:

global with sharing class portalUserAuthenticationService
    global static String checkPortalUser(){
   RestRequest req = RestContext.request;
        RestResponse res = Restcontext.response;
        String Endpoint=req.requestURI;
        string userName=endpoint.substring(endpoint.lastIndexOf('/')+1);
         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];
                List<Account> acc = [Select Id,Status__c,Account_Type__c From Account where PersonContactid=:users[0].contactId and IsPersonAccount=true];
                    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';
                        return 'false';  
                    return 'false';
                return 'false';
            return 'false';
Test Class:
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.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];

I am not getting how to increase the code coverage for this class.Kindly help how to increase code coverage.

Thanks in Advance.

Kevin CrossKevin Cross
In your test class, add username in place of the asterisk in '/portalUserAuthentication/*'.  I also used full URL, including my instance name, in test for what that's worth.  In fact, here is a sample of my flow.
// Set up a test request
RestRequest request = new RestRequest();
// Set request properties
request.requestUri = 'https://{instance name here}/services/apexrest/{REST url pattern here}';
request.httpMethod = 'GET';
// Assign the request to RestContext (allows parsing URL params)
RestContext.request = request;
For your case, you want POST method and need to pass any data through request.requestBody.

Hope that helps!