function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bouscalbouscal 

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);
            }
  }
}

 

Best Answer chosen by Admin (Salesforce Developers) 
MJ Kahn / OpFocusMJ Kahn / OpFocus

A few things:

 

  • In your test method, I don't see where you actually insert the cases. You do an update, but I don't see an insert statement, so I'm surprised that the test is passing. (Maybe the insert statement got lost when you stripped out company-identifiable information, but if so, it's hard to know what else might have been stripped out that's contributing to your problem.)
  • Your class doesn't appear to be bulkified. If it's called with 8 Cases, it'll do 8 separate inserts and 8 separate updates. That's not very efficient, and if your class is called with enough Cases, you'll hit governor limits. You should bulkify the code, so you wind up doing only 1 insert and 1 update.
  • Your test code doesn't actually verify anything. If your newAsset() class returned a list of the Assets that it created, that might make it easier for you to verify that it's creating the Assets correctly.
  • As a best practice, you should avoid using a hard-coded Group Id whenever possible. Instead, query the Group object to find the Id that matches a specific Group Name. This makes the code much easier to read, and avoids any issues if you add a new Group in the sandbox, where it has a different Id than the same Group Name in Production.


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:

 

String strDesc = '';
if (mapDescs.containsKey(c.Reason_Detail__c)) strDesc = mapDescs.(c.Reason_Detail__c);

 

You could do the same thing for the Type. Simplifying the code might help you figure out where it's going wrong.

 

 

 

All Answers

MJ Kahn / OpFocusMJ Kahn / OpFocus

A few things:

 

  • In your test method, I don't see where you actually insert the cases. You do an update, but I don't see an insert statement, so I'm surprised that the test is passing. (Maybe the insert statement got lost when you stripped out company-identifiable information, but if so, it's hard to know what else might have been stripped out that's contributing to your problem.)
  • Your class doesn't appear to be bulkified. If it's called with 8 Cases, it'll do 8 separate inserts and 8 separate updates. That's not very efficient, and if your class is called with enough Cases, you'll hit governor limits. You should bulkify the code, so you wind up doing only 1 insert and 1 update.
  • Your test code doesn't actually verify anything. If your newAsset() class returned a list of the Assets that it created, that might make it easier for you to verify that it's creating the Assets correctly.
  • As a best practice, you should avoid using a hard-coded Group Id whenever possible. Instead, query the Group object to find the Id that matches a specific Group Name. This makes the code much easier to read, and avoids any issues if you add a new Group in the sandbox, where it has a different Id than the same Group Name in Production.


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:

 

String strDesc = '';
if (mapDescs.containsKey(c.Reason_Detail__c)) strDesc = mapDescs.(c.Reason_Detail__c);

 

You could do the same thing for the Type. Simplifying the code might help you figure out where it's going wrong.

 

 

 

This was selected as the best answer
bouscalbouscal

Thanks for the tips MJ, still learning this stuff so off to the sandbox I go.  :-)

Abhi_TripathiAbhi_Tripathi

Hey 

bouscalbouscal

Although my issue is yet unresolved this did point out several problems with my code thanks!