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
Gaurav AgnihotriGaurav Agnihotri 

unable to create a test class

Gurus, 
I am new to salesforce.com. I need help with creating test class. 
Class:
public class ProductSearchController
{
	public ProductSearchDataModel productSearchData { get; set; }

	public ProductSearchController(ApexPages.StandardController controller)
	{
		this.productSearchData = new ProductSearchDataModel();

		Id quoteId = ApexPages.currentpage().getparameters().get('Id');
        
        ProductSearch_GetQuoteLineItems getQuoteLineItems = new ProductSearch_GetQuoteLineItems();

        this.productSearchData.productsOnQuote = getQuoteLineItems.Execute(quoteId);
	}

	public PageReference runSearch()
	{
        productSearchData.hasRecords = false;
        
        String productNumber = Apexpages.currentPage().getParameters().get('productNumber');
        String productDescription = Apexpages.currentPage().getParameters().get('productDescription');
        String productType = Apexpages.currentPage().getParameters().get('productType');

        if (productSearchData.productsFromSearch != null)
        {
            productSearchData.productsFromSearch.clear();
        }
        
        if (productNumber.Length() > 1 || productDescription.length() > 1 || productType.length() > 1)
		{
			ProductSearchProcess productSearch = new ProductSearchProcess();

			productSearchData.productsFromSearch = productSearch.GetProducts(productNumber, productDescription, productType);
            
            if(!productSearchData.productsFromSearch.isEmpty())
            {
                productSearchData.hasRecords = true;
            }
		}

		return null;
	}
	
	public PageReference ProcessSelectedProducts()
	{
		Id quoteId = System.currentPageReference().getParameters().get('id');
        
		ProductSearchProcessSave saveProducts = new ProductSearchProcessSave();

		String uiMessage = saveProducts.SaveSelectedProducts(quoteId, productSearchData);

		if (uiMessage != null)
		{
			ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, uiMessage));
		}

		return null;
	}

	public PageReference BackToQuote()
	{
		Id quoteNumber = System.currentPageReference().getParameters().get('id');

		string Oppty_Id = [SELECT OpportunityId FROM Quote WHERE Id = :quoteNumber].OpportunityId;
		boolean AccntOpty = [SELECT Account_Opportunity__c FROM Opportunity WHERE Id = :Oppty_Id].Account_Opportunity__c;

		if (AccntOpty == true)
		{
			return new PageReference('/apex/newquotepage?id=' + QuoteNumber);
		}

		if (AccntOpty == false)
		{
			return new PageReference('/' + QuoteNumber);
		}

		return new PageReference('/' + QuoteNumber);
	}
}

Test Class:
@isTest
private class ProductSearchProcess_Tests
{
    @testSetup static void GetTestData()
    {
        TestDataFactory_Accounts.Execute();
        TestDataFactory_Products.Execute();
        TestDataFactory_SpecialDiscounts.Execute();
        TestDataFactory_Pricebook.Execute();
        TestDataFactory_ExchangeRates.Execute();
        List<Opportunity> testOpportunities = new List<Opportunity> ();
        List<Quote> testQuotes = new List<Quote> ();
        List<Account> testAccounts = [Select Id from Account];
        
        integer i = 0;
        
        for (Account testAccount : testAccounts)
        {
            Opportunity opty = new Opportunity();
            
            opty.Name = 'Opportunity' + i;
            opty.AccountId = testAccount.Id;
            opty.CloseDate = System.today().addMonths(1);
            opty.StageName = 'Prospect';
            
            testOpportunities.add(opty);
            
            i++;
        }
        
        insert testOpportunities;
        
        i = 0;
        
        //inserting test Quotes
        
        Id StandardPriceBookId = test.getStandardPricebookId();
        
        for (Opportunity testOpportunity : testOpportunities)
        {
            Quote newQuote = new Quote();
            
            newQuote.Pelco_Account_Name__c = testOpportunity.AccountId;
            newQuote.OpportunityId = testOpportunity.Id;
            newQuote.Name = 'Test Quote' + i;
            newQuote.Pricebook2Id = StandardPriceBookId;
            
            TestQuotes.add(newQuote);
            
            i++;
        }
        
        insert TestQuotes;
        system.debug('TestQuotes='+TestQuotes);
        system.debug('testOpportunities='+testOpportunities);
    }
    //Positive Tests
    private static testMethod void Test_Product()
    {
        
        PageReference pageRef = page.ProductSearch;
        
        Test.setCurrentPageReference(pageRef);
        
        system.debug('pageRef'+pageRef);
        
        List<Quote> testQuotes = [select id,name from Quote];
        
        ProductSearch_GetQuoteLineItems getQuoteLineItems = new ProductSearch_GetQuoteLineItems();
        
		ApexPages.CurrentPage().getparameters().put('id', TestQuotes[0].id);
        ApexPages.currentPage().getParameters().put('productNumber', 'EPM');
		ApexPages.currentPage().getParameters().put('productDescription', 'En');
		ApexPages.currentPage().getParameters().put('productType', 'i');
        
		system.debug('TestQuotes[0].id='+TestQuotes[0].id);         
        
		Test.startTest();
              
        ApexPages.StandardController sc = new ApexPages.standardController(TestQuotes[0]);
        
        ProductSearchController psController = new ProductSearchController(sc);
                
        psController.runSearch();
        
        psController.ProcessSelectedProducts();
        
        Test.stopTest();
       
        //Test.startTest();
        
        //ProductSearchProcess search = new ProductSearchProcess();
        
        //List<ProductSearchItemDataModel> productsFromSearch = search.GetProducts('tb1', '', '');
        
        //Integer actualValue = productsFromSearch.size();
        
        //Test.stopTest();
        
        //System.assertEquals(1, actualValue);
    }
}

I am not sure why it has 0 code coverage.

Regards, 
Gaurav
pconpcon
It should have some coverage.  How are you running the tests and how are you seeing that you have 0 coverage?
Amit Chaudhary 8Amit Chaudhary 8
Hi Gaurav.

In Line number 67 please create Quote record and replace with below line.
List<Quote> testQuotes = [select id,name from Quote];


 
Gaurav AgnihotriGaurav Agnihotri
Thanks pcon and Amit.

pcon, 
I apologize for confusion. I do have some code coverage as shown in attachmentCode Coverage
I am struggling to get100%  code coverage.
Gaurav AgnihotriGaurav Agnihotri
Here is the class:
public class ProductSearchProcessSave
{
	public String SaveSelectedProducts(Id quoteId, ProductSearchDataModel productSearchData)
	{
		String uiMessage;
		String profileName = [SELECT Name FROM Profile WHERE Id = :UserInfo.getProfileId()].Name;

		List<Quote> currentQuote = [SELECT Pelco_Account_Name__c, Submitted__c FROM Quote WHERE Id = :quoteId];

		if (currentQuote[0].Submitted__c && profileName != 'System Administrator')
		{
			uiMessage = 'This Quote has been submitted for approval or has already been approved, therefore you cannot add additional products. Please submit a request to GSOExtranet@schneider-electric.com if you need to add products to this quote';
		}
		else
		{
			List<Account> currentAccount = [SELECT AccountNumber, Currency_Code__c FROM Account WHERE Id = :currentQuote[0].Pelco_Account_Name__c];

			//if (currentAccount[0].AccountNumber == null)
			//{
			//uiMessage = 'Pricing not possible. Customer Number is null. Please add Customer Number for Account Name: ' + AccountName
			//}
			//else
			//{

			//}

			ProductSearch_GetQuoteLineItems getQuoteLineItems = new ProductSearch_GetQuoteLineItems();

			List<ProductSearchItemDataModel> productsOnQuote = getQuoteLineItems.Execute(quoteId);
			System.debug('productsOnQuote.size()='+productsOnQuote.size());
			System.debug('productsOnQuote'+productsOnQuote);
			if (!productSearchData.productsFromSearch.isEmpty())
			{
				QuoteLineItems_GetDealerPrice getDealerPrice = new QuoteLineItems_GetDealerPrice();
				List<QuoteLineItem> productsToInsert = new List<QuoteLineItem> ();

				List<String> productNumbers = new List<String> ();

				Boolean insertProduct;

				for (ProductSearchItemDataModel productFromSearch : productSearchData.productsFromSearch)
				{
					insertProduct = true;

					if (productFromSearch.productQuantity > 0 && productFromSearch.productQuantity < 1000000)
					{
						for (ProductSearchItemDataModel quoteProduct : productsOnQuote)
						{
							System.debug('quoteProduct.Product2Id ='+quoteProduct.Product2Id);
							System.debug('productFromSearch.product2Id ='+productFromSearch.product2Id);
							if (quoteProduct.Product2Id == productFromSearch.product2Id)
							{
								insertProduct = false;
								uiMessage=productFromSearch.productNumber+' has already been added to the Quote. If you want to change the Qantity go back to Quote and edit the Product\'s Quantity.';
								break;
							}
						}

						if (insertProduct)
						{
							productNumbers.add(productFromSearch.productNumber);

							QuoteLineItem productToInsert = new QuoteLineItem();

							productToInsert.QuoteId = quoteId;
							productToInsert.PricebookEntryId = productFromSearch.pricebookEntryId;
							productToInsert.Product2Id = productFromSearch.product2Id;
							productToInsert.Quantity = productFromSearch.productQuantity;
							productToInsert.Currency__c = currentAccount[0].Currency_Code__c;
							productToInsert.Discount_Code__c = productFromSearch.discountCode;
							//productToInsert.Discount = getStandardDiscount.Execute(productFromSearch.productNumber, currentAccount[0].AccountNumber);
							productToInsert.UnitPrice = productFromSearch.msrpPrice;
							//productToInsert.Dealer_Price__c = getDealerPrice.Execute(productFromSearch.productNumber, currentAccount[0].AccountNumber);
							productToInsert.CCO_Standard_Cost__c = productFromSearch.ccoStandardCost;

							productsToInsert.add(productToInsert);

							productsOnQuote.Add(productFromSearch);
						}
					}
				}

				System.debug('productsToInsert.size()='+productsToInsert.size());
				System.debug('productsToInsert='+productsToInsert);


				PopulateStandardDiscounts(productsToInsert, productNumbers, currentAccount[0].AccountNumber);

				PopulateDealerPrices(productsToInsert, productNumbers, currentAccount[0].AccountNumber);

				productSearchData.productsOnQuote = productsOnQuote;

				insert productsToInsert;
			}
		}

		return uiMessage;
	}

	private void PopulateStandardDiscounts(List<QuoteLineItem> productsToInsert, List<String> productNumbers, String accountNumber)
	{
		if (!productsToInsert.isEmpty())
		{
			QuoteLineItems_GetStandardDiscount getStandardDiscount = new QuoteLineItems_GetStandardDiscount();

			List<QuoteLineItems_GetStandardDiscount_Data> discounts = getStandardDiscount.Execute(productNumbers, accountNumber);
			System.debug('discounts='+discounts);
			for (QuoteLineItem item : productsToInsert)
			{
				for (QuoteLineItems_GetStandardDiscount_Data discount : discounts)
				{
					System.debug('item.Product2Id='+item.Product2Id);
					System.debug('discount.Product2Id='+discount.Product2Id);

					
					if (item.Product2Id == discount.Product2Id)
					{
						item.Discount = discount.DiscountPercent;
						break;
					}
				}
			}
		}
	}

	private void PopulateDealerPrices(List<QuoteLineItem> productsToInsert, List<String> productNumbers, String accountNumber)
	{
		if (!productsToInsert.isEmpty())
		{
			//System.debug('PopulateDealerPrices');
			//System.debug('productsToInsert=' + productsToInsert);
			//System.debug('productNumbers=' + productNumbers);
			QuoteLineItems_GetDealerPrice getDealerPrice = new QuoteLineItems_GetDealerPrice();
			
			List<QuoteLineItems_GetDealerPrice_Data> dealerPrices = getDealerPrice.Execute(productNumbers, accountNumber);

			for (QuoteLineItem item : productsToInsert)
			{
				for (QuoteLineItems_GetDealerPrice_Data dealerPrice : dealerPrices)
				{
				   //System.debug(item);
				  
				   //System.debug('Found dealerPrice');
				   //System.debug('item.Product2.Item__c ='+item.Product2.Item__c );
				   //System.debug('item.Product2Id='+item.Product2Id);
				   //System.debug('dealerPrice.ItemNumber='+dealerPrice.ItemNumber);
				   //System.debug('dealerPrice.Product2Id='+dealerPrice.Product2Id);
					if (item.Product2Id == dealerPrice.Product2Id)
					{
						//System.debug('inside if loop');
						item.Dealer_Price__c = dealerPrice.Price;
						//System.debug(dealerPrice.Price);

						break;
					}
				}
			}
		}
	}
}

After 
ProductSearch_GetQuoteLineItems getQuoteLineItems = new ProductSearch_GetQuoteLineItems(); 
none of the code is covered
Chandra Sekhar CH N VChandra Sekhar CH N V
Are you running the tests from developer console? Try running them from setup menu (Develop--> Apex Test Execution)