function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shri BEShri BE 

Need help in completeing Bulk Api apex test class

Hi everyone,

I am working on Apex bulk api request. I have completed class and got stuck in test class. I have started the test class and need to know how to complete and pass 75% code coverage.

Apex Class:
@RestResource(urlMapping='/insertAccount/*')
global with sharing class insertAccount
{
    @HttpPost
    global static List<Id> doPost(insertAccount.reqAcc reqAccount) 
    {
        Set<String> emails = new Set<String>();
        List<insertAccount.reqAccount> accounts = reqAccount.Account;
        
        for(insertAccount.reqAccount ac : accounts)
        {
            emails.add(ac.email);
            System.debug('--- Account Email ---' + ac.email);
            System.debug('--- Adding Account Email  ---' + emails);
        }

        Map<String, Id> mapAccount = new Map<String, Id>();
        
        for(Account a : [SELECT Id, PersonEmail FROM Account WHERE PersonEmail IN: emails])
        {
            mapAccount.put(a.PersonEmail, a.Id);
            System.debug('--- Map Acc Email ---' + a.PersonEmail);
        }
                
        List<Account> accList = new List<Account>();        
        
        for(insertAccount.reqAccount ac : accounts)
        {
            Account acc = new Account();
            if(mapAccount.containsKey(ac.email))
            {
                acc.Id = mapAccount.get(ac.email);
            }
            acc.lastname = ac.lastname;
            acc.firstname = ac.firstname;
            acc.PersonMobilePhone = ac.mobile;
            acc.PersonEmail = ac.email;
            acc.Billingcity = ac.city;
            acc.BillingState = ac.state;
            acc.BillingCountry = ac.country;
            acc.BillingPostalcode = ac.postal;
            acc.phone = ac.phone;
            
            accList.add(acc);
        }
        
        List<Id> successIds = new List<Id>();
        
        if(!accList.isEmpty())
        {
            for(Database.UpsertResult upsertresult: Database.upsert(accList))
            {
                successIds.add(upsertresult.getId());
            }
        }
        return successIds;
    }
    
    global class reqAcc 
    {
            global List<insertAccount.reqAccount> account;
    }
    
    global class reqAccount
    {
        Public String lastname;
        Public String firstname;
        Public String phone;
        Public String mobile;
        Public String email;
        Public String city;
        Public String state;
        Public String country;
        Public String postal;
    }
}
Test Class:
@IsTest
private class insertAccountTest
{
    static testMethod void testPostMethod()
    {
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/insertAccount';
        request.httpMethod = 'POST';
        RestContext.request = request;        
        
        String strId = insertAccount.doPost();
        System.assert(strId !=null );
    }
}
Thanks in Advance.

 
Shubham_KumarShubham_Kumar
Hi Shri 

You have to create your test data and request body before calling the method.
//step1 Create Your Test Data Here
//step 2
String myJSON = ' '; // Create your request body here 

RestRequest request = new RestRequest();
request.requestUri ='/services/apexrest/insertAccount';
request.httpMethod = 'POST';
request.requestBody = Blob.valueof(myJSON);
RestResponse res = new RestResponse();
RestContext.request = request;
RestContext.response = res;
insertAccount.doPost();

Do let me know if you have any further queries and please mark this as the best answer if this helped you.

Thanks
Shubham Kumar
Shri BEShri BE
Hi Shubham,

I am getting Error in line 12. Method does not exist or incorrect signature: void doPost()
insertAccount.doPost();

Can you check and let me know how to resolve this error.

Thanks.
Shubham_KumarShubham_Kumar
Hi Shri

you will have to pass the parameter in doPost(insertAccount.reqAcc reqAccount)  method, its your inner class "reqAccount" instance.
Shri BEShri BE
Hi Shubham,

I done the changes but still I am getting error 
Compile Error: Extra ')', at 'reqAccount'
 
@isTest()
private class insertAccountTest
{
    static testMethod void testPostMethod()
    {
        String json = '{'+
        '   \"reqAccount\": '+
        '    {'+
        '     \"account\": '+
        '      ['+
        '       {'+
        '        \"lastname\"  : \"Test2\",'+
        '        \"firstname\" : \"Account2\",'+
        '        \"mobile\"    : \"1234567890\",'+
        '        \"email\"     : \"123@gmail.com\",'+
        '        \"city\"      : \"Sydney\",'+
        '        \"state\"     : \"NSW\",'+
        '        \"country\"   : \"AUS\",'+
        '        \"postal\"    : \"2541\",'+
        '        \"phone\"     : \"9876543210\",'+
        '       },'+
        '{'+
        '        \"lastname\"  : \"Sample\",'+
        '        \"firstname\" : \"Account2\",'+
        '        \"mobile\"    : \"1234567890\",'+
        '        \"email\"     : \"Sample2@gmail.com\",'+
        '        \"city\"      : \"Sydney\",'+
        '        \"state\"     : \"NSW\",'+
        '        \"country\"   : \"AUS\",'+
        '        \"postal\"    : \"2541\",'+
        '        \"phone\"     : \"9876543210\",'+
        '       }     '+
        '      ]'+
        '}'+
        '}';        
        RestRequest request = new RestRequest();
        request.requestUri ='/services/apexrest/insertAccount';
        request.httpMethod = 'POST';
        request.requestBody = Blob.valueof(json);
        RestResponse res = new RestResponse();        
        RestContext.request = request;
        RestContext.response = res;        
        insertAccount.doPost(insertAccount.reqAcc reqAccount);
    }
}

Can you please check.

Thanks.