You need to sign in to do that
Don't have an account?

Testcase Parameters Help
global class Test{
webService static String Attachment(string obj, string id) {
String retRes = '';
String[] emails = new String[]{};
String emailList = null;
String theHTML = 'The report header goes here!';
PageReference thepage = null;
Blob bpagePDF = null;
String bpageHTML = theHTML;
Integer i = 0;
Contract__c con = [Select id from Contract__c where Id = :Id];
String AccountName = con.Opportunity__r.Account.Name;
if(obj=='Account'){
for(Contact c : [Select Email from Contact Where AccountId = :id]) {
emails.add(c.Email);
if(i==0) emailList = c.Email;
else emailList = emailList + ', ' + c.Email;
i++;
}
}
else if(obj=='Contract'){
for(Contact c : [Select Email, Account.Name from Contact Where AccountId = :con.Opportunity__r.AccountID]) {
emails.add(c.Email);
if(i==0) emailList = c.Email;
else emailList = emailList + ', ' + c.Email;
i++;
}
}
else{
retRes = 'Call to Send email is invalid. Please contact your developer. Error 112.' + obj;
return retRes;
}
try{
thepage = new PageReference('/apex/test?id='+id);
thePage.setRedirect(true);
bpagePDF = thepage.getContentAsPDF();
bpageHTML = thepage.getContent().toString();
} catch(exception e){
retRes = 'Page Error: '+e.getMessage();
return retRes;
}
if(emails.size()==0) emails.add([Select Email from User where Id =:UserInfo.getUserID()].email);
try {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emails);
mail.setReplyTo('no-reply@mas.com');
mail.setSenderDisplayName('test');
mail.setSubject('test');
mail.setHtmlBody(bpageHTML);
mail.setWhatId(id);
Messaging.EmailFileAttachment efaPage = new Messaging.EmailFileAttachment();
efaPage.setContentType('application/pdf');
efaPage.setFileName(AccountName+' test.pdf');
efaPage.setinline(true);
efaPage.setBody(bpagePDF);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efaPage}); //efaTable
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
retRes = 'Mail Sent Successfully to: ' + emailList;
retRes = retRes + '\nFor Account: ' + AccountName;
} catch(EmailException ex){
System.debug(ex.getMessage());
retRes = 'Email Error: '+ex.getMessage();
} catch(Exception ex) {
System.debug('Error-'+ ex);
retRes = 'Exception error: '+ex.getMessage();
}
return retRes;
}
}
How to pass parameters in testcase. Above is my class.
@isTest
private class TestSuite {
static testMethod void runPositiveTestCases(
Account obj = new account();
obj.name = 'test account';
//assign all the relavant fields
insert obj;
string str = Test.Attachment('Account',obj.id);
}
static testMethod void runPositiveTestCases1(
Account obj = new account();
obj.name = 'test account';
//assign all the relavant fields
insert obj;
contract objc = new contract();
objc.name = 'test contract';
objc.account = obj.id;
//assign all the relavant fields
insert objc;
string str = Test.Attachment('Contract',obj.id);
}
}
also need a change in the class in this line Contract__c con = [Select id from Contract__c where Id = :Id];
Contract__c con;
String AccountName;
if(obj == 'Contract'){
con = [Select id from Contract__c where Id = :Id];
AccountName = con.Opportunity__r.Account.Name;
}
All Answers
for unit test.. you can call Test.Attachment('Account', give some account id with in the org);
Testmethod.
@isTest
private class TestSuite {
static testMethod void runPositiveTestCases(
Account obj = new account();
obj.name = 'test account';
//assign all the relavant fields
insert obj;
string str = Test.Attachment('Account',obj.id);
}
static testMethod void runPositiveTestCases1(
Account obj = new account();
obj.name = 'test account';
//assign all the relavant fields
insert obj;
contract objc = new contract();
objc.name = 'test contract';
objc.account = obj.id;
//assign all the relavant fields
insert objc;
string str = Test.Attachment('Contract',obj.id);
}
}
also need a change in the class in this line Contract__c con = [Select id from Contract__c where Id = :Id];
Contract__c con;
String AccountName;
if(obj == 'Contract'){
con = [Select id from Contract__c where Id = :Id];
AccountName = con.Opportunity__r.Account.Name;
}
Thanks For reply.
ill work accordingly. Get back to you.
Hi Kiran,
able to get 45% of codecoverage.
Unable to cover email code..
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(emails);
mail.setReplyTo('no-reply@mas.com');
mail.setSenderDisplayName('test');
mail.setSubject('test');
mail.setHtmlBody(bpageHTML);
mail.setWhatId(id);
Messaging.EmailFileAttachment efaPage = new Messaging.EmailFileAttachment();
efaPage.setContentType('application/pdf');
efaPage.setFileName(AccountName+' test.pdf');
efaPage.setinline(true);
efaPage.setBody(bpagePDF);
mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efaPage}); //efaTable
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
Error: Compile Error: Method does not exist or incorrect signature:testSendEmail() at line 58 column 9