2

i need some help on a TestClass as i said above, bellow i wil show you what the class looks like and the TestClass:

APEX CLASS:

public with sharing class DirectDebitController {

    public class AccountAmount {
        @AuraEnabled
        public Id accountId {get; set;}
        @AuraEnabled
        public String accountName {get; set;}
        @AuraEnabled
        public string sortCode {get; set;}
        @AuraEnabled
        public String accountNumber {get; set;}
        @AuraEnabled
        public Decimal amount {get; set;}
        @AuraEnabled
        public List<Invoice_Header__c> invoicesToBePaid {get; set;}
        @AuraEnabled
        public Integer numberOfAccounts {get; set;}

        public AccountAmount(Id accountId, String accountName, String sortCode, String accountNumber, Decimal amount, List<Invoice_Header__c> invoicesToBePaid, Integer numberOfAccounts) {

            this.accountId = accountId;            
            this.accountName = accountName;
            this.sortCode = sortCode;
            this.accountNumber = accountNumber;
            this.amount = amount;
            this.invoicesToBePaid = invoicesToBePaid;
            this.numberOfAccounts = numberOfAccounts;
        }
    }

    @AuraEnabled
    public static List<AccountAmount> fillTable() {
        List<AccountAmount> accounts = new List<AccountAmount>();
        List < Account > accountsAndAmounts = [SELECT Name, Payment_Account_Number__c, Payment_Sort_Code__c, (SELECT Id, AmountOutstanding__c FROM Invoice_Headers__r WHERE AmountOutstanding__c > 0 AND ToPublish__c = true AND Query__c = false AND Overdue_Days__c > 0) FROM Account WHERE Enqix_Payment_Method__c = 'Direct Debit'
            AND Id IN(SELECT Account__c FROM Invoice_Header__c WHERE AmountOutstanding__c > 0 AND ToPublish__c = true AND Query__c = false AND Overdue_Days__c > 0) order by Name];
        Integer numberOfAccounts = Database.countQuery('SELECT COUNT() FROM Account WHERE Enqix_Payment_Method__c = \'Direct Debit\' AND Id IN(SELECT Account__c FROM Invoice_Header__c WHERE AmountOutstanding__c > 0 AND ToPublish__c = true AND Query__c = false AND Overdue_Days__c > 0)');
        for (Account a : accountsAndAmounts) {
            List<Invoice_Header__c> invoices = new List<Invoice_Header__c>();
            Decimal amount = 0;
            for (Invoice_Header__c i : a.Invoice_Headers__r) {
                amount += i.AmountOutstanding__c;
                invoices.add(i);
            }
            AccountAmount aa = new AccountAmount(a.Id, a.Name, a.Payment_Sort_Code__c, a.Payment_Account_Number__c, amount, invoices, numberOfAccounts);
            accounts.add(aa);
        }
        return accounts;

    }
}

TESTCLASS :

@isTest
private class DirectDebitControllerTest {

  @isTest
    static void TestfillTable(){        

        Account novaConta = new Account(
            Name = 'TESTE Account',
            Enqix_Payment_Method__c = 'Direct Debit',
            Payment_Account_Number__c = '123',
            Payment_Sort_Code__c = '321',
            Credit_Days__c = 200           
        );
        insert novaConta;
        system.debug('NOVA INVOICE'+ novaConta);

        Invoice_Header__c header = new Invoice_Header__c(
            Name = 'TESTE header',
            Account__c = novaConta.Id,
            Salesforce__c = false,
            CreatedDate__c = Date.newInstance(2016, 11, 11),
            Query__c = False,
            AmountOutstanding__c = 2000 
        );
        insert header;

        Account accountTest = [Select Id, Name, Enqix_Payment_Method__c, Payment_Account_Number__c, Payment_Sort_Code__c, Credit_Days__c from Account where Id =: novaConta.Id];
        Invoice_Header__c headerInserido = [Select Id, Query__c, ToPublish__c, Account__c, AmountOutstanding__c, Salesforce__c, Payment_Due_Date__c, Overdue_Days__c from Invoice_Header__c Where Id = :header.Id][0];
        //system.debug('Header novo '+ header + ' ToPublish está a true? = ' + header.ToPublish__c);
        system.debug('Header novo '+ headerInserido + ' ToPublish está a true? = ' + headerInserido.ToPublish__c + ' Overdue_Days valor? = ' + headerInserido.Overdue_Days__c);

        DirectDebitController.fillTable();
        //DirectDebitController.sendEmailWithAttachment(List<Account> accountTest);
    }
}

If you need some input from me just ask, but i really could use some help, thanks in advance.

2

Covering those lines is easy, and you may want to use an assert to do it. You can do it two ways.

Easy (and not best practice)

DirectDebitController.AccountAmount aa = new DirectDebitController.AccountAmount(<provide parameters>);
aa.accountId = <someValue>;
(do the same for all your properties)

Harder (but best practice since it helps you assert the behavior). Add this to the end of your test method.

List<DirectDebitController.AccountAmount> aaRecs = DirectDebitController.fillTable();
for (DirectDebitController.AccountAmount aa : aaRecs) {
  System.assertEquals(<valueToCompare>, aa.accountId);
  <do this for all your properties>
}

The second way not only covers and exercises your code, but it also verifies that your list is populated with the values you expect it to have.

  • Hi @Sebastian Kessel , first of all thank you for the response, the two options are returning an error saying the AccountAmount is a Invalid Type. error message: 'Invalid type: AccountAmount' – Nuno Carvalho Nov 21 '18 at 17:07
  • Fixed the code for you. Remember that you have to fully qualify inner classes. – Sebastian Kessel Nov 21 '18 at 17:24
  • 1
    that fixed the error TY, i will keep in mind to fully qualify the inner classes, my bad. – Nuno Carvalho Nov 21 '18 at 17:36

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.