You need to sign in to do that
Don't have an account?
bouscal
Need Help with Apex Test Class
I have a class I wrote to automatically create Assets when a Case is closed based on values within the Case.
I also have a test class that I thought would test each instance but for some reason, though the test passes, though the code functions as desired, the testing states 0/0 test methods passed and 0% code coverage. What have I overlooked?
Note, string values listed may not match as I've tried to strip company specific information from the code.
public class CreateAsset { public static void newAsset(Case[] cs) { // declare variables string dtmToday = system.today().format(); string strDesc = '' ; /* see other workbook */ string strType = ''; id strOwnr = null; /* depends on product */ for (Case c:cs) { if (c.isclosed == true){ if (c.Asset_Created__c != true) { id strAcct=null; string strName = c.asset_to_install__c + ' - ' + c.client_id__c + ' - ' + dtmToday; strAcct = c.accountid; id strCont = c.contactid; id strCase = c.id; id strOpp = c.opportunity__c; /* Check Reason Detail for Description and Type value */ strType = 'New'; if (c.Reason_Detail__c == 'New - Prod 1') { strDesc = 'New sale as a consolidator'; } if (c.Reason_Detail__c== 'New - Prod 2') { strDesc = 'New sale'; } if (c.Reason_Detail__c== 'New - Prod 3') { strDesc = 'New sale'; } if (c.Reason_Detail__c== 'New - Prod 4' || c.Reason_Detail__c == 'New - Prod 5') { strDesc = 'New sale'; } if (c.Reason_Detail__c== 'a reason detail') { strDesc = 'a description'; strType = 'Migration'; } if (c.Reason_Detail__c== 'Because') { strDesc = 'a description'; strType = 'Migration'; } if (c.Reason_Detail__c== 'New - Prod 6') { strDesc = 'New sale'; } if (c.Reason_Detail__c== 'New - Prod 7') { strDesc = 'something'; strType = 'New'; } /* Check Product Group for assignment */ /* Owned by JDoe unless one of the following is true */ /* JDoe= 00570000001fNiX */ /* CDoe= 00570000001hbu7 */ /* FDoe= 00570000001aSmE */ strOwnr = '00570000001fNiX'; If (c.Product_Group__c == 'Prod' || c.Product_Group__c == 'Prod') { strOwnr = '00570000001hbu7'; } If (c.Product_Group__c == 'Prod') { strOwnr = '00570000001aSmE'; } if (strDesc != '') { Asset[] newAsset = new Asset[0]; newAsset.add(new Asset( Name = strName, AccountId = strAcct, ContactId = strCont, Owner__c = strOwnr, Case_Number__c = strCase, Opportunity__c = strOpp, Description = strDesc, Requestor__c = c.Requestor__c , Type__c = strType, Status = 'Purchased', Quantity = 1, PurchaseDate = c.Contract_Signature_Date__c, Medium__c = c.Media__c, Number_of_Users__c = c.No_of_Users__c, Product_Group__c = c.Product_Group__c)); if (c.Asset_Created__c != true){ if (c.isclosed == true) { insert newAsset; } } // update the existing case c.AssetId=newAsset[0].id; c.Asset_Created__c=true; c.Asset_Created_dt__c=system.today(); // update c; } } } } } }
And here's the test
@isTest(SeeAllData=true) private class CreateAssetTest{ static void testAll(){ TestObjects to = new TestObjects(); Account acct = to.getAccount(true); Contact contact = to.getContact(acct.Id, false); Date theDate=System.today(); // Create test cases List<Case> cases = new List<Case>{}; for(Integer i = 0; i < 8; i++){ Case c = new Case(Subject='Test Case ' + i, AccountId=acct.Id, ContactId=contact.Id, Requestor__c=contact.Id, Origin='Self-initiated', Description='Test Case ' + i, Contract_Signature_Date__c=theDate, Media__c='DVD', No_of_Users__c=2, Product_Group__c='', Reason__c='', Reason_Detail__c='', Status='Open'); cases.add(c); for(i = 0; i < 8; i++){ cases[i].status='Closed'; if(i==0){ cases[i].Reason__c='New Account Setup'; cases[i].Reason_Detail__c='New - Prod 1'; cases[i].Product_Group__c='Group'; } if(i==1){ cases[i].Reason__c='New Account Setup'; cases[i].Reason_Detail__c='New - Prod 2'; cases[i].Product_Group__c='Group'; } if(i==2){ cases[i].Reason__c='New Account Setup'; cases[i].Reason_Detail__c='New - Prod 3'; cases[i].Product_Group__c='Group'; } if(i==3){ cases[i].Reason__c='New Account Setup'; cases[i].Reason_Detail__c='New - Prod 4'; cases[i].Product_Group__c='Group'; } if(i==4){ cases[i].Reason__c='Acct Maintenance'; cases[i].Reason_Detail__c='Maintenance - Media Change'; cases[i].Product_Group__c='Group'; } if(i==5){ cases[i].Reason__c='ABS/ABL'; cases[i].Reason_Detail__c='ABS/ABL - Migration'; cases[i].Product_Group__c='Group'; } if(i==6){ cases[i].Reason__c='New Account Setup'; cases[i].Reason_Detail__c='New - Prod 5'; cases[i].Product_Group__c = 'Group'; } if(i==7){ cases[i].Reason__c='New Account Setup'; cases[i].Reason_Detail__c='New - Prod 6'; cases[i].Product_Group__c = 'Group'; } } update cases; CreateAsset.newAsset(cases); } } }
A few things:
In addition, I can see some ways in which you could simplify your code. For example, instead of having newAsset() have a series of IF statements that assign a Desc based on the Reason_Detail__c, how about creating a map of Strings (Desc values), indexed by a String (Reason_Detail__c). You could initialize the map in 1 line of code, then replace all of those IF statements with these 2 lines:
You could do the same thing for the Type. Simplifying the code might help you figure out where it's going wrong.
All Answers
A few things:
In addition, I can see some ways in which you could simplify your code. For example, instead of having newAsset() have a series of IF statements that assign a Desc based on the Reason_Detail__c, how about creating a map of Strings (Desc values), indexed by a String (Reason_Detail__c). You could initialize the map in 1 line of code, then replace all of those IF statements with these 2 lines:
You could do the same thing for the Type. Simplifying the code might help you figure out where it's going wrong.
Thanks for the tips MJ, still learning this stuff so off to the sandbox I go. :-)
Hey bouscal.
You can check out this link to get something about test classes
http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html
Although my issue is yet unresolved this did point out several problems with my code thanks!