You need to sign in to do that
Don't have an account?
Gorky
Try to simulate object pass by trigger.new on a TestMethod
Hello Everybody,
I'm really newby whit Apex, this is my first testMethod and I'm stuck with this. Maybe anyone can help me?
The method I want to test called checkOrder waits for a Opportunity object who is sent by a call in a before update trigger.
I try to simulate this on my testMethod but obviously I'm not doing this well 'cause in the call to checkIntranetOrder inside my test method the for condition never begins.
What I'm doing wrong? What is the way to simulate the pass of a Opportunity object as sent by a trigger.new or trigger.old?
Thanks in advance
The code:
public static testMethod void TestcheckIntranetOrder() {
Opportunity[] lOp = [Select Id,Order__c from Opportunity where Id = '006R000000286W2']; --> Make a Opportunity whit the values I need
chekOrder(lOp); --> Call chekOrder Method
}
public static void checkOrder(Opportunity[] accs){
for (Opportunity a:accs){ --> Want a opportunity
if (a.Order__c!=NULL){
a.addError('Alert');
}
}
}
}
I'm really newby whit Apex, this is my first testMethod and I'm stuck with this. Maybe anyone can help me?
The method I want to test called checkOrder waits for a Opportunity object who is sent by a call in a before update trigger.
I try to simulate this on my testMethod but obviously I'm not doing this well 'cause in the call to checkIntranetOrder inside my test method the for condition never begins.
What I'm doing wrong? What is the way to simulate the pass of a Opportunity object as sent by a trigger.new or trigger.old?
Thanks in advance
The code:
public static testMethod void TestcheckIntranetOrder() {
Opportunity[] lOp = [Select Id,Order__c from Opportunity where Id = '006R000000286W2']; --> Make a Opportunity whit the values I need
chekOrder(lOp); --> Call chekOrder Method
}
public static void checkOrder(Opportunity[] accs){
for (Opportunity a:accs){ --> Want a opportunity
if (a.Order__c!=NULL){
a.addError('Alert');
}
}
}
}
Using a <list>
List<Opportunity> leads = new List<Opportunity>();
leads.add(new Opportunity(Order__c='12332'));
checkOrder(leads);
Thanks ! :)
In your TestMethod, you should actually create a new Object, then insert it. This will cause your trigger to activate. You should then do some tests (eg System.Assert) to check that your trigger code did what you expected.
The system will automatically roll-back the Object you created, since it was part of a TestMethod.
For an example, see the Testing section of the Introduction to Apex article.