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

Help with Test Class created.
Hello I am attempting to test a trigger I have created, but I am failing at line 43 of the Test class. Any ideas what may be happening here based on the code for the trigger and test class below? I am new to Apex code and developing in general, so I am hopful someone more experienced may be able to help.
The error code I am getting when testing the class is as follows: Class.InstallProdDuplicatePreventerTests.testInstallProdDuplicatePreventer: line 43, column 1
Trigger -
trigger InstallProdDuplicatePreventer on AVISPL_Client_Product__c
(before insert, before update) {
Map<String, AVISPL_Client_Product__c > IPMap = new Map<String, AVISPL_Client_Product__c >();
for (AVISPL_Client_Product__c IP : System.Trigger.new) {
// Make sure we don't treat an email address that
// isn't changing during an update as a duplicate.
if ((IP.Name != null) &&
(System.Trigger.isInsert ||
(IP.Name !=
System.Trigger.oldMap.get(IP.Id).Name))) {
// Make sure another new lead isn't also a duplicate
if (IPMap.containsKey(IP.Name)) {
IP.Name.addError('Another Installed Product has the '
+ 'same Name.');
} else {
IPMap.put(IP.Name, IP);
}
}
}
// Using a single database query, find all the leads in
// the database that have the same email address as any
// of the leads being inserted or updated.
for (AVISPL_Client_Product__c Ip2 : [SELECT Name FROM AVISPL_Client_Product__c
WHERE Name IN :IPMap.KeySet()]) {
AVISPL_Client_Product__c newIP = IPMap.get(IP2.Name);
newIP.Name.addError('A Installed Product with this name '
+ 'already exists.');
}
}
Test Class -
@IsTest
public class InstallProdDuplicatePreventerTests{
static testMethod void testInstallProdDuplicatePreventer() {
// First make sure there are no Installed Products already in the system
// that have the Name used for testing
Set<String> testName = new Set<String>();
testName.add('1213');
testName.add('1212');
testName.add('1211');
testName.add('1210');
testName.add('1209');
System.assert([SELECT count() FROM AVISPL_Client_Product__c
WHERE Name IN :testName] == 0);
// Seed the database with some Installed Products, and make sure they can
// be bulk inserted successfully.
AVISPL_Client_Product__c IP1 = new AVISPL_Client_Product__c(Name='Test1');
AVISPL_Client_Product__c IP2 = new AVISPL_Client_Product__c(Name='Test2');
AVISPL_Client_Product__c IP3 = new AVISPL_Client_Product__c(Name='Test3');
AVISPL_Client_Product__c[] IPs = new AVISPL_Client_Product__c[] {IP1, IP2, IP3};
insert IPs;
// Now make sure that some of these Installed Products can be changed and
// then bulk updated successfully. Note that IP1 is not
// being changed, but is still being passed to the update
// call. This should be OK.
IP2.Name = '12111';
IP3.Name = '33333';
update IPs;
// Make sure that single row Installed Prod duplication prevention works
// on insert.
AVISPL_Client_Product__c dup1 = new AVISPL_Client_Product__c(Name='33333');
try {
insert dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'String');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that single row Installed Prod duplication prevention works
// on update.
dup1 = new AVISPL_Client_Product__c(Id = IP1.Id, Name='Test1');
try {
update dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that bulk Installed Products duplication prevention works on
// insert. Note that the first item being inserted is fine,
// but the second and third items are duplicates. Note also
// that since at least one record insert fails, the entire
// transaction will be rolled back.
dup1 = new AVISPL_Client_Product__c(Name='Test1');
AVISPL_Client_Product__c dup2 = new AVISPL_Client_Product__c(Name='Test2');
AVISPL_Client_Product__c dup3 = new AVISPL_Client_Product__c(Name='Test3');
AVISPL_Client_Product__c[] dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
// System.assert(e.getDmlFields(1)[0] == 'Name');
System.assert(e.getDmlMessage(1).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that bulk Installed Prod duplication prevention works on
// update. Note that the first item being updated is fine,
// because the Name is new, and the second item is
// also fine, but in this case it's because the Name
// doesn't change. The third case is flagged as an
// error because it is a duplicate of the Name of the
// first Installed Prod value in the database, even though that value
// is changing in this same update call. Note also that since at least one record update
// fails, the entire transaction will be rolled back.
dup1 = new AVISPL_Client_Product__c(Id=IP1.Id, Name='TestDup1');
dup2 = new AVISPL_Client_Product__c(Id=IP2.Id, Name='TestDup2');
dup3 = new AVISPL_Client_Product__c(Id=IP3.Id, Name='TestDup3');
dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.debug(e.getNumDml());
System.debug(e.getDmlMessage(0));
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 2);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// inserting Installed Products. Note that this test also catches an
// attempt to insert a Installed Product where there is an existing
// duplicate.
dup1 = new AVISPL_Client_Product__c(Name='Test1Dup');
dup2 = new AVISPL_Client_Product__c(Name='Test2Dup');
dup3 = new AVISPL_Client_Product__c(Name='Test3Dup');
dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'Another new Installed Product has the same Name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
// System.assert(e.getDmlFields(1)[0] == 'Name');
System.assert(e.getDmlMessage(1).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// updating Installed Products. Note that this test also catches an attempt
// to update a Installed Product where there is an existing duplicate.
dup1 = new AVISPL_Client_Product__c(Id=IP1.Id, Name='TestDup1');
dup2 = new AVISPL_Client_Product__c(Id=IP2.Id, Name='TestDup4');
dup3 = new AVISPL_Client_Product__c(Id=IP3.Id, Name='TestDup2');
dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'Another new Installed Product has the same Name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
// System.assert(e.getDmlFields(1)[0] == 'Name');
System.assert(e.getDmlMessage(1).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
}
}
The error code I am getting when testing the class is as follows: Class.InstallProdDuplicatePreventerTests.testInstallProdDuplicatePreventer: line 43, column 1
Trigger -
trigger InstallProdDuplicatePreventer on AVISPL_Client_Product__c
(before insert, before update) {
Map<String, AVISPL_Client_Product__c > IPMap = new Map<String, AVISPL_Client_Product__c >();
for (AVISPL_Client_Product__c IP : System.Trigger.new) {
// Make sure we don't treat an email address that
// isn't changing during an update as a duplicate.
if ((IP.Name != null) &&
(System.Trigger.isInsert ||
(IP.Name !=
System.Trigger.oldMap.get(IP.Id).Name))) {
// Make sure another new lead isn't also a duplicate
if (IPMap.containsKey(IP.Name)) {
IP.Name.addError('Another Installed Product has the '
+ 'same Name.');
} else {
IPMap.put(IP.Name, IP);
}
}
}
// Using a single database query, find all the leads in
// the database that have the same email address as any
// of the leads being inserted or updated.
for (AVISPL_Client_Product__c Ip2 : [SELECT Name FROM AVISPL_Client_Product__c
WHERE Name IN :IPMap.KeySet()]) {
AVISPL_Client_Product__c newIP = IPMap.get(IP2.Name);
newIP.Name.addError('A Installed Product with this name '
+ 'already exists.');
}
}
Test Class -
@IsTest
public class InstallProdDuplicatePreventerTests{
static testMethod void testInstallProdDuplicatePreventer() {
// First make sure there are no Installed Products already in the system
// that have the Name used for testing
Set<String> testName = new Set<String>();
testName.add('1213');
testName.add('1212');
testName.add('1211');
testName.add('1210');
testName.add('1209');
System.assert([SELECT count() FROM AVISPL_Client_Product__c
WHERE Name IN :testName] == 0);
// Seed the database with some Installed Products, and make sure they can
// be bulk inserted successfully.
AVISPL_Client_Product__c IP1 = new AVISPL_Client_Product__c(Name='Test1');
AVISPL_Client_Product__c IP2 = new AVISPL_Client_Product__c(Name='Test2');
AVISPL_Client_Product__c IP3 = new AVISPL_Client_Product__c(Name='Test3');
AVISPL_Client_Product__c[] IPs = new AVISPL_Client_Product__c[] {IP1, IP2, IP3};
insert IPs;
// Now make sure that some of these Installed Products can be changed and
// then bulk updated successfully. Note that IP1 is not
// being changed, but is still being passed to the update
// call. This should be OK.
IP2.Name = '12111';
IP3.Name = '33333';
update IPs;
// Make sure that single row Installed Prod duplication prevention works
// on insert.
AVISPL_Client_Product__c dup1 = new AVISPL_Client_Product__c(Name='33333');
try {
insert dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'String');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that single row Installed Prod duplication prevention works
// on update.
dup1 = new AVISPL_Client_Product__c(Id = IP1.Id, Name='Test1');
try {
update dup1;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 0);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that bulk Installed Products duplication prevention works on
// insert. Note that the first item being inserted is fine,
// but the second and third items are duplicates. Note also
// that since at least one record insert fails, the entire
// transaction will be rolled back.
dup1 = new AVISPL_Client_Product__c(Name='Test1');
AVISPL_Client_Product__c dup2 = new AVISPL_Client_Product__c(Name='Test2');
AVISPL_Client_Product__c dup3 = new AVISPL_Client_Product__c(Name='Test3');
AVISPL_Client_Product__c[] dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
// System.assert(e.getDmlFields(1)[0] == 'Name');
System.assert(e.getDmlMessage(1).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that bulk Installed Prod duplication prevention works on
// update. Note that the first item being updated is fine,
// because the Name is new, and the second item is
// also fine, but in this case it's because the Name
// doesn't change. The third case is flagged as an
// error because it is a duplicate of the Name of the
// first Installed Prod value in the database, even though that value
// is changing in this same update call. Note also that since at least one record update
// fails, the entire transaction will be rolled back.
dup1 = new AVISPL_Client_Product__c(Id=IP1.Id, Name='TestDup1');
dup2 = new AVISPL_Client_Product__c(Id=IP2.Id, Name='TestDup2');
dup3 = new AVISPL_Client_Product__c(Id=IP3.Id, Name='TestDup3');
dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.debug(e.getNumDml());
System.debug(e.getDmlMessage(0));
System.assert(e.getNumDml() == 1);
System.assert(e.getDmlIndex(0) == 2);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// inserting Installed Products. Note that this test also catches an
// attempt to insert a Installed Product where there is an existing
// duplicate.
dup1 = new AVISPL_Client_Product__c(Name='Test1Dup');
dup2 = new AVISPL_Client_Product__c(Name='Test2Dup');
dup3 = new AVISPL_Client_Product__c(Name='Test3Dup');
dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
insert dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'Another new Installed Product has the same Name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
// System.assert(e.getDmlFields(1)[0] == 'Name');
System.assert(e.getDmlMessage(1).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
// Make sure that duplicates in the submission are caught when
// updating Installed Products. Note that this test also catches an attempt
// to update a Installed Product where there is an existing duplicate.
dup1 = new AVISPL_Client_Product__c(Id=IP1.Id, Name='TestDup1');
dup2 = new AVISPL_Client_Product__c(Id=IP2.Id, Name='TestDup4');
dup3 = new AVISPL_Client_Product__c(Id=IP3.Id, Name='TestDup2');
dups = new AVISPL_Client_Product__c[] {dup1, dup2, dup3};
try {
update dups;
System.assert(false);
} catch (DmlException e) {
System.assert(e.getNumDml() == 2);
System.assert(e.getDmlIndex(0) == 1);
System.assert(e.getDmlFields(0).size() == 1);
// System.assert(e.getDmlFields(0)[0] == 'Name');
System.assert(e.getDmlMessage(0).indexOf(
'Another new Installed Product has the same Name.') > -1);
System.assert(e.getDmlIndex(1) == 2);
System.assert(e.getDmlFields(1).size() == 1);
// System.assert(e.getDmlFields(1)[0] == 'Name');
System.assert(e.getDmlMessage(1).indexOf(
'A Installed Product with this Name already exists.') > -1);
}
}
}
System.assert(e.getDmlMessage(0).indexOf('A Installed Product with this Name already exists.') > -1);
Might be easier to read if you use the "Add a code sample" button ('<>') for showing code. Are you getting a runtime error, or compile time error?