You need to sign in to do that
Don't have an account?
Marry 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...
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(); }
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
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
nope IsClosed depends on the Opportunity Stage
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
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
@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.