function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Leonard CadetLeonard Cadet 

Developing A Test Class

I've written Apex classes but this is my first time written a test class. I've read a number of sources and watch videos but I don't grasp the foundation to get a test to succesfully cover my code. 

Here is my Apex class that works in my full sandbox:

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Reference Code for creating records  
//https://salesforce.stackexchange.com/questions/127889/how-to-automatically-create-a-new-record-in-another-object
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

global class MasterInventoryETL implements Schedulable {
    global void execute(SchedulableContext sc){
        List<Stock_Entry__c > Stock_E = [
            SELECT Aisle__c,Item__c,Last_Record__c,Section__c,Shelf__c,Units_in_Stock__c,units_entry__c,
            Warehouse_name__c,Account_item_owner__c,Effective_Start_Date__c,Effective_End_Date__c,
            Status__c,Stock_Location__c,Transaction_Entry__c,Transaction_Number__c,Virtual_Bin_Designation__c,
            Master_Allocation__c,P_O__c,Shipping_ID__c
            FROM Stock_Entry__c 
            WHERE last_record__c = true 
        ];
        
        List<Master_Inventory__c > snewt = new List <Master_Inventory__c>(); 
        
        
        for( Stock_Entry__c se :Stock_E){
            
            Master_Inventory__c snew = new Master_Inventory__c (
                units_entry__c = se.units_entry__c ,
                Units_in_Stock__c = se.Units_in_Stock__c,
                warehouse_name__c = se.warehouse_name__c ,
                Aisle__c = se.Aisle__c,
                Section__c = se.section__c,
                Shelf__c = se.Shelf__c,
                Status__c = se.Status__c,
                Item__c = se.Item__c,
                Account_item_owner__c =se.Account_item_owner__c,
                Effective_Start_Date__c = se.Effective_Start_Date__c,
                Effective_End_Date__c = se.Effective_End_Date__c,
                Master_Allocation__c = se.Master_Allocation__c,
                P_O__c = se.P_O__c,
                Shipping_ID__c = se.Shipping_ID__c,
                Stock_Location__c = se.Stock_Location__c,
                Transaction_Entry__c = se.Transaction_Entry__c,
                Transaction_Number__c = se.Transaction_Number__c,
                Virtual_Bin_Designation__c = se.Virtual_Bin_Designation__c,
                Power_of_One__c = 1
                
            );
            
            snewt.add(snew);
            
        }
        
        Insert snewt;
    }
    
}

Here is my test class, I have 3 tests using different sources I've read and none work:
@isTest(SeeAllData=TRUE)
Public class MasterInventoryETL_test{
    public static String CRON_EXP = '0 0 1 * * ? ';
  
   @isTest static void test1()
    {
        Stock_Entry__c s = [SELECT Id,pallet_location__c,item__c,aisle__c,shelf__c,
        Virtual_Bin_Designation__c, section__c,status__c, units_Entry__c
        FROM Stock_Entry__c WHERE Virtual_Bin_Designation__c LIKE '%Test%' Limit 1]; 
        System.assert(s != null);        
        // Create a test stock entry item based on the queried record. 
        Stock_Entry__c testStockEntry= s.clone();
        testStockEntry.Virtual_Bin_Designation__c = 'Test Stock';
        insert testStockEntry;
        // Query the test stock entry that was inserted.
        Stock_Entry__c testStockEntry2 = [SELECT Id,pallet_location__c,item__c,aisle__c,shelf__c,
        Virtual_Bin_Designation__c, section__c,status__c, units_Entry__c
        FROM Stock_Entry__c WHERE Virtual_Bin_Designation__c='Test Stock' LIMIT 1];
        System.assert(testStockEntry2 != null);
        
    }
    @isTest static void test2() {
        Account acc = new Account();
        acc.name ='Test';
        acc.RecordTypeID = '012j0000000pirBAAQ';
        insert acc;
        Sourcing_Item__c sitem = new Sourcing_Item__c();
        sitem.name = 'test mitem';
        sitem.Unit_of_measure__c = 'unit';
        sitem.Account_Owner__c = acc.id;
        insert sitem;
        Stock_Entry__c sec = new Stock_Entry__c();
        sec.Virtual_Bin_Designation__c = 'test';
        sec.units_Entry__c = 0;
        sec.item__c = sitem.Id;
        sec.status__c = 'In hand';
        insert sec;
        Test.startTest(); 
        MasterInventoryETL mItest = new MasterInventoryETL();
        mItest.execute(null);
        Test.stopTest(); 
    }
    @isTest static void test3() {
        Account acc = new Account();
        acc.name ='Test';
        acc.RecordTypeID = '012j0000000pirBAAQ';
        insert acc;
        Sourcing_Item__c sitem = new Sourcing_Item__c();
        sitem.name = 'test mitem';
        sitem.Unit_of_measure__c = 'unit';
        sitem.Account_Owner__c = acc.id;
        insert sitem;
        Stock_Entry__c sec = new Stock_Entry__c();
        sec.Virtual_Bin_Designation__c = 'test';
        sec.units_Entry__c = 0;
        sec.item__c = sitem.Id;
        sec.status__c = 'In hand';
        insert sec;
        test.startTest();
        String jobId = System.schedule('MasterInventoryETL',  CRON_EXP,  new MasterInventoryETL()); 
        Test.stopTest(); 
    }

}


Any help would be apperciated. I am making test data to be used in the test method but I'm not even sure what data points I should make, how to make test data or how to confirm I'm creating things properly.
AnudeepAnudeep (Salesforce Developers) 
I recommend checking if you are able to schedule the job by executing the below line of code anonymously in the developer console (by hardcoding CRON_EXP)
String jobId = System.schedule('MasterInventoryETL',  CRON_EXP,  new MasterInventoryETL());

I don't see any issues with the test class other than using SeeAllData=true where you can use @TestSetup

If you want examples, I recommend reviewing the following posts

http://amitsalesforce.blogspot.com/2017/07/how-to-write-test-class-for-scheduler.html

https://salesforce.stackexchange.com/questions/95827/test-class-for-scheduler-class

Let me know if it helps
Leonard CadetLeonard Cadet
Hello,

So I'm what should I put into the developer console when checking if I can schedule the job anonymously? I used the following and get an error:

Unexpected token 'jobId'

 
String jobId =System.schedule('MasterInventoryETLClassTest', 0 0 0 15 3 ? *, new MasterInventoryETL());


Also I've followed the examples in the links you've sent but my code coverage isn't improving:
 
@isTest
private class MasterInventoryETL_test
{

    @isTest static void test1()
    {
        String CRON_EXP = '0 0 0 15 3 ? *';
        
        // Create your test data
        Account acc = new Account();
		acc.name ='Test';
        acc.RecordTypeID = '012j0000000pirBAAQ';
		insert acc;

        Sourcing_Item__c sitem = new Sourcing_Item__c();
        sitem.name = 'test mitem';
        sitem.Unit_of_measure__c = 'unit';
        sitem.Account_Owner__c = acc.id; 
        insert sitem;

        Stock_Entry__c sec = new Stock_Entry__c();
        sec.Virtual_Bin_Designation__c = 'test';
        sec.units_Entry__c = 0;
        sec.item__c = sitem.Id;
        sec.status__c = 'In hand';
        sec.aisle__c = 'a';
        sec.section__c = 2;
        sec.shelf__c = 'a';
        insert sec;
        
        Test.startTest();

            String jobId = System.schedule('MasterInventoryETLClassTest',  CRON_EXP, 
            new MasterInventoryETL());

            CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime 
            FROM CronTrigger WHERE id = :jobId];

            System.assertEquals(CRON_EXP, ct.CronExpression);
            System.assertEquals(0, ct.TimesTriggered);

        Test.stopTest();

        // Add assert here to validate result
        List<AsyncApexJob> jobsScheduled = [select Id, ApexClassID, ApexClass.Name, 
        Status, JobType from AsyncApexJob where JobType = 'ScheduledApex'];

        System.assertEquals(1, jobsScheduled.size(), 'expecting one scheduled job');
    }
}



It looks like what I'm doing isn't relating to the apex class I want to test