-1

My tests are all passing, so I know that my test code is calling my main class's methods. Salesforce is telling me I have 0% coverage though, so I'm not really sure what's going on.

(I'm very inexperienced with Apex, this is my first trying to write tests or use the Batch interface).

This is my class:

global class CandidateValidatorBatch implements Database.Batchable<sObject> {

global Database.Querylocator start(Database.BatchableContext BC) {
    return Database.getQueryLocator('SELECT Contact.CV_TR__c, Contact.ts2__Text_Resume__c, Contact.ts2__Resume_Last_Updated__c FROM Contact WHERE Contact.Account.Name = \'Candidates\'');
}

global void execute(Database.BatchableContext BC, List<sObject> scope) {
    List<Contact> candidates = new List<Contact>();
    List<Contact> updated_candidates = new List<Contact>();
    for(sObject objScope: scope){
        Contact candidate = (Contact)objScope;
        if (String.isBlank(candidate.ts2__Text_Resume__c)){
          candidate.CV_TR__c = false;
          candidate.ts2__Resume_Last_Updated__c = null;
        } else{
          candidate.CV_TR__c = true;
        }
        updated_candidates.add(candidate);
    }
    Database.SaveResult[] srList = Database.update(updated_candidates, true);

    for (Database.SaveResult sr : srList) {
        if (sr.isSuccess()) {
            System.debug('Successfully updated contact. Contact ID: ' + sr.getId());           
        } else {
            for(Database.Error err : sr.getErrors()) {
                System.debug('The following error has occured.');
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Contact fields that affected this error: ' + err.getFields());
            }
        }
    }

}

global void finish(Database.BatchableContext BC){

}

}

And this is my test class:

@isTest 
private class CandidateValidatorTest {
   @isTest static void test() {

      Contact cand1 = new Contact();
      cand1.CV_TR__c = false;
      cand1.ts2__Text_Resume__c = 'content';
      cand1.LastName = 'test1';
      Datetime c1time = Datetime.newInstance(2014, 1, 31, 7, 8, 16);
      cand1.ts2__Resume_Last_Updated__c = c1time;
      insert cand1;

      Contact cand2 = new Contact();
      cand2.CV_TR__c = false;
      cand2.ts2__Text_Resume__c = '';
      cand2.LastName = 'test2';
      Datetime c2time = Datetime.newInstance(2014, 2, 31, 7, 8, 16);
      cand2.ts2__Resume_Last_Updated__c = c2time;
      insert cand2;

      CandidateValidatorBatch theBatch = new CandidateValidatorBatch();
      theBatch.start( (Database.BatchableContext) null );
      theBatch.execute( (Database.BatchableContext) null, new List<sObject>{ cand1, cand2 });
      theBatch.finish( (Database.BatchableContext) null );


      System.assertEquals(true, cand1.CV_TR__c);
      System.assertEquals('content', cand1.ts2__Text_Resume__c);
      System.assertEquals(c1time, cand1.ts2__Resume_Last_Updated__c);
      System.assertEquals(false, cand2.CV_TR__c);
      System.assertEquals('', cand2.ts2__Text_Resume__c);
      System.assertEquals(null, cand2.ts2__Resume_Last_Updated__c);


  }
}
3
  • are you running in developer console.
    – Ratan Paul
    Dec 17, 2015 at 12:18
  • Ratan: Yes I am.
    – valedan
    Dec 17, 2015 at 12:28
  • Poet: Yes, that was the issue! I needed to check the Always Run Asynchronously box. Thanks!
    – valedan
    Dec 17, 2015 at 12:32

1 Answer 1

1

Solved! It was the same problem and solution as this post: Salesforce Test Coverage "Stuck" at 0%

Guess I should search better next time.

1
  • Good to know. Caught me out as well! Nice, simple fix though.
    – Dan Jones
    Dec 17, 2015 at 12:42

Not the answer you're looking for? Browse other questions tagged .