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
Marry SteinMarry Stein 

Unterstand @testsetup methode

Hey guys,
i am starting to freak out ^^  i have created multiple records in my testsetup methode. Now, i want to test my controller methode in different ways...Null, bulky, postive. I thought i have query the records i want to use in the specific test methode to test that the controller methode works like it should. 

Unfortunately, the controller methode catches all records  in my testsetup methode... 
Controller methode:
    public List<Opportunity>getOpportuntiies(){
        List <Opportunity> oppList = [SELECT Name, Account.Name, Stagename, CloseDate, z_Vertragsbeginn__c FROM Opportunity WHERE OwnerId =:UserInfo.getUserId() AND IsClosed = false AND z_Vertragsbeginn__c <= Last_Month LIMIT 100 ];
        
        numOpps = oppList.size();
        return oppList;        
    }


testsetup methode: 

 @testSetup static void setup () {
        
        Opportunity opp = new Opportunity ();
        opp.Name = 'test';
        opp.StageName = '40 - Demo Termin vereinbart';
        opp.Override_Region__c = 'München';
        opp.CloseDate = System.today();
        opp.z_Vertragsbeginn__c = System.today().addDays(-91);
        insert opp;
        
        Opportunity opp1 = new Opportunity ();
        opp1.Name = 'test';
        opp1.StageName = '40 - Demo Termin vereinbart';
        opp1.Override_Region__c = 'München';
        opp1.CloseDate = System.today();
     	opp1.z_Vertragsbeginn__c = System.today().addDays(-35);
        insert opp1;
        
        Opportunity opp2 = new Opportunity ();
        opp2.Name = 'test';
        opp2.StageName = '40 - Demo Termin vereinbart';
        opp2.Override_Region__c = 'München';
        opp2.CloseDate = System.today();
        opp2.z_Vertragsbeginn__c = System.today();
        insert opp2;
}



Null test 

@isTest static void getOpportuntiiesNull(){
        
        List<Opportunity> tstOpps = new List <Opportunity>();         
        
        tstOpps = [SELECT Name, Account.Name, Stagename, CloseDate, z_Vertragsbeginn__c 
                   FROM Opportunity WHERE OwnerId =:UserInfo.getUserId() AND IsClosed = false AND z_Vertragsbeginn__c = THIS_Month];
        
        system.assertEquals(1, tstOpps.size());
        TaskList tstCtrl = new TaskList();
        Test.startTest();
        system.assertEquals(0, tstCtrl.getOpportuntiies().size());
        Test.stopTest();
    }

 
Meghna Vijay 7Meghna Vijay 7
Hi Maria,

In the controller method query why don't you use only z_Vertragsbeginn__c < Last_Month instead of z_Vertragsbeginn__c <= Last_Month, actually it may be possible that both THIS_MONTH and LAST_MONTH = 00:00:00. Can you try it and let me know?

Thanks
Marry SteinMarry Stein
Hi Meghna,
thanks for your response ! I have changed the controller methode to z_Vertragsbeginn__c < THIS_MONTH.

It does not work :( 

The problem is, that my test methode catches all records in my testsetup methode. I do not have to query any records in my tes methode, because it makes no different. That's the point i do not unterstand. I thought i have to create test records and select them in each test methode... 

Greetings Marry
Meghna Vijay 7Meghna Vijay 7
Is @seeAllData=false in the test class?
 
Meghna Vijay 7Meghna Vijay 7
One more question it says IsClosed = false i think when we set CloseDate in opportunity then isClosed = true right? 
Marry SteinMarry Stein
Is @seeAllData=false in the test class? yep, thats standard 

nope IsClosed depends on the Opportunity Stage
 
Meghna Vijay 7Meghna Vijay 7
So what you are saying is that tstOpps.Size() should be 1 but it is returning 3 hence when you are calling getOpportuntiies it should return null but is returning size of 2 right?
 
Marry SteinMarry Stein
yep exatctly, and i dont unterstand why. The tstCtrl.getOpportuntiies() methode acess all records of the testsetup methode 
Meghna Vijay 7Meghna Vijay 7
Why don't you just change the name of the opportunity records to 'OppName1', 'OppName2' and 'OppName3' and use soql like :- [SELECT Name, Account.Name, Stagename, CloseDate, z_Vertragsbeginn__c
   FROM Opportunity WHERE OwnerId =:UserInfo.getUserId() AND IsClosed = falseAND z_Vertragsbeginn__c = THIS_Month AND Name='OppName3']; It may be because  z_Vertragsbeginn__c = THIS_Month is setting TRUE for all the 3 records and there is no need to add OwnerId filter condition in the SOQL. When you are creating the test dataand running it why is there a need to use it at all. It will run in your context only.
Thanks


 
Marry SteinMarry Stein
Nope, thats not the fault...
the soql works like it should.

        system.assertEquals(1, tstOpps.size());

The problem is, that the methode does not care about the query. It catches all records in my testsetup methode...No matter which records i query in my soql statement

 
SUBHASIS DHAR 9SUBHASIS DHAR 9
Is it possible to write a method within @testsetup method ? I am trying to something like this :
@istest
public class ManagingApex1Test {
   public   integer accountCount = 2;
   public   integer contactsOnEachAccount = 3;

@testSetup
      static void  dataSetUp() {
      public void CreateTestAccounts(Integer accountCount , integer contactsOnEachAccount ) {
 /* code for inserting acounts & contacts */
}
}
However I am getting error "non static method cannot be referenced from a static context"  . Kindly advise.