You need to sign in to do that
Don't have an account?
Allen2
Help to complete the test class for the below code
APEX CLASS
trigger AvoidDuplicateUsageEntry on Amendment__c (before insert)
{
for (Amendment__c amendment: Trigger.new)
{
if (amendment.Override_Warning__c == false)
{
try
{
Amendment__c[] a = [SELECT a.CreatedById, a.CreatedDate, a.Amendment__c from Amendment__c a where a.Resp__c = :amendment.Resp__c and a.LSD__c = :amendment.LSD__c ORDER BY CreatedDate desc];
if (a.size() > 0)
{
User u = [SELECT u.Name from User u where id = :a[0].CreatedById];
String amendmentStr = String.escapeSingleauotes(a[0].Amendment__c);
amendmentStr = amendmentStr.replace('\"', '\\\"');
String userStr = String.escapeSingleauotes(u.Name);
userStr = userStr.replace('\"', '\\\"');
String dateStr = a[0].CreatedDate.format('MM/dd/yyyy hh:mm a');
String errorJSON = 'var errorJSON = {timesUsed: ' + a.size() + ', amendment: \"' + amendmentStr + '\", user: \"' + userStr + '\", time: \"' + dateStr + '\"};';
amendment.Resp__c.addError(errorJSON);
} // endif
}
catch (aueryException e)
{
}
} // endif
} // endfor
}
TEST CLASS
@isTest
public class test_Test {
static testMethod void avoidDuplicateEntryTest() {
Profile p = [select id from profile where name='System Administrator'];
User u = new User(alias = 'standt', email = 'standarduser@testorg.com', emailencodingkey = 'UTF-8', lastname = 'Testing', languagelocalekey = 'en_US',
localesidkey = 'en_US', profileid = p.Id, timezonesidkey = 'America/Los_Angeles',
username = 'testclassuser@testorg.com');
insert u;
System.runAs(u){
Test.startTest();
Resp__c resp = new Resp__c();
resp.status__c = 'Draft';
insert resp;
LSD__c ref = new LSD__c();
ref.Due_Date__c = system.today();
insert ref;
list <Amendment__c> as = new list <Amendment__c>();
Amendment__c amend = new Amendment__c();
amend.Override_Warning__c = False;
amend.LSD__c = ref.Id;
amend.Resp__c = resp.Id;
amend.Amendment__c = 'TestText';
amend.CreatedById = u.Id;
as.add(amend);
Amendment__c amend1 = new Amendment__c();
amend1.Override_Warning__c = False;
amend1.LSD__c = ref.Id;
amend1.Resp__c = resp.Id;
amend1.Amendment__c = 'TestText1';
amend1.CreatedById = u.Id;
as.add(amend1);
insert as;
system.debug('size' + as.size());
Test.stopTest();
}
}
}
I am not able to cover the bold & underlined part of my apex class. I am inserting the list in my test class but while debugging I m not getting any value in the list in my test class so unable to cover the rest of the part.
Could anyone can help me in this what is wrong I am doing here.. Please....
trigger AvoidDuplicateUsageEntry on Amendment__c (before insert)
{
for (Amendment__c amendment: Trigger.new)
{
if (amendment.Override_Warning__c == false)
{
try
{
Amendment__c[] a = [SELECT a.CreatedById, a.CreatedDate, a.Amendment__c from Amendment__c a where a.Resp__c = :amendment.Resp__c and a.LSD__c = :amendment.LSD__c ORDER BY CreatedDate desc];
if (a.size() > 0)
{
User u = [SELECT u.Name from User u where id = :a[0].CreatedById];
String amendmentStr = String.escapeSingleauotes(a[0].Amendment__c);
amendmentStr = amendmentStr.replace('\"', '\\\"');
String userStr = String.escapeSingleauotes(u.Name);
userStr = userStr.replace('\"', '\\\"');
String dateStr = a[0].CreatedDate.format('MM/dd/yyyy hh:mm a');
String errorJSON = 'var errorJSON = {timesUsed: ' + a.size() + ', amendment: \"' + amendmentStr + '\", user: \"' + userStr + '\", time: \"' + dateStr + '\"};';
amendment.Resp__c.addError(errorJSON);
} // endif
}
catch (aueryException e)
{
}
} // endif
} // endfor
}
TEST CLASS
@isTest
public class test_Test {
static testMethod void avoidDuplicateEntryTest() {
Profile p = [select id from profile where name='System Administrator'];
User u = new User(alias = 'standt', email = 'standarduser@testorg.com', emailencodingkey = 'UTF-8', lastname = 'Testing', languagelocalekey = 'en_US',
localesidkey = 'en_US', profileid = p.Id, timezonesidkey = 'America/Los_Angeles',
username = 'testclassuser@testorg.com');
insert u;
System.runAs(u){
Test.startTest();
Resp__c resp = new Resp__c();
resp.status__c = 'Draft';
insert resp;
LSD__c ref = new LSD__c();
ref.Due_Date__c = system.today();
insert ref;
list <Amendment__c> as = new list <Amendment__c>();
Amendment__c amend = new Amendment__c();
amend.Override_Warning__c = False;
amend.LSD__c = ref.Id;
amend.Resp__c = resp.Id;
amend.Amendment__c = 'TestText';
amend.CreatedById = u.Id;
as.add(amend);
Amendment__c amend1 = new Amendment__c();
amend1.Override_Warning__c = False;
amend1.LSD__c = ref.Id;
amend1.Resp__c = resp.Id;
amend1.Amendment__c = 'TestText1';
amend1.CreatedById = u.Id;
as.add(amend1);
insert as;
system.debug('size' + as.size());
Test.stopTest();
}
}
}
I am not able to cover the bold & underlined part of my apex class. I am inserting the list in my test class but while debugging I m not getting any value in the list in my test class so unable to cover the rest of the part.
Could anyone can help me in this what is wrong I am doing here.. Please....
As for providing coverage for those lines, you could try creating records in a @testSetup before running that test method.
Please help me if can do any changes in test class and cover that part.
In your test class, there are two Amendments inserted, but that fires the Trigger in a before insert context. This means that those records will not be returned when it reaches that query line, so it will not pass that if check.
I believe you can also get coverage for that section in this scenario if you change before insert to after insert so those test records will be returned. Or you could try the @testSetup method where you create existing Amendment records first, then test the insertion of additional Amendment records in a test method.
I tried below code... but nothing happened.. still the same result..
@isTest
public class test_Test {
static void setupData(){
Profile p = [select id from profile where name='System Administrator'];
User u = new User(alias = 'standt', email = 'standarduser@testorg.com', emailencodingkey = 'UTF-8', lastname = 'Testing', languagelocalekey = 'en_US',
localesidkey = 'en_US', profileid = p.Id, timezonesidkey = 'America/Los_Angeles',
username = 'testclassuser@testorg.com');
insert u;
Resp__c resp = new Resp__c();
resp.status__c = 'Draft';
insert resp;
LSD__c ref = new LSD__c();
ref.Due_Date__c = system.today();
insert ref;
//list <Amendment__c> as = new list <Amendment__c>();
Amendment__c amend = new Amendment__c();
amend.Override_Warning__c = False;
amend.LSD__c = ref.Id;
amend.Resp__c = resp.Id;
amend.Amendment__c = 'TestText';
amend.Source__c = 'Responses';
amend.CreatedById = u.Id;
insert amend;
}
static testMethod void avoidDuplicateEntryTest() {
Test.startTest();
setupData();
Amendment__c amend = [Select Id from Amendment__c where Source__c = 'Responses'];
amend.Amendment__c = 'Testing';
update amend;
Test.stopTest();
}
}