测试 class Apex 触发更新
Test class for Apex Trigger for update
我是 Apex Development 的新手。我想为我的 Apex 触发器编写一个测试类。
我正在与您分享我的代码:
trigger ClosedOpportunityTrigger on Opportunity (before update) {
for(Opportunity o:Trigger.New) {
if(o.Probability==100 && o.Invoiced__c == true)
{
Attachment a = new Attachment();
try {
a = [Select Id, Name from Attachment where ParentId =:o.Id];
}
catch(Exception e) {
a = null;
}
if (a == null)
o.addError('Add an attachment before you close the Opportunity');
}
}
}
有两件事需要完成:-
1. 在 'Opportunity' 对象中创建一条记录。使用代码更新它。
2. 创建附件。这里是link供大家参考https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ
祝你好运
public static void testmethod(){
opportunity opp = new opportunity()
//change whatever fields u need to make probability 100%
opp.stage = 'Closed Won';
opp.Invoiced__c == true;
try{
insert opp
}
catch(Exception e){
string errormessage = e.getMessage();
}
//now that u know how to do it, do a positive test where you also add an
//attachment
//as a side note, your catch block will never fire, because you aren't
//catching any exceptions you are just getting a null string
}
我是 Apex Development 的新手。我想为我的 Apex 触发器编写一个测试类。
我正在与您分享我的代码:
trigger ClosedOpportunityTrigger on Opportunity (before update) {
for(Opportunity o:Trigger.New) {
if(o.Probability==100 && o.Invoiced__c == true)
{
Attachment a = new Attachment();
try {
a = [Select Id, Name from Attachment where ParentId =:o.Id];
}
catch(Exception e) {
a = null;
}
if (a == null)
o.addError('Add an attachment before you close the Opportunity');
}
}
}
有两件事需要完成:- 1. 在 'Opportunity' 对象中创建一条记录。使用代码更新它。 2. 创建附件。这里是link供大家参考https://developer.salesforce.com/forums/?id=906F00000008yzKIAQ
祝你好运
public static void testmethod(){
opportunity opp = new opportunity()
//change whatever fields u need to make probability 100%
opp.stage = 'Closed Won';
opp.Invoiced__c == true;
try{
insert opp
}
catch(Exception e){
string errormessage = e.getMessage();
}
//now that u know how to do it, do a positive test where you also add an
//attachment
//as a side note, your catch block will never fire, because you aren't
//catching any exceptions you are just getting a null string
}