You need to sign in to do that
Don't have an account?
karol.freeberg
Code coverage help please.
I am really new at apex code and worse at trying to do the test coverage. My code is only at 65% and I'm at a loss at what else do to. Can anyone help please.
Here is my current test code and my trigger is below that.
The Test Code: Coverage only at 65%
@IsTest Private class TestSRProducts {
testMethod private static void getthedata() {
// Create the user
// User testUser = NewObjFactory.buildTestUser(0, 'Pipo', 'Tanya');
// insert testuser;
String PM7 = 'Tanya Pipo';
User testuser = [Select ID, Name
From User
Where Name = :PM7];
system.debug('User ' +testuser.id);
// Create the Account
Account testacct = NewObjFactory.buildTestAcct(0, 'User - IT', testuser.id);
insert testacct;
// Create the Contact
Contact testcontact = NewObjFactory.buildTestContact(0, testacct.id, testuser.id);
insert testcontact;
// Create a product
Product2 testprod = NewObjFactory.buildTestProducts(0);
insert testprod;
Product2 testprod2 = NewObjFactory.buildTestProd2(0);
insert testprod2;
// Create a Stocking Request
Stocking_Request__c testSR = NewObjFactory.buildTestStockingReq(0, testacct.id, testuser.id, testcontact.id);
insert testSr;
system.debug('SR ' +testsr.id);
// Create a SR Product and envokes the trigger
SR_Products__C testsrprod = NewObjFactory.buildtestSRProducts(0, testSR.id);
SR_Products__C testsrprod2 = NewObjFactory.buildtestSRProducts(0, testSR.id);
testsrprod2.Product__c = [Select ID from Product2 where name = :'CLS2PAP SHEET SECONDS GLOSS'].id;
Test.startTest();
insert testsrprod;
insert testsrprod2;
Test.stopTest();
Stocking_Request__c SR1 = [Select ID, Name, product_manager_1__c,
product_manager_2__c, product_manager_3__c, product_manager_4__c,
product_manager_5__c, product_manager_6__c, product_manager_7__c, product_manager_8__c
From Stocking_Request__c
Where ID = :testsr.id];
If (SR1 != null){
system.debug('product manager is '+SR1.name);
system.debug('product manager7 is '+ SR1.product_manager_7__c);
}
System.assertEquals (SR1.product_manager_7__c, testuser.id);
System.assertNotEquals (SR1.product_manager_6__c, testuser.id);
System.assertNotEquals (SR1.product_manager_5__c, testuser.id);
System.assertNotEquals (SR1.product_manager_4__c, testuser.id);
System.assertNOtEquals (SR1.product_manager_3__c, testuser.id);
System.assertNotEquals (SR1.product_manager_2__c, testuser.id);
System.assertNotEquals (SR1.product_manager_1__c, testuser.id);
} //End getthedata
} //End Class TestSRProducts
The trigger:
trigger SR_Set_Approvers on SR_Products__c (After insert, After Update) {
// This trigger sets all the product managers from the Stocking Request records
// With the data from the SR - Product Details records
// Loop all product records that have changed
for (SR_Products__c prod : Trigger.new) {
// system.debug('product name1 = ' + prod.Stocking_Request__c);
// Get all the product records related to the SR
SR_Products__c[] allproducts = [SELECT product_manager__c, PM_Type__C,
stocking_request__c
FROM SR_Products__c
WHERE stocking_request__c = :Prod.stocking_request__c
];
String Pm1 = '';
String Pm2 = '';
String Pm3 = '';
String Pm4 = '';
String Pm5 = '';
String Pm6 = '';
String Pm7 = '';
String Pm8 = '';
//Set the PM variables with the PM from the SR-Products records
for(SR_Products__c p : allproducts) {
If (p.PM_Type__c == 'PM1')
pm1 = p.product_manager__c;
If (p.PM_Type__c == 'PM2')
pm2 = p.product_manager__c;
If (p.PM_Type__c == 'PM3')
pm3 = p.product_manager__c;
If (p.PM_Type__c == 'PM4')
pm4 = p.product_manager__c;
If (p.PM_Type__c == 'PM5')
pm5 = p.product_manager__c;
If (p.PM_Type__c == 'PM6')
pm6 = p.product_manager__c;
If (p.PM_Type__c == 'PM7')
pm7 = p.product_manager__c;
If (p.PM_Type__c == 'PM8')
pm8 = p.product_manager__c;
} //end loop products
// Get the Stocking Request record
//system.debug('product name = ' + prod.Stocking_Request__c);
Stocking_Request__c sr = [Select ID, Name,
Product_Manager_1__c, Product_Manager_2__c,
Product_Manager_3__c, Product_Manager_4__c,
Product_Manager_5__c, Product_Manager_6__c,
Product_Manager_7__c, Product_Manager_8__c
From Stocking_Request__c
Where ID = :prod.Stocking_Request__c];
// Reset all PM fields to blank to start out with
sr.Product_Manager_1__c = null;
sr.Product_Manager_2__c = null;
sr.Product_Manager_3__c = null;
sr.Product_Manager_4__c = null;
sr.Product_Manager_5__c = null;
sr.Product_Manager_6__c = null;
sr.Product_Manager_7__c = null;
sr.Product_Manager_8__c = null;
// Get the user record IDs for the PM variable fields
// And set the PM fields in the stocking request record
If (PM1 != '' ){
User u1 = [Select ID, Name
From User
Where Name = :PM1];
If (u1 != null)sr.Product_Manager_1__c = u1.ID;
} //End PM1 if
If (PM2 != '' ){
User u2 = [Select ID, Name
From User
Where Name = :PM2];
If (u2 != null)sr.Product_Manager_2__c = u2.id;
} //End PM2 if
If (PM3 != '' ){
User u3 = [Select ID, Name
From User
Where Name = :PM3];
If (u3 != null)sr.Product_Manager_3__c = u3.id;
} //End PM3 if
If (PM4 != ''){
User u4 = [Select ID, Name
From User
Where Name = :PM4];
If (u4 != null)sr.Product_Manager_4__c = u4.id;
} //End PM4 if
If (PM5 != '' ){
User u5 = [Select ID, Name
From User
Where Name = :PM5];
If (u5 != null)sr.Product_Manager_5__c = u5.id;
} //End PM5 if
If (PM6 != ''){
User u6 = [Select ID, Name
From User
Where Name = :PM6];
If (u6 != null)sr.Product_Manager_6__c = u6.id;
} //End PM6 if
If (PM7 != '' ){
User u7 = [Select ID, Name
From User
Where Name = :PM7];
If (u7 != null)sr.Product_Manager_7__c = u7.id;
} //End PM7 if
If (PM8 != '' ){
User u8 = [Select ID, Name
From User
Where Name = :PM8];
If (u8 != null)sr.Product_Manager_8__c = u8.id;
} //End PM8 if
Update sr;
} // End for looping of changed product records
}
Here is my current test code and my trigger is below that.
The Test Code: Coverage only at 65%
@IsTest Private class TestSRProducts {
testMethod private static void getthedata() {
// Create the user
// User testUser = NewObjFactory.buildTestUser(0, 'Pipo', 'Tanya');
// insert testuser;
String PM7 = 'Tanya Pipo';
User testuser = [Select ID, Name
From User
Where Name = :PM7];
system.debug('User ' +testuser.id);
// Create the Account
Account testacct = NewObjFactory.buildTestAcct(0, 'User - IT', testuser.id);
insert testacct;
// Create the Contact
Contact testcontact = NewObjFactory.buildTestContact(0, testacct.id, testuser.id);
insert testcontact;
// Create a product
Product2 testprod = NewObjFactory.buildTestProducts(0);
insert testprod;
Product2 testprod2 = NewObjFactory.buildTestProd2(0);
insert testprod2;
// Create a Stocking Request
Stocking_Request__c testSR = NewObjFactory.buildTestStockingReq(0, testacct.id, testuser.id, testcontact.id);
insert testSr;
system.debug('SR ' +testsr.id);
// Create a SR Product and envokes the trigger
SR_Products__C testsrprod = NewObjFactory.buildtestSRProducts(0, testSR.id);
SR_Products__C testsrprod2 = NewObjFactory.buildtestSRProducts(0, testSR.id);
testsrprod2.Product__c = [Select ID from Product2 where name = :'CLS2PAP SHEET SECONDS GLOSS'].id;
Test.startTest();
insert testsrprod;
insert testsrprod2;
Test.stopTest();
Stocking_Request__c SR1 = [Select ID, Name, product_manager_1__c,
product_manager_2__c, product_manager_3__c, product_manager_4__c,
product_manager_5__c, product_manager_6__c, product_manager_7__c, product_manager_8__c
From Stocking_Request__c
Where ID = :testsr.id];
If (SR1 != null){
system.debug('product manager is '+SR1.name);
system.debug('product manager7 is '+ SR1.product_manager_7__c);
}
System.assertEquals (SR1.product_manager_7__c, testuser.id);
System.assertNotEquals (SR1.product_manager_6__c, testuser.id);
System.assertNotEquals (SR1.product_manager_5__c, testuser.id);
System.assertNotEquals (SR1.product_manager_4__c, testuser.id);
System.assertNOtEquals (SR1.product_manager_3__c, testuser.id);
System.assertNotEquals (SR1.product_manager_2__c, testuser.id);
System.assertNotEquals (SR1.product_manager_1__c, testuser.id);
} //End getthedata
} //End Class TestSRProducts
The trigger:
trigger SR_Set_Approvers on SR_Products__c (After insert, After Update) {
// This trigger sets all the product managers from the Stocking Request records
// With the data from the SR - Product Details records
// Loop all product records that have changed
for (SR_Products__c prod : Trigger.new) {
// system.debug('product name1 = ' + prod.Stocking_Request__c);
// Get all the product records related to the SR
SR_Products__c[] allproducts = [SELECT product_manager__c, PM_Type__C,
stocking_request__c
FROM SR_Products__c
WHERE stocking_request__c = :Prod.stocking_request__c
];
String Pm1 = '';
String Pm2 = '';
String Pm3 = '';
String Pm4 = '';
String Pm5 = '';
String Pm6 = '';
String Pm7 = '';
String Pm8 = '';
//Set the PM variables with the PM from the SR-Products records
for(SR_Products__c p : allproducts) {
If (p.PM_Type__c == 'PM1')
pm1 = p.product_manager__c;
If (p.PM_Type__c == 'PM2')
pm2 = p.product_manager__c;
If (p.PM_Type__c == 'PM3')
pm3 = p.product_manager__c;
If (p.PM_Type__c == 'PM4')
pm4 = p.product_manager__c;
If (p.PM_Type__c == 'PM5')
pm5 = p.product_manager__c;
If (p.PM_Type__c == 'PM6')
pm6 = p.product_manager__c;
If (p.PM_Type__c == 'PM7')
pm7 = p.product_manager__c;
If (p.PM_Type__c == 'PM8')
pm8 = p.product_manager__c;
} //end loop products
// Get the Stocking Request record
//system.debug('product name = ' + prod.Stocking_Request__c);
Stocking_Request__c sr = [Select ID, Name,
Product_Manager_1__c, Product_Manager_2__c,
Product_Manager_3__c, Product_Manager_4__c,
Product_Manager_5__c, Product_Manager_6__c,
Product_Manager_7__c, Product_Manager_8__c
From Stocking_Request__c
Where ID = :prod.Stocking_Request__c];
// Reset all PM fields to blank to start out with
sr.Product_Manager_1__c = null;
sr.Product_Manager_2__c = null;
sr.Product_Manager_3__c = null;
sr.Product_Manager_4__c = null;
sr.Product_Manager_5__c = null;
sr.Product_Manager_6__c = null;
sr.Product_Manager_7__c = null;
sr.Product_Manager_8__c = null;
// Get the user record IDs for the PM variable fields
// And set the PM fields in the stocking request record
If (PM1 != '' ){
User u1 = [Select ID, Name
From User
Where Name = :PM1];
If (u1 != null)sr.Product_Manager_1__c = u1.ID;
} //End PM1 if
If (PM2 != '' ){
User u2 = [Select ID, Name
From User
Where Name = :PM2];
If (u2 != null)sr.Product_Manager_2__c = u2.id;
} //End PM2 if
If (PM3 != '' ){
User u3 = [Select ID, Name
From User
Where Name = :PM3];
If (u3 != null)sr.Product_Manager_3__c = u3.id;
} //End PM3 if
If (PM4 != ''){
User u4 = [Select ID, Name
From User
Where Name = :PM4];
If (u4 != null)sr.Product_Manager_4__c = u4.id;
} //End PM4 if
If (PM5 != '' ){
User u5 = [Select ID, Name
From User
Where Name = :PM5];
If (u5 != null)sr.Product_Manager_5__c = u5.id;
} //End PM5 if
If (PM6 != ''){
User u6 = [Select ID, Name
From User
Where Name = :PM6];
If (u6 != null)sr.Product_Manager_6__c = u6.id;
} //End PM6 if
If (PM7 != '' ){
User u7 = [Select ID, Name
From User
Where Name = :PM7];
If (u7 != null)sr.Product_Manager_7__c = u7.id;
} //End PM7 if
If (PM8 != '' ){
User u8 = [Select ID, Name
From User
Where Name = :PM8];
If (u8 != null)sr.Product_Manager_8__c = u8.id;
} //End PM8 if
Update sr;
} // End for looping of changed product records
}
All Answers
For example
SR_Products__c sr1 = new SR_Products__c(Name='test sr1', PM_Type__c='PM1');
SR_Products__c sr2 = new SR_Products__c(Name='test sr2', PM_Type__c='PM2);
.
.
.
(do this until PM8);
insert new List<SR_Products__c>{sr1, sr2, ...., sr8};