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

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:
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.
You have to create your test data and request body before calling the method.
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
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.
you will have to pass the parameter in doPost(insertAccount.reqAcc reqAccount) method, its your inner class "reqAccount" instance.
I done the changes but still I am getting error
Compile Error: Extra ')', at 'reqAccount'
Can you please check.
Thanks.