0 votes
19 views
by

this is my test class:

@isTest
public class Test_LWCOrder {
    
    private static Order__c order;
    private static ERPvs__Productos_Pedidos__c pp;
    private static Erpvs__Envio__c envio;


    @TestSetup
    public static void setup(){
        TestFactory instance = TestFactory.instance;

        order = instance.create(new Order__c());

        pp = instance.create(new ERPvs__Productos_Pedidos__c());

        envio = new Erpvs__Envio__c();
        insert envio;

        instance.insertAllByDependencies();

    }

    static{
        order = [select id from Order__c limit 1];
        pp = [select id from ERPvs__Productos_Pedidos__c limit 1];
        envio = [select id from Erpvs__Envio__c limit 1];
    }

    public static testmethod void getOrdersTest(){
        System.assertEquals(LWCOrder.getOrders(new List<Id>{order.id}).size(), 1);
    }

    public static testmethod void getProductosPedidosTest(){
        System.assertEquals(LWCOrder.getProductosPedidos(new List<Id>{pp.id}).size(), 1);
    }

    public static testmethod void getEnviosTest(){
        System.assertEquals(LWCOrder.getEnvios(new List<Id>{envio.id}).size(), 1);
    }

    public static testmethod void getMessagesTest(){
        Map<Id, List<Messages__c>> results = LWCOrder.getMessages(new List<Id>{order.id});
    }


}

I'* using static properties to be able to access their sf ids, and pass them to the methods I'* testing. But for some reason the query returns that there are no rows for assignment. Am I doing something wrong?

1 Answer

0 votes
by
 
Best answer

Your static initialization block (where you're performing the queries) is being run prior to your @testSetup method. That, or the static variables are being reset by Salesforce after running the setup method but before your test methods are run.

In either case, the solution is to turn that static initializer into a static method that you can call at the beginning of each of your test methods. It is a bit of boilerplate, but that's just what we have to do.

    static void runSetupQueries(){
        order = [select id from Order__c limit 1];
        pp = [select id from ERPvs__Productos_Pedidos__c limit 1];
        envio = [select id from Erpvs__Envio__c limit 1];
    }

    public static testmethod void getOrdersTest(){
        runSetupQueries();
        System.assertEquals(LWCOrder.getOrders(new List<Id>{order.id}).size(), 1);
    }
Welcome to Memory Exceeded, where you can ask questions and receive answers from other members of the community.
...