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
Travis WrightTravis Wright 

Need help with test class

Need help with a test class for the following Class. I am new to development and need to get this class above 75%. I didn't write this class so I am kinda stuck. Any help would be great. 

Class
trigger CreateAssetonClosedWon on Opportunity (after insert, after update) {
  Map<String, AssetRollup__c> rollupMap = AssetRollup__c.getAll();
 
     for(Opportunity o: trigger.new){
      if(o.isWon == true && o.HasOpportunityLineItem == true){
         String opptyId = o.Id;
         OpportunityLineItem[] OLI = [Select UnitPrice, Quantity, PricebookEntry.Product2Id,
                                             TotalPrice,
                                             PricebookEntry.Product2.Name, Description, Converted_to_Asset__c,
                                             PricebookEntry.Product2.Create_Asset__c, PricebookEntry.Product2.Sales_Order_Group__c
                                      From OpportunityLineItem
                                      where OpportunityId = :opptyId
                                        and Converted_to_Asset__c = false
                                        and PricebookEntry.Product2.Create_Asset__c = true];
         Asset[] ast = new Asset[]{};
         Asset hotline;
         Asset a = new Asset();
         for(OpportunityLineItem ol: OLI){
            a = new Asset();
          a.AccountId = o.AccountId;
            a.Product2Id = ol.PricebookEntry.Product2Id;
            a.Quantity__c = ol.Quantity;
            a.Price =  ol.TotalPrice;
            a.PurchaseDate = o.CloseDate;
            a.Renewal_Date__c = o.CloseDate.Month() + '/' + o.CloseDate.Day();
            a.Status = 'Purchased';
            a.Description = ol.Description;
            a.Name = ol.PricebookEntry.Product2.Name;
        // TODO:extend this to support matching on sales order group from AssetRollup__c and build a list of rollups         
        if(ol.PricebookEntry.Product2.Sales_Order_Group__c=='International Hotline'){
          if(hotline == null) {
            hotline = a;
            AssetRollup__c rollup = rollupMap.get(ol.PricebookEntry.Product2.Sales_Order_Group__c);
            if (rollup != null){
              hotline.Name = rollup.Asset_Name__c;
              hotline.Product2Id = rollup.ProductId__c;
            }
          }
          else {
            hotline.Quantity__c += a.Quantity__c;
            hotline.Price += ol.TotalPrice;
          }
        }
        else{
              ast.add(a);
        }
           
            ol.Converted_to_Asset__c = true;
       }
       if(hotline != null) {
         ast.add(hotline);
       }
      update OLI;
      
      insert ast;
     }
    }
}

Test Class


@isTest
Public class testReassignCNO {
    
     static testMethod void testPardotScore() {
    
        Account a = new Account();
        a.name = 'TNW (for Competitor Profile)';
        a.OwnerId = '005A0000000gWDt';
        insert a;
       
        Contact q = new Contact();
        q.FirstName = 'Test';
        q.LastName = 'User 1';
        q.pi__score__c = 20;
        q.accountId = a.Id;
        q.OwnerId = '005A0000000gWDt';
        q.No_Longer_Employed__c = true;
        insert q;
       
         Contact c = new Contact();
        c.FirstName = 'Test2';
        c.LastName = 'User 2';
        c.pi__score__c = 20;
        c.accountId = a.Id;
        c.OwnerId = '005F0000003kNza';
        insert c;
        
        a.assign__c = True;
        update a;
       
        
       
    }
}
Best Answer chosen by Travis Wright
James LoghryJames Loghry

If you haven't already, please review the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

A
Lso, if you search the forums, there are literally hundreds of these kinds of posts which could also help.

In short, at minium you'll want to:

  1. Create an opportunity record in your unit test (this will fire your trigger and execute the code to some degree).
  2. Create any other dependent data like Opportunity Line Item records.
  3. Provide system asserts in your unit test to verify that your class is working as expected.
Good luck,

All Answers

James LoghryJames Loghry

If you haven't already, please review the following link: https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

A
Lso, if you search the forums, there are literally hundreds of these kinds of posts which could also help.

In short, at minium you'll want to:

  1. Create an opportunity record in your unit test (this will fire your trigger and execute the code to some degree).
  2. Create any other dependent data like Opportunity Line Item records.
  3. Provide system asserts in your unit test to verify that your class is working as expected.
Good luck,
This was selected as the best answer
Travis WrightTravis Wright
Noticed I gave the wrong test class. Not sure can you provide some details on number 3 cause I believe that is all I am missing. Looking at the document right now. 

@isTest

private class testCreateAssetonClosedWon {
   
    static testMethod void testCreateAssetonClosedWon(){
       
        Account a = [select Id from Account limit 1];
        PricebookEntry pbID = [select Id from PricebookEntry WHERE IsActive = true limit 1];
        Opportunity o = new Opportunity();
        OpportunityLineItem ol = new OpportunityLineItem();
       
        o.AccountId = a.Id;
        o.Name = 'test';
        o.StageName = '1. Initial Contact';
        o.Type = 'New';
        o.LeadSource = 'AM Activity';
        o.CloseDate = date.today();
        insert o;
       
        ol.OpportunityId = o.Id;
        ol.Quantity = 1;
        ol.UnitPrice = 2.00;
        ol.PricebookEntryId = pbId.Id;
       
        insert ol;
       
               
        delete o;
       
       
       
    }
   
   
}
mahesh reddy 4mahesh reddy 4
Hi, no need to cover all classes we have to cover overall code coverage of 75%..
Travis WrightTravis Wright
Current Coverage of this class is 47% and my organization requires it be above 75%. I think All I need to do is add the system asserts to check inf asset is created correctly. 
Sanjay George 1Sanjay George 1
Hi, I could see you were using Custom settings in your class.

To provide test class the best practise is to load the sample data or the custom setting as a static resource and link it to your Test Class. This will help you get the converage of those lines: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_testing_load_data.htm

List<AssetRollup__c> ls = Test.loadData(AssetRollup__c.sObjectType, '<StaticResourceName>');