salesforce测试类与被测试类的编写
public class AutoPostDemoUnit {
@InvocableMethod(label='AutoPostDemoUnit' description='AutoPostDemoUnit')//加这个注释的原因是可以通过REST API调用这个方法 就是通过Label标签的值来调用被标记的这个方法
public static void postDemoUnit(List<Contact> con)
{
//system.debug('AutoPostDemoUnit-test:' + con[0].Id);
try{
//获取查询到的数据
List<Contact> st = new List<Contact>();//泛型集合
st=[SELECT AccountId,Id, FirstName, LastName, Email, Phone, MobilePhone, Delivery_Address__c, Delivery_City__c, Delivery_State_Province__c, Delivery_Post_Code__c,
Delivery_Country__c FROM Contact WHERE Id =:con[0].Id];
if(st != null && st.size() > 0){//这个条件有什么用
List<Account> ac = new List<Account>();
ac = [SELECT Name FROM Account WHERE Id = :st[0].AccountId];
//组装contactdetail数据
Map<String,String> ap1=new Map<String,String>();
ap1.put('id',st[0].Id);
ap1.put('lastname',st[0].LastName);
ap1.put('firstname',st[0].FirstName);
ap1.put('email',st[0].Email);
ap1.put('phone',st[0].Phone);
ap1.put('mobile',st[0].MobilePhone);
ap1.put('delivery_address_c',st[0].Delivery_Address__c);
ap1.put('delivery_city_c',st[0].Delivery_City__c);
ap1.put('delivery_state_province_c',st[0].Delivery_State_Province__c);
ap1.put('delivery_post_code_c',st[0].Delivery_Post_Code__c);
ap1.put('delivery_country_c',st[0].Delivery_Country__c);
ap1.put('operator',UserInfo.getUsername());
//组装accountdetail数据
Map<String,String> ap2= new Map<String,String>();
ap2.put('id',st[0].AccountId);
ap2.put('companyname',ac[0].Name);
//组装action数据
Map<String,String> ap3= new Map<String,String>();
ap3.put('status','create');
//把上面三种数据组装成DEMOrequest数据
Map<String,Object> ap = new Map<String,Object>();
ap.put('action',ap3);
ap.put('accountdeatil',ap2);
ap.put('contactdetail',ap1);
//进行最后组装
Map<String,Object> ap0 = new Map<String,Object>();
ap0.put('DEMOreqest',ap);
String s= Json.serialize(ap0);//把组装好的Map集合转化成Jason格式的字符串
//System.debug(s);//测试输出的数据是不是Jason格式的字符串
postData(s);//调用发送数据发法
}
}catch(exception e){
sendEmail(new string[] {'john.ml.zhou@qisda.com'},'AutoPostDemoUnit Failed','Error Message:'+e.getMessage());
}
}
//发送数据方法
@future(callout=true)//@future例如:在对外部服务进行异步Web服务调出时,可以使用future注释。如果没有注释,Web服务调出将从执行Apex代码的同一线程执行,并且在调出完成之前不会发生任何额外的处理(同步处理)。
在APEX函数中调用外部网络服务时,可以定义该函数为Future,并加入callout=true,例如:
@future(callout=true)
public static void callWebService() {
String result = ExampleWebServiceClass.getWebServiceResult();
}
通过这种方式,此函数不需要等待网络服务的回应,从而继续执行其他的功能。
public static void postData(String s){
//获取邮件地址,并转化成数组形式
String strs = Label.DemoUnitEmailReciver;
List<String> st = strs.split(';');
String[] sts = new List<String>(st);
//发送组装成的json格式字符串的数据
String Url = Label.DemoUnitAPI;//获取URL地址
String Body = s;//获取传递过来的字符串
HttpRequest hr = new HttpRequest();
hr.setHeader('Content-Type','application/x-www-form-urlencoded');//确认Content-Type
hr.setEndpoint(Url);
hr.setMethod('POST');
String str = EncodingUtil.base64Encode(Blob.valueOf(Body));
hr.setbody('info='+str);
String resultJson = '';
if(!Test.isRunningTest()){//这个条件是干什么的?
Http http = new Http();
HTTPResponse response = new HTTPResponse();
response = http.send(hr);
//解析返回的json格式的响应结果
resultJson = response.getBody();
}else{
resultJson = '{"Response":{"result_code":"1","result":"响应失败"}}';
}
Map<String,Object> ap = (Map<String,Object>)Json.deserializeUntyped(resultJson);
Map<String,Object> ap1=(Map<String,Object>)ap.get('Response');
if(ap1.get('result_code') != '0'){
//如果result_code不等于0,则发送邮件给操作者
sendEmail(sts,'AutoPostDemoUnit Failed',ap1.get('result').toString());//调用发送邮件的方法
}
}
//发送邮件方法
public static void sendEmail(string[] emailTo, string sSubject, string sBody){
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();//这一句是什么意思?
email.setToAddresses(emailTo);//设置收件人地址
email.setSubject(sSubject);//设置标题
email.setHtmlBody(sBody);//设置邮件内容
try{
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });//发送邮件//这一行代码不理解是怎么用的
}catch(exception e){
System.debug('sendEmail:' + e.getMessage());//e.getMessage()方法是怎么用的。
}
}
}
测试类:
@isTest
class AutoPostDemoUnit_Test {
@isTest static void AutoPostDemoUnit_Test()
{
test.startTest();
Account tempAccount = new Account(Name='John', Region__c='BQA', NS__c='BQA');//造这个数据的时候为什么要加上Region__c='BQA',NS__c='BQA'
insert tempAccount;//测试类为什么要这样插入数据 插入数据的时候为什么不写入Id,Id为什么是自动生成的
Contact tempContact = new Contact(AccountId=tempAccount.Id,FirstName='John',LastName='Zhou',Email='john.ml.zhou@qisda.com',Phone='+86178286620',MobilePhone='18226126764', Delivery_Address__c='qisda', Delivery_City__c='suzhou', Delivery_State_Province__c='AL', Delivery_Post_Code__c='30256d', Delivery_Country__c='United States');
insert tempContact;
List<Contact> con = new List<Contact>();
Contact c = new Contact();
c.Id = tempContact.Id;
con.add(c);
AutoPostDemoUnit.postDemoUnit(con);//传入数组集合对象
test.stopTest();
}
}
List<ID> ids
List<Account> accounts = [SELECT Name FROM Account WHERE Id in :ids];//Id : ids意思是Id等于ids集合里的全部id,相当于select执行了很多次查询,然后用Account泛型集合接收Name;
for(Account account : accounts){
accountNames.add(account.Name);
}
return accountName;
}
}
本文来自博客园,作者:白菜豆腐粉丝汤,转载请注明原文链接:https://www.cnblogs.com/v178286621/p/13822242.html

浙公网安备 33010602011771号