0

I create scheduled apex and create test class but i only get 71% code coverage even though i already create test contact that meet the query. When i try to command on the query i got 100% code coverage with same test class.

Here is the first apex class (71%)

global class DepostiInvoiceAutomation implements Schedulable
{
global void execute(SchedulableContext SC) 
{  
    Date dt = Date.newInstance(2016,12,1);

    List<Contact> contactToUpdate = new List<Contact>();
    List<Deposit_Invoice__c> depositToCreate = new List<Deposit_Invoice__c>();
    List<Contact> listContact = new List<Contact>();
    listContact= [SELECT Id FROM Contact WHERE Is_Deposit_Not_Generated__c=true AND Deposit_Received__c = true AND Next_Deposit_Generate_Date__c!= null 
                  AND Next_Deposit_Generate_Date__c<=:system.today() AND CreatedDate>=:dt AND Account.Are_All_Opportunities_Closed__c=false 
                  AND Account.Sign_Up__c=true LIMIT 100];

    for(Contact con : listContact){
        con.Is_Deposit_Not_Generated__c = false;
        contactToUpdate.add(con);
        Deposit_Invoice__c dep = new Deposit_Invoice__c(Contact__c=con.id,Amount__c=250000);
        depositToCreate.add(dep);
    }
    update contactToUpdate;
    insert depositToCreate;
}
}

Here is the updated code (100%) after i command "Is_Deposit_Not_Generated__c=TRUE". Is_Deposit_Not_Generated__c is checkbox field that have default value true

global class DepostiInvoiceAutomation implements Schedulable
{
global void execute(SchedulableContext SC) 
{  
    Date dt = Date.newInstance(2016,12,1);

    List<Contact> contactToUpdate = new List<Contact>();
    List<Deposit_Invoice__c> depositToCreate = new List<Deposit_Invoice__c>();
    List<Contact> listContact = new List<Contact>();
    listContact= [SELECT Id FROM Contact WHERE /*Is_Deposit_Not_Generated__c=true AND*/ Deposit_Received__c = true AND Next_Deposit_Generate_Date__c!= null 
                  AND Next_Deposit_Generate_Date__c<=:system.today() AND CreatedDate>=:dt AND Account.Are_All_Opportunities_Closed__c=false 
                  AND Account.Sign_Up__c=true LIMIT 100];

    for(Contact con : listContact){
        con.Is_Deposit_Not_Generated__c = false;
        contactToUpdate.add(con);
        Deposit_Invoice__c dep = new Deposit_Invoice__c(Contact__c=con.id,Amount__c=250000);
        depositToCreate.add(dep);
    }
    update contactToUpdate;
    insert depositToCreate;
}
}

Here is the test class

@isTest
private class TestDepositInvoiceAutomation{

static testMethod void automateDeposit1()
{
    Datetime createDate = Datetime.now().addDays(-29);
    Account acc = new Account(Name='test acc',Sign_Up_Timestamp__c=system.today());
    insert acc;
    Contact con = new Contact(LastName='test con',AccountId=acc.id,Deposit_Received_Date__c=system.today().addDays(-100),Deposit_Received__c=true,
                              Is_Deposit_Not_Generated__c=true);
    insert con;
    Opportunity opp = new Opportunity(Name='test opp', AccountId=acc.id,StageName='Invoiced',CloseDate = system.today());
    insert opp;
    con.Is_Deposit_Not_Generated__c=false;
    update con;
    Deposit_Invoice__c dep = new Deposit_Invoice__c(Contact__c=con.id,Amount__c = 250000);
    insert dep;

    System.schedule('testDepostiInvoiceAutomation', '0 31 13 * * ?', new DepostiInvoiceAutomation());

}
}

Is there something i miss?

  • 1
    It looks like Next_Deposit_Generate_Date__c should be set to a specific value (Date.today). Make sure you have all the fields you need. – sfdcfox Mar 23 '17 at 4:45
  • 3
    Please include code as text, not images. – Adrian Larson Mar 23 '17 at 4:47
  • It means field Is_Deposit_Not_Generated__c is not correctly setup in test class and query return zero row. That's why code (Inside for loop) is marked in red – J Doe Mar 23 '17 at 5:04
  • What I have identified is first you are inserting a contact with Is_Deposit_Not_Generated__c= TRUE. Then after inserting Is_Deposit_Not_Generated__c=FALSE after opportunity creation. Your query is looking Is_Deposit_Not_Generated__c= TRUE that is not available in data. The best approach is to use system.assert to verify your result in test class – J Doe Mar 23 '17 at 5:13
0

You are creating a contact with Is_Deposit_Not_Generated__c= true and then after inserting opportunity you are updating the con.Is_Deposit_Not_Generated__c= false so the value of Is_Deposit_Not_Generated__c will be false.

In your query you are trying to select Is_Deposit_Not_Generated__c = true but you have updated the Is_Deposit_Not_Generated__c = false, you will not be getting any records for this condition because of that the for loop is not executing. When you comment Is_Deposit_Not_Generated__c you will get records and the for loop gets executed.

| improve this answer | |
  • it make it 100% code coverage, i do that because i think we need to create scenario exactly like the class thats why i create "Is_Deposit_Not_Generated__c = false". – febriantarigan Mar 23 '17 at 6:05
  • Congrats you have executed every line and tested nothing – Eric Mar 23 '17 at 7:17

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.