You need to sign in to do that
Don't have an account?
David Durant
APEX Code Coverage Test Class Issue
I am having trouble with creating a test class for a trigger I just created. This is the first trigger that I have created and it functions perfectly however, I currently have 0% code coverage. I have been reading the force.com Developers Guide and SDFC99.com to try to obtain some knowledge on where to even start with this test class but I am not even sure what to test.
Below is the apex trigger I have created.
"Error Message System.QueryException: List has no rows for assignment to SObject
Stack Trace Class.TestQuickUpdate.insertNewRecord: line 5, column 1"
Its referencing the "Id accessory360Id" I am not sure what to do next. If I am writing the test class correctly or not.
Thank you in adavance to anybody that can steer me down the right path.
Below is the apex trigger I have created.
trigger UpdateFieldValues on Case (after insert) { Id accessory360Id = [SELECT Id FROM Accessory__c WHERE Name='197 - 360μm FiberFlex™ Fiber' Limit 1].Id; Id accessoryCleaverId = [SELECT Id FROM Accessory__c WHERE Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”' Limit 1].Id; Id accessoryStripperId = [SELECT Id FROM Accessory__c WHERE Name='134 - Fiber Stripping Tool' Limit 1].Id; List<Accessory_Item__c> Accessories = new List<Accessory_Item__c>(); for (Case c : Trigger.New) { if (c.Quick_Order__c == '360 Fiber') { Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = accessory360Id)); } Else if (c.Quick_Order__c == 'Cleaver') { Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = accessoryCleaverId)); } Else if (c.Quick_Order__c == 'Fiber Stripper'){ Accessories.add(new Accessory_Item__c( Case__c = c.Id, Item__c = accessoryStripperId)); } } insert Accessories; }Below is what I currently have for the test class however, it is still failing while atempting to test.
@isTest public class TestQuickUpdate { static testMethod void insertNewRecord() { Id accessory360Id = [new Accessory__c (Name='197 - 360μm FiberFlex™ Fiber')].Id; Id accessoryCleaverId = [new Accessory__c (Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”'].Id; //may need \\" replace " Id accessoryStripperId = [new Accessory__c (Name='134 - Fiber Stripping Tool'].Id; Case recordToCreate1 = new Case(); recordToCreate1.Origin = 'Phone'; recordToCreate1.AccountId = '001W000000I931R'; recordToCreate1.ContactId = '003W000000OAOFK'; recordToCreate1.Site__c = 'a0EW0000000fLBY'; recordToCreate1.Quick_Order__c = '360 Fiber'; recordToCreate1.ContactId = '003W000000OAOFK'; insert recordToCreate1; system.assertequals(1,[select accessory_item__c where case__c = :recordToCreate1.id,Accessory__c= :accessory360Id]); Case recordToCreate2 = new Case(); recordToCreate2.Origin = 'Phone'; recordToCreate2.AccountId = '001W000000I931R'; recordToCreate2.ContactId = '003W000000OAOFK'; recordToCreate2.Site__c = 'a0EW0000000fLBY'; recordToCreate2.Quick_Order__c = 'Cleaver'; recordToCreate2.ContactId = '003W000000OAOFK'; insert recordToCreate2; system.assertequals(1,[select accessory_item__c where case__c = :recordToCreate2.id,Accessory__c= :accessoryCleaverId]); Case recordToCreate3 = new Case(); recordToCreate3.Origin = 'Phone'; recordToCreate3.AccountId = '001W000000I931R'; recordToCreate3.ContactId = '003W000000OAOFK'; recordToCreate3.Site__c = 'a0EW0000000fLBY'; recordToCreate3.Quick_Order__c = 'Fiber Stripper'; recordToCreate3.ContactId = '003W000000OAOFK'; insert recordToCreate3; system.assertequals(1,[select accessory_item__c where case__c = :recordToCreate3.id,Accessory__c= :accessoryStripperId]); } }The error its giving me is:
"Error Message System.QueryException: List has no rows for assignment to SObject
Stack Trace Class.TestQuickUpdate.insertNewRecord: line 5, column 1"
Its referencing the "Id accessory360Id" I am not sure what to do next. If I am writing the test class correctly or not.
Thank you in adavance to anybody that can steer me down the right path.
please replace your test class with this code
@isTest
public class TestQuickUpdate {
static testMethod void insertNewRecord() {
Accessory__c objAcc1 = new Accessory__c (Name='197 - 360μm FiberFlex™ Fiber');
insert objAcc1;
Accessory__c objAcc2 = new Accessory__c (Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1');
insert objAcc2;
Accessory__c objAcc3 = new Accessory__c (Name='134 - Fiber Stripping Tool');
insert objAcc3;
Case objCase1 = new Case(Quick_Order__c = '360 Fiber');
insert objCase1;
Case objCase2 = new Case(Quick_Order__c = '360 Fiber');
insert objCase2;
Case objCase3 = new Case(Quick_Order__c = '360 Fiber');
insert objCase3;
}
}
To cover your trigger, you need simply insert 3 account in test class with same name and then you need to insert 3 cases with quickOrders...
Please check with this and let me know if this solves your issue...Also if is there any mandatory field then provide that field for account and case any dummy value..
P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
Thanks,
Sandeep
Salesforce Certified Developer
You are accessing the Accessory__c on line 5 and 6.
You actually need to create Accessory__c records and then insert them above line five
Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
I finally have been able to improve my code coverage from 0% to 16%! At least its something!
This is how far I got with the test class code:
Below the code I will put the error message that I am encountering.