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
Cory CowgillCory Cowgill 

Executing Multiple TestMethods in one Test Class.

I have an apex trigger on the Event object. The trigger works fine for both single and bulk DML operations on events.

 

I have a seperate apex test class using the @isTest annotation. I have two seperate test methods in the test class.

 

If I only run one of the test methods (I.E. I comment out the other test method) the unit test passes, the asserts are successful on either one of the test methods.

 

If I try to keep BOTH test methods in the same test class it fails. It may be end of long day and my eyes / memory is burnt out. But I am pretty sure you can have multiple "static testMethod void" tests run in a single Unit Test Class and that they should be independent of each other.

 

Any Ideas?

 

===================

@isTest
private class EventTriggerTest
{
    //Works If I comment out the second method, and vice-versa
    static testMethod void testAddWBAccountEventBulk()
    {
        test.startTest();
        Account firm = UnitTestFactory.buildTestFirm();
        insert firm;
        
        WB_Strategy__c strat = UnitTestFactory.buildTestWBStrategy();
        insert strat;
        
        WB_Account__c wbAcct1 = UnitTestFactory.buildTestWBAccount(firm.Id);
        wbAcct1.Strategy__c = strat.Id;
        insert wbAcct1;
        
        Contact aContact = UnitTestFactory.buildTestContact(firm.Id);
        insert aContact;
        List<Event> eventsInsert = new List<Event>();
        for(integer x = 0; x < 200; x++)
        {
            Event aEvent = UnitTestFactory.buildTestEvent(wbAcct1.Id, aContact.Id);
            aEvent.Strategy__c = null;
            eventsInsert.add(aEvent);
        }
        
        insert eventsInsert;
        
        List<Event> returnEvents = [Select Id, Strategy__c from Event where WhatId =: wbAcct1.Id];
        test.stopTest();
        system.assertEquals(200,returnEvents.size());
        for(Integer x = 0; x < 200; x++)
        {
            system.assertEquals(strat.Name,returnEvents.get(x).Strategy__c);
        }
    }    
    
    static testMethod void testAddWBAccountEvent()
    {
        test.startTest();
        Account firm = UnitTestFactory.buildTestFirm();
        insert firm;
        
        WB_Strategy__c strat = UnitTestFactory.buildTestWBStrategy();
        insert strat;
        
        WB_Account__c wbAcct1 = UnitTestFactory.buildTestWBAccount(firm.Id);
        wbAcct1.Strategy__c = strat.Id;
        insert wbAcct1;
        
        Contact aContact = UnitTestFactory.buildTestContact(firm.Id);
        insert aContact;
        
        Event aEvent = UnitTestFactory.buildTestEvent(wbAcct1.Id, aContact.Id);
        aEvent.Strategy__c = null;
        insert aEvent;
        
        Event returnEvent = [Select Id, Strategy__c from Event where Id =: aEvent.Id limit 1];
        test.stopTest();
        system.assertEquals(strat.Name,returnEvent.Strategy__c);
    }
}

hisrinuhisrinu

Normally you can have more than one test methods in a class... there is no harm in it.

 

In your case you are inserting 200 records and you might be having some triggers or workflows which is taking more time to complete these insertion of records........ but at the end of the day, it should give you the result.

Cory CowgillCory Cowgill

Yeah, I almost always do multiple unit test methods in one test class to clearly delineate the different test functions.

 

However, for some weird reasone the assert will bomb on my bulk test, even though the debug logs are showing that the values are being populated on the before insert trigger. It will show the value as "Null" when in the debug logs I see the value is getting set, and I don't see it getting overwritten by some other weird code.

 

Very weird and perplexing.

Cory CowgillCory Cowgill

So I merged the two testMethods into one large test method and everything works and all asserts pass.

 

I'm not a huge fan of doing it that way, but seems to be only way I can get it to work for the moment. I prefer to see the smaller testMethods broken out so when one fails I can see exactly which testMethod and what functionality.

 

I swear I've done this multiple times in the past, but I could be losing my mind.

 

It must be something weird with maybe the setup and teardown functionality in Apex, or how it executes the stacks for the different testMethods within one test class.

 

Anyway, thanks for your input. If anyone else has input its always appreciated.

 

Peace.

dmchengdmcheng

This shouldn't make a difference, but is there a reason why you have all your data setup statements within the Test.StartTest?

Cory CowgillCory Cowgill

Yeah, I know. I was cutting/pasting them around to see if it made a difference.

 

Best practice is to keep them out of the Test.startTest()/Test.StopTest(), but I've been trying anything to get past this issue.

 

However, due to this bug I was moving them around just to see if they had an impact.

 

I currently only have them wrapped around the actual insert events; call only, which is where trigger is fired and what I'm testing.