You need to sign in to do that
Don't have an account?
Test class for trigger.isUpdate and trigger.isInsert
Hi,
I have a trigger that generates the password based on a picklist value I had some problem with the test class can any one help.
Trigger
trigger Sendpassword on Contact (before update, before insert) {
if(!Validator_cls.hasAlreadyDone()){
string tokenM;
if(trigger.isInsert){
List<Contact> con =Trigger.new;
Integer len = 8;
Blob blobkey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
tokenM = key.substring(0,len).toUpperCase();
for (Contact tpwd : con) {
if(tpwd.Temporary_KL_Login__c=='Enable and Send Password' )
tpwd.Temporary_KL_Login_Password__c =tokenM;
}
}
if(trigger.isUpdate){
Contact conn= Trigger.old[0];
List<Contact> con =Trigger.new;
Integer len = 8;
Blob blobkey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
tokenM = key.substring(0,len).toUpperCase();
for (Contact tpwd : con) {
if(tpwd.Temporary_KL_Login__c=='Enable and Send Password' && conn.Temporary_KL_Login__c != tpwd.Temporary_KL_Login__c )
tpwd.Temporary_KL_Login_Password__c =tokenM;
}
}
Validator_cls.setAlreadyDone();
}
}
Test class
@isTest
Public class Sendpassword
{
static testMethod void Sendpassword()
{
Contact con = new Contact(LastName='Test', Title = 'testt' ,
Phone = '4561230789' , Email = 'qwwervvsv@gmail.com' ,Temporary_KL_Login_Password__c='78B910FC' );
insert con;
con.Phone ='4561230789';
update con;
Validator_cls.hasAlreadyDone();
}
}
i am getting 54% code coverage unable to cover the things inside the "trigger.isUpdate"
I used another class "Validator_cls" to make the trigger fire only once
Validator_cls
global class Validator_cls{
private static boolean blnAlreadyDone = false;
public static boolean hasAlreadyDone(){
return blnAlreadyDone;
}
public static void setAlreadyDone() {
blnAlreadyDone = true;
}
}
Hi,
Please let me know which lines are not covered i.e red in color.
Regards,
Rajesh.
trigger Sendpassword on Contact (before update, before insert) {
if(!Validator_cls.hasAlreadyDone()){
string tokenM;
if(trigger.isInsert){
List<Contact> con =Trigger.new;
Integer len = 8;
Blob blobkey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
tokenM = key.substring(0,len).toUpperCase();
for (Contact tpwd : con) {
if(tpwd.Temporary_KL_Login__c=='Enable and Send Password' )
tpwd.Temporary_KL_Login_Password__c =tokenM;
}
}
if(trigger.isUpdate){
Contact conn= Trigger.old[0];
List<Contact> con =Trigger.new;
Integer len = 8;
Blob blobkey = crypto.generateAesKey(128);
String key = EncodingUtil.convertToHex(blobKey);
tokenM = key.substring(0,len).toUpperCase();
for (Contact tpwd : con) {
if(tpwd.Temporary_KL_Login__c=='Enable and Send Password' && conn.Temporary_KL_Login__c != tpwd.Temporary_KL_Login__c )
tpwd.Temporary_KL_Login_Password__c =tokenM;
}
}
Validator_cls.setAlreadyDone();
}
}
HI,
In update case the "Validator_cls.hasAlreadyDone()" value become true and the code is not going inside the loop .
modified your test class with the following code:
@isTest
Public class Sendpassword {
static testMethod void Sendpassword() {
Contact con = new Contact(
LastName='Test',
Title = 'testt' ,
Phone = '4561230789' ,
Email = 'qwwervvsv@gmail.com' ,
Temporary_KL_Login_Password__c='78B910FC'
);
insert con;
Validator_cls obj = new Validator_cls(); / ** reset the blnAlreadyDone variable to false.**/
Validator_cls.setAlreadyDoneToFalse();
con.Phone ='4561230789';
update con;
}
}
Thanks
Puja
Mindfire Solution
www.mindfiresolutions.com
Hi,
Where "setAlreadyDoneToFalse" method define in "Validator_cls" class like:
global class Validator_cls{
private static boolean blnAlreadyDone = false;
public static boolean hasAlreadyDone(){
return blnAlreadyDone;
}
public static void setAlreadyDone() {
blnAlreadyDone = true;
}
public static void setAlreadyDoneToFalse() { /** Add new Method to reset it again false.**/
blnAlreadyDone = false;
}
}
and then use this method in test class
@isTest
Public class Sendpassword {
static testMethod void Sendpassword() {
Contact con = new Contact(
LastName='Test',
Title = 'testt' ,
Phone = '4561230789' ,
Email = 'qwwervvsv@gmail.com' ,
Temporary_KL_Login_Password__c='78B910FC'
);
insert con;
/ ** reset the blnAlreadyDone variable to false.**/
Validator_cls.setAlreadyDoneToFalse();
con.Phone ='4561230789';
update con;
}
}
Thanks
Puja
Mindfire Solution
www.mindfiresolutions.com