0

hi guyz im new in salesforce can anyone help meh with my code for testclass heres the code:

@isTest
public class Testclass {

    @isTest static void Testclass() {

        List<sobject> lmsin = [Select Id,Name,Field1,Field1,Field1,(Select Id, Field1 from sobject) from sobject];
        insert lmsin;
        sobject lmsca = new sobject();
        lmsca.Name = '';
        lmsca.Field1 = '';
        lmsca.Field1 = '';
        lmsca.Field1 = '';
        insert lmsca;


        test.startTest();

        Batchclass lcfb = new Batchclass();
        Database.executeBatch(lcfb,1);

        test.stopTest();        
    }
}
  • What's the issue with code? – Reshma Dec 12 '18 at 7:44
  • I don’t think you can reference fields like that for a generic sObject. Also what does your start method look like. What errors are you getting? – Eric Dec 12 '18 at 8:56
0

I will approach this by setting up your test data and then running the test:

@isTest
private class BatchTestClass{

    @testSetup static void setupData()
    {

        // Inserting test data here 


       // Do some asserts here to make sure your data is corret
        System.assertEquals(); 
        System.assertEquals(); 
        System.assertEquals(); 

    }
    // Run your test
    @isTest static void Runtest() 
    {   
        // Firing apex bacth class      
        Test.startTest();
        YourBatchClassName batchToTest = new YourBatchClassName (); 
        Id batchId = Database.executeBatch(batchToTest );
        Test.stopTest();

        // After batch is completed run asserts here 
        System.assertEquals();
        System.assertEquals();
        System.assertEquals();



    }

}

So you need to insert the test data that will be similar to the expecting data from your batch operation. Then run a test calling the real batch class name. Make sure you assert your test data an expecting results.

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.