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

Code coverage error 71% need 75%
I've been trying to add more criteria to pass the code coverage to move a trigger to production but my test class doesn't pass the the coverage test. I don't know how much more criteria I can add. Any help with this would be greatly appreciated.
@IsTest public class TestAfterPOLogUpdateTest { static testMethod void myUnitTest() { // TO DO: implement unit test test.startTest(); Map<String, Schema.RecordTypeInfo> recordTypes = Schema.SObjectType.Yushin_Product__c.getRecordTypeInfosByName(); List<String> recordTypeNames = new List<String>(recordTypes.keyset()); PageReference pageRef = Page.OppTabDetailinsidesales2; Contact c= new Contact(); c.FirstName ='Bob'; c.LastName = 'Poliquin'; c.Email = 'Acme@aol.com'; c.Phone = '5088084748'; insert c; Account a = new Account(); a.Name = 'Test'; a.Industry = 'Retail'; a.Territory_Code__c='KY'; a.Sales_Agency__c='Five Nines - Plastics Division'; insert a; Opportunity o = new Opportunity(); o.name = 'Test'; o.AccountId = a.Id; o.StageName = 'Closed Won'; o.CloseDate = date.today(); o.Type = 'New Customers'; o.Product_Type__c = 'Robot'; insert o; Product2 prod = new Product2(Name = 'HOP Five 900X', Robot_Category__c = 'Hardware',Standard_Price__c=12345); insert prod; Id pricebookId = Test.getStandardPricebookId(); PricebookEntry standardPrice = new PricebookEntry( Pricebook2Id = pricebookId, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true); insert standardPrice; Request_for_Special_Payment__c rsp = new Request_for_Special_Payment__c (); rsp.Opportunity_Name__c = o.Id; rsp.Account__c = a.Id; rsp.Requestor_Submit_for_Approval__c = true; rsp.Request_Submitted_By__c = 'Bob Poliquin'; rsp.Requestor_Date_Submitted__c=date.today(); insert rsp; rsp.Opportunity_Name__c = o.Id; rsp.Supervisor_Approved__c = true; rsp.Rejected_By__c='Chris Parillo'; rsp.Supervisor_Approval_Date__c=date.today(); update rsp; PO_Log__c p = new PO_Log__c(); p.Customer__c=a.Id; p.Opportunity__c=o.Id; p.Order_Type__c='EOAT'; insert p; p.Opportunity__c= o.Id; p.PO__c = '1223'; p.Sales_Order_Number__c = '12453'; p.Order_Type__c = 'Robot'; p.Notes__c= 'text'; p.Order_Configuration__c = 'ATC'; p.Sales_Order_Number__c = '189345'; p.AddPLogOpps__c = true; update p; Yushin_Product__c ypd = new Yushin_Product__c(Discharge_Direction__c='Clamp Traverse'); ypd.Opportunity__c=o.id; ypd.Account__c =a.Id; ypd.Discharge_Direction__c='Clamp Traverse'; ypd.IMM_Mfr__c='Arburg'; ypd.IMM_Model__c='NT450'; ypd.Plant_Voltage__c='110'; ypd.Interface_Voltage__c='110 VDC'; ypd.Equipment_Type__c = 'Robot'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); insert ypd; ypd.Account__c =a.Id ; ypd.Discharge_Direction__c='Clamp Traverse'; ypd.IMM_Mfr__c='Engel'; ypd.IMM_Model__c='110t'; ypd.Plant_Voltage__c='110'; ypd.Interface_Voltage__c='110 VDC'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Equipment_Type__c = 'Other'; ypd.Product_Detail_Report__c = 'Misc. Item'; ypd.SPI_Reported__c=false; ypd.Date_Booked__c = Date.newInstance(2016, 12, 9); ypd.Date_Entered__c = Date.newInstance(2016, 12, 8); ypd.Sales_Order_Number__c = '1910678'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Equipment_Type__c = 'Installation'; ypd.Product_Detail_Report__c = 'Installation'; ypd.SPI_Reported__c=false; ypd.Date_Booked__c = Date.newInstance(2016, 12, 9); ypd.Date_Entered__c = Date.newInstance(2016, 12, 8); ypd.Sales_Order_Number__c = '1910678'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Equipment_Type__c = 'EOAT'; ypd.Price__c = 499; ypd.Model__c='HOP 750'; ypd.Status__c ='Quoted'; ypd.Date_Booked__c = Date.newInstance(2016, 12, 9); ypd.Date_Entered__c = Date.newInstance(2016, 12, 8); update ypd; ypd.Equipment_Type__c = 'Safety Guarding'; ypd.Product_Detail_Report__c = 'AUTO-SM'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Equipment_Type__c = 'Conveyor'; ypd.Price__c =45080; ypd.Status__c ='Sold'; ypd.Date_Booked__c = Date.newInstance(2016, 12, 9); ypd.Date_Entered__c = Date.newInstance(2016, 12, 8); ypd.Quantity__c= 1; ypd.Product_Detail_Report__c = 'AUTO-SM'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Price__c =45080; ypd.Equipment_Type__c = 'Servo Trav over 600'; ypd.Model__c='FRA-1530S-22-11-C1 '; ypd.Status__c ='Sold'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Price__c =45080; ypd.Equipment_Type__c = 'Adapter Plate'; ypd.Product_Detail_Report__c='Misc. Item'; ypd.Status__c ='Sold'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); update ypd; ypd.Price__c =45080; ypd.Equipment_Type__c = 'A-Axis'; ypd.Product_Detail_Report__c='EOAT-SM'; ypd.RecordTypeId = recordTypes.get(recordTypeNames[0]).getRecordTypeId(); ypd.Status__c ='Sold'; update ypd; ypd.Price__c =45080; ypd.Equipment_Type__c = 'B-Axis'; ypd.Status__c ='Sold'; update ypd; ypd.Price__c =45080; ypd.Equipment_Type__c = 'Freight'; ypd.Product_Detail_Report__c='Freight'; update ypd; ypd.Price__c =45080; ypd.Equipment_Type__c = 'Degating Station'; ypd.Product_Detail_Report__c='AUTO-SM'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Installation'; ypd.Product_Detail_Report__c='Installation'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Hybrid'; ypd.Product_Detail_Report__c='SM-Hybrid'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Software'; ypd.Product_Detail_Report__c='Software'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Servo Traverse Super Large 1300 and greater'; ypd.Model__c='RAII-a-3000SL'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Spacer'; ypd.Product_Detail_Report__c='Misc. Item'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Stanchion'; ypd.Status__c ='Quoted'; update ypd; ypd.Equipment_Type__c = 'Servo Trav over 600'; ypd.Model__c='YCII-800D-30-18'; ypd.Product_Detail_Report__c='LG-YC'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'System - SMALL'; ypd.Product_Detail_Report__c='AUTO-SM'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Collaborative robot'; ypd.Model__c='OB7'; ypd.Product_Detail_Report__c='SPECIAL'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Side-entry (horz)'; ypd.Model__c='SVR B50'; ypd.Product_Detail_Report__c='SM-SE'; update ypd; ypd.Equipment_Type__c = 'Side-entry (Vert)'; ypd.Product_Detail_Report__c='SM-SE'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Parts'; ypd.Product_Detail_Report__c='Misc. Item'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'Safety Guarding'; ypd.Product_Detail_Report__c='AUTO-SM'; ypd.Status__c ='Sold'; update ypd; ypd.Equipment_Type__c = 'System - MEDIUM'; ypd.Product_Detail_Report__c='AUTO-SM'; ypd.Status__c ='Quoted'; ypd.Order_Config__c = 'Systems'; ypd.Order_Type__c = 'Systems'; update ypd; test.stopTest(); } }
All Answers
How are you running your tests? If you use the Developer Console, you can run your tests and discover which lines of code are and aren't being tested.
Code coverage is simply a count that shows how much of your code is actually being run in a test. If you're getting 71%, that means 29% of your code isn't being touched by the tests you've written so far.
By running these tests in the console and seeing which lines aren't being run, you can determine what scenarios you need to devise in order to cross that 75% threshold.
Hope this helps!
When a test is run, it actually creates entirely new test data. RecordTypes included. So the Ids you're looking for do not exist.
Instead, you'll have to update your apex to go grab the RecordTypeId, like so:
...replacing "ContentVersion" with your ObjectType (such as Account, Contact, Product, whatever) and 'Record Type Name Goes Here' with your actual Record Type name.
Id rtId = Schema.SObjectType.ContentVersion.getRecordTypeInfosByName().get('Record Type Name Goes Here').getRecordTypeId();
I updated my test class and trigger which improved the coverage but not by much. I dont understand why certain parts of the trigger is covered and other parts are not. See my screen shot below.
My Updated Trigger: Should I add test for deleted records?
Updated Test Class:
As best as I can tell, your trigger is looking for the Product Type on the Opportunity itself. Although you insert and update many products, you appear to keep using the same opportunity. That opportunity has a Product Type of "Robot", so the "Robot" part of your code is run. You never change the opportunity itself, so the Product Type on it never changes.
I would recomment creating a new opportunity for every product you're testing. I believe that would fix the issue.
I will try that solution.
Thanks again everyone!