0

I have the following problem in a test class.

Constructor not defined: [Soma_Dependentes_Update].<Constructor>()

Test class of the batch process:

@isTest
public class Soma_Dependentes_UpdateTest {
    @isTest
    public static void testeit() {
        Account acc = new Account(Name='Dependentes');
        insert acc;

        Test.startTest();
        Soma_Dependentes_Update batch = new Soma_Dependentes_Update();
        Id batchId = Database.executeBatch(batch);
        Test.stopTest();
    }
}

Here's the Batch class:

global class Soma_Dependentes_Update implements Database.Batchable<sObject> {

    global final String Query;
    global final String Field;
    global final Integer Value;

    global Soma_Dependentes_Update(String q, String f, Integer v) {

        Query = q; Field = f; Value = v;
    }


    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    } 

    global void execute(Database.BatchableContext BC, List<Account> scope) {
        for(Account acc : scope) {
        acc.put(Field, Value);
        }   
        update scope;
    }

    global void finish(Database.BatchableContext BC) {

    }
}

I use the following snippet parameters in Execute Anonymous window:

//Parametriza e executa a classe Soma_Dependentes_Update. 
String q = 'SELECT SomaDependentes__c FROM Account WHERE Name LIKE \'%Dependentes%\'';
String f = 'SomaDependentes__c';
public list<AggregateResult> countDep = new list<AggregateResult>();
countDep = [SELECT sum(Dependentes__c)am FROM Contact WHERE Account.Name LIKE '%Dependentes%'];
String str = String.valueOf(countDep[0].get('am'));
Integer v = Integer.valueOf(str);

//Executa o Job para atualizaçAo de SomaDependentes__c no objeto Conta.
ID jobID = Database.ExecuteBatch(new Soma_Dependentes_Update(q, f, v));

Can anyone help to identify what I'm doing wrong with the code?

1
  • Take a look at this answer for the question linked in the comments. Your issue here is that you don't have a default constructor defined in your batch class, and that you are trying to invoke it in your test class. If you do so, you will need to define a no-arg constructor in your class as well.
    – Jayant Das
    Mar 8, 2019 at 18:33

1 Answer 1

1

You don't have a constructor with no arguments, which is what you're trying to create in your test class. You can either create a default constructor such as:

global Soma_Dependentes_Update() {
    String q = 'SELECT SomaDependentes__c FROM Account WHERE Name LIKE \'%Dependentes%\'';
    String f = 'SomaDependentes__c';
    countDep = [SELECT sum(Dependentes__c)am FROM Contact WHERE Account.Name LIKE '%Dependentes%'];
    String str = String.valueOf(countDep[0].get('am'));
    Integer v = Integer.valueOf(str);
    Soma_Dependentes_Update(q,f,v);
}

that calls your other constructor (you can set these defaults to be whatever you want). Or, you can set some values to actually test in your test class (as above). Currently, your test class is only testing to make sure that this runs without errors, and not actually explicitly testing that your batch class works. I would write a @testSetup method where you insert the relevant data, and then in your test method run an actual check to make sure that your batch class did what it's supposed to do to those records.

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