I'm trying to put a webservices class into production but its telling me my code coverage is only 58%. I'm familiar with having to write a test class for triggers but what if this is a just a class, how do you make sure this code hits the 75%?

enter image description here

global class ClientAllocation {

WebService static List<Account> getAccount()
    string strQuery = 'select Name, CASESAFE_ID__c, Production_environment__c, External_Identifier__c, Member_Web_URL__c, Administration__c, Pensions__c, Broking__c, Helpdesk__c, Helpdesk_Email__c, Helpdesk_Number__c, Account_Owner_Name__c, Darwin_Support_Associate__c, Selection_Window_Open_Month__c WHERE Type = Client';
    return Database.query(strQuery);


  • 1
    The query doesn't look valid. You are better off using static SOQL for something like this i.e. return [select Name, ...]; so the query is checked at compile time. You still need to write a test though.
    – Keith C
    Jun 30 '15 at 12:40

You could start with something like this:

public class SampleTestClass{
    public static @isTest void sampleTestMethod(){
        list<Account> alist = ClientAllocation.getAccount();
        // system.assert();

In order to actually perform a test, you would have to use the assert() method to verify that the list of accounts returned by the function is what you expected them to be. What I have above is a very basic framework. You should use it as a framework for building your test.

And, thank you Keith for pointing out that the method is static.

  • 1
    The method is static...
    – Keith C
    Jun 30 '15 at 12:36
  • Hi Keith, could you elaborate? what do you mean by that? Jun 30 '15 at 12:52
  • Martin, how would I go about writing a test for a class? Could you provide an example? Jun 30 '15 at 12:52
  • Thanks Martin, that seemed to have worked for that particular class. I'm now faced with another challenge where its saying my overall code coverage is less than 75%!!! There are several very old triggers and classes that i think might be causing this. Jun 30 '15 at 13:56
  • @user3480962 Writing test code can be quite a bit of extra work. Best of luck.
    – martin
    Jun 30 '15 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.