You need to sign in to do that
Don't have an account?
code coverage
Hi,
how to write test method for this class,
public class Emailnewpurchaserecord
{
set<id> pset=new set<id>();
map<string,string> emap=new map<string,string>();
public Emailnewpurchaserecord()
{
for(profile p:[select id,name from profile where name=:'Executive User'])
pset.add(p.id);
for(profile p1:[select id,name from profile where name=:'Factory User'])
pset.add(p1.id);
system.debug(pset+'p===');
for(user u:[select id,name,Email,profileid from user where profileid in : pset])
{
if(u.id!=null)
emap.put(u.name,u.email);
}
system.debug(emap+'e===');
}
public void send()
{
if(emap.size()>0)
{
for(string s:emap.keyset())
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {emap.get(s)};
mail.setToAddresses(toAddresses);
mail.setReplyTo('support@acme.com');
mail.setSenderDisplayName(s);//here if we specify s that will taken the name which specified in object
mail.setSubject('Purchase Order is Placed');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('hi');
mail.setHtmlBody('New Purchase Order is Created');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
}
Thank you.
Hi
As in above test class that Account will not contains lastname and firstname but contain name .make sure it contact or not.
Try this
@isTest
Private class testemailfun_test{
static testMethod void testemailfun()
{
Profile p = [select id from profile where name='Executive User'];
UserRole r = [Select id from userrole limit 1];
User u = new User(alias = 'standt', email='standarduser3411@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='standarduser434@testorg.com');
insert u;
Account a = new Account(name='Terry');
// insert a;
Profile p1 = [select id from profile where name='Factory User'];
UserRole r1 = [Select id from userrole limit 1];
User u1 = new User(alias = 'standt', email='standarduser1231@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p1.Id, userroleid = r1.Id,
timezonesidkey='America/Los_Angeles',
username='standarduser565@testorg.com');
insert u1;
Account a1 = new Account(name='Terry');
// insert a1;
Emailnewpurchaserecord test1=new Emailnewpurchaserecord();
test1.send();
}
Regards,
Rajesh.
All Answers
Hi,
Try the below code snippet as reference:
public class Emailnewpurchaserecord
{
set<id> pset=new set<id>();
map<string,string> emap=new map<string,string>();
public Emailnewpurchaserecord()
{
for(profile p:[select id,name from profile where name=:'Executive User'])
pset.add(p.id);
for(profile p1:[select id,name from profile where name=:'Factory User'])
pset.add(p1.id);
system.debug(pset+'p===');
for(user u:[select id,name,Email,profileid from user where profileid in : pset])
{
if(u.id!=null)
emap.put(u.name,u.email);
}
system.debug(emap+'e===');
}
public void send()
{
if(emap.size()>0)
{
for(string s:emap.keyset())
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {emap.get(s)};
mail.setToAddresses(toAddresses);
mail.setReplyTo('support@acme.com');
mail.setSenderDisplayName(s);//here if we specify s that will taken the name which specified in object
mail.setSubject('Purchase Order is Placed');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('hi');
mail.setHtmlBody('New Purchase Order is Created');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
/*--------- test method ---------------------*/
static testMethod void testemailfun()
{
Profile p = [select id from profile where name='Executive User'];
UserRole r = [Select id from userrole limit 1];
User u = new User(alias = 'standt', email='standarduser1@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='standarduser@testorg.com');
Account a = new Account(Firstname='Terry', Lastname='Testperson');
insert a;
insert u;
Profile p1 = [select id from profile where name='Factory User'];
UserRole r1 = [Select id from userrole limit 1];
User u1 = new User(alias = 'standt', email='standarduser1@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p1.Id, userroleid = r1.Id,
timezonesidkey='America/Los_Angeles',
username='standarduser@testorg.com');
Account a1 = new Account(Firstname='Terry', Lastname='Testperson');
insert a1;
insert u1;
Emailnewpurchaserecord test1=new Emailnewpurchaserecord();
test1.send();
}
}
Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.
Thank you Ankit,
that code coverse only 41%.
thank you.
Hi
As in above test class that Account will not contains lastname and firstname but contain name .make sure it contact or not.
Try this
@isTest
Private class testemailfun_test{
static testMethod void testemailfun()
{
Profile p = [select id from profile where name='Executive User'];
UserRole r = [Select id from userrole limit 1];
User u = new User(alias = 'standt', email='standarduser3411@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p.Id, userroleid = r.Id,
timezonesidkey='America/Los_Angeles',
username='standarduser434@testorg.com');
insert u;
Account a = new Account(name='Terry');
// insert a;
Profile p1 = [select id from profile where name='Factory User'];
UserRole r1 = [Select id from userrole limit 1];
User u1 = new User(alias = 'standt', email='standarduser1231@testorg.com',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US', profileid = p1.Id, userroleid = r1.Id,
timezonesidkey='America/Los_Angeles',
username='standarduser565@testorg.com');
insert u1;
Account a1 = new Account(name='Terry');
// insert a1;
Emailnewpurchaserecord test1=new Emailnewpurchaserecord();
test1.send();
}
Regards,
Rajesh.
Thank you Rajesh, it is working.