You need to sign in to do that
Don't have an account?
Gaurav Agnihotri
unable to create a test class
Gurus,
I am new to salesforce.com. I need help with creating test class.
Class:
Test Class:
I am not sure why it has 0 code coverage.
Regards,
Gaurav
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
In Line number 67 please create Quote record and replace with below line.
pcon,
I apologize for confusion. I do have some code coverage as shown in attachment
I am struggling to get100% code coverage.
After
ProductSearch_GetQuoteLineItems getQuoteLineItems = new ProductSearch_GetQuoteLineItems();
none of the code is covered