+ Start a Discussion
SAHG-SFDCSAHG-SFDC 

Test Class Assistance For Coverage

public with sharing class OptyProduct {
Public List<OpportunityLineItem> ProdList{get;set;}
    public OptyProduct(ApexPages.StandardController controller) {
        ProdList = new List<OpportunityLineItem>();
        ProdList  =[SELECT Quantity,UnitPrice,TotalPrice,Sales_Price__c,Price_Type__c, Invoice_Frequency__c, Monthly_Minimum__c,product2.Name,
        ServiceDate FROM OpportunityLineItem where Opportunity.Id=:ApexPages.currentPage().getParameters().get('id')];
    }
}
HI,

I have this class for which I need test class , what are the steps I should follow to get good coverage

Advance thanks
 
Best Answer chosen by SAHG-SFDC
Lalit Mistry 21Lalit Mistry 21
The below test class will give you 100% coverage without testing for functionality.
@IsTest
private with sharing class OptyProductTest {
	public static testMethod void testForCoverage(){
		PageReference pgRef = new PageReference('/006');
		pgRef.getParameters().put('id','');
		Test.setCurrentPage(pgRef);
		System.Test.startTest();
		OptyProduct opptyProductController = new OptyProduct(null);
		System.assert(opptyProductController.ProdList.isEmpty());
		System.Test.stopTest();
	}
}

 

All Answers

Lalit Mistry 21Lalit Mistry 21
The below test class will give you 100% coverage without testing for functionality.
@IsTest
private with sharing class OptyProductTest {
	public static testMethod void testForCoverage(){
		PageReference pgRef = new PageReference('/006');
		pgRef.getParameters().put('id','');
		Test.setCurrentPage(pgRef);
		System.Test.startTest();
		OptyProduct opptyProductController = new OptyProduct(null);
		System.assert(opptyProductController.ProdList.isEmpty());
		System.Test.stopTest();
	}
}

 
This was selected as the best answer
SAHG-SFDCSAHG-SFDC
HI Lalit,

I  cannot be more thankful , Thank you so much! I got 100% coverage

How does this work though?
SAHG-SFDCSAHG-SFDC
If you can tell me what is line 5 and 9 doing?
Lalit Mistry 21Lalit Mistry 21
Here are the few points due to which it works.
1. Class constructor parameter (controller instance) is not used anywhere in the class. So passing a null value do not have any negative impact. Ideally, if parameter is not used, then you should get rid of that unwanted parameter.
2. List of OpportunityLineItem is not used in the class (I suspect its used on vf page). As a result, if SOQL query returns 0 rows, still your class would not have failed because of the use of list variable (ProdList). Hence, all you had to do is  to ensure current page has id parameter (value does not matter here as we are just testing for coverage. If you want your SOQL to return the list of line items then you will need to pass the valid opportunity value).
 
Lalit Mistry 21Lalit Mistry 21
Line 5 simply adds url parameter to the page, in your case its 'id'.
Line 9 is a simple assert to validate the size of list after initialization of controller. since we did not pass valid opportunity id with line items, product list size should be 0. Its as good as if(ProdList.size() > 0) then throw exception.
SAHG-SFDCSAHG-SFDC
Wow! Thanks again this has helped me so much ! :)