+ Start a Discussion
David DurantDavid 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.
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.


 
sandeep sankhlasandeep sankhla
Hi David,

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 
Mudasir WaniMudasir Wani
Hi David,

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 
Accessory__c  testOne= new Accessory__c (Name='197 - 360μm FiberFlex™ Fiber');
Insert testOne;
Accessory__c  TestTwo= new Accessory__c (Name='241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”');
Insert TestTwo;
Accessory__c  TestThree = new Accessory__c (Name='134 - Fiber Stripping Tool';
insert TestThree;
Then modify the code on line 5 and 6 and  7 as

 Id accessory360Id = testOne.Id;
 Id accessoryCleaverId = TestTwo.Id;
 Id accessoryStripperId = TestThree.Id;


Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
David DurantDavid Durant
Thank you guys both for your imput.

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.
@isTest 
public class TestQuickUpdate2 {
  static testMethod void insertNewRecord() {
       
       Accessory__c A1 = new Accessory__c();
       a1.Name           = '197 - 360μm FiberFlex™ Fiber';
       a1.Description__c = '360μm FiberFlex™ Fiber';
       a1.Item_Num__c    = '197';
       a1.Unit_Price__c  = 295.00;
        
        insert A1;

       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;

       List<Case> Fiber = [SELECT Id FROM Case WHERE Quick_Order__c = '360 Fiber'];
       system.assertequals(1, Fiber.size());

       Accessory__c A2 = new Accessory__c();
       a2.Name           = '241 - Ceramic Fiber Cleavers (set of 3) 1” x 1”';
       a2.Description__c = 'Ceramic Fiber Cleavers (set of 3) 1” x 1”';
       a2.Item_Num__c    = '241';
       a2.Unit_Price__c  = 25.00;
        
        insert A2;

       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;

       List<Case> Cleaver = [SELECT Id FROM Case WHERE Quick_Order__c = 'Cleaver'];
       system.assertequals(1, Cleaver.size());

       Accessory__c A3 = new Accessory__c();
       a3.Name           = '134 - Fiber Stripping Tool';
       a3.Description__c = 'Fiber Stripping Tool';
       a3.Item_Num__c    = '134';
       a3.Unit_Price__c  = 110.00;
        
        insert A3;

       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;

       List<Case> Stripper = [SELECT Id FROM Case WHERE Quick_Order__c = 'Stripper'];
       system.assertequals(1, Stripper.size());

    }
}


User-added image