+ Start a Discussion
Vipin K 10Vipin K 10 

Test Class for the Quote

Hi All,

Can someone help me with test class for the below apex class?

public class DisplayQuote
{
    public List<Quote> allquotes{get;set;}
    public Boolean result{get;set;}
    public Id quoteId {get;set;}
    
    public DisplayQuote(ApexPages.StandardController stdController)
    {
        sObject quote = stdController.getRecord();
        quoteId = (Id) quote.get('Id');

        String rowsString = ApexPages.currentPage().getParameters().get('rows');
        if (rowsString == null || rowsString  == '')
        {
        rowsString = '10';
        }

        if (rowsString == 'All')
        {
        rowsString = '1000';
        }
        
        Integer rows = Integer.valueOf(rowsString);
        allQuotes = new List<Quote>([SELECT Opportunity.Account.Name, Opportunity.name,OpportunityId, Name, IsSyncing, Id, ExpirationDate, Discount, CreatedById, Status From Quote WHERE Status = 'Review Pending' limit :rows]);
        if (allQuotes.isEmpty())
        result = false;
        else
        result = true;
    }
}
Nayana KNayana K
@isTest
public class DisplayQuote_Test {
	
	@testSetup static void createData() {
		List<Quote> lstQuote = new List<Quote>();

        Account objAccount = new Account(Name = 'Test Acc1', BillingCity = 'Test City', BillingState = 'Test State', 
                                                                    BillingStreet = 'Test Street', BillingPostalCode = '12345', 
                                                                    BillingCountry = 'Test Country', Phone = '123456');


        insert objAccount;
        
        
        Product2 objProduct = new Product2(Name = 'Test product1', family = 'Cafe');
                                                    
        insert objProduct;
       
        PriceBookEntry objPBE = new PriceBookEntry(UnitPrice = 300, PriceBook2Id = Test.getStandardPricebookId(),
                                                        Product2Id = objProduct.Id, IsActive = true);
                                                               
        
        insert objPBE;
        
        Opportunity objOpp = new Opportunity(Name = 'Test Opp', AccountId = objAccount.Id, StageName = 'Verbal Confirmation', CloseDate = Date.today()+1);
        insert objOpp;
        
        for(Integer i=0; i< 10 ; i++)
		{
			lstQuote.add(new Quote(OpportunityId = objOpp.Id, Name = 'Test Quote' + i, Pricebook2Id = objPBE.Id, Status = 'Review Pending'));
		}
        insert lstQuote;
	}

    public static testMethod void TestDisplayQuote1() {

		PageReference pageRef = Page.REPLACE_WITH_YOURPAGE_NAME_HERE;
        Test.setCurrentPage(pageRef);
		
		StandardController sc = new StandardController(new Quote());
		// Add parameters to page URL
        ApexPages.currentPage().getParameters().put('rows', '');
		
		DisplayQuote controller = new DisplayQuote(sc);
		system.assertEquals(true, controller.result);
		system.assertEquals(10, controller.allquotes.size());

	}
	
	public static testMethod void TestDisplayQuote2() {

		PageReference pageRef = Page.REPLACE_WITH_YOURPAGE_NAME_HERE;
        Test.setCurrentPage(pageRef);
		
		StandardController sc = new StandardController(new Quote());
		// Add parameters to page URL
        ApexPages.currentPage().getParameters().put('rows', 'All');
		
		DisplayQuote controller = new DisplayQuote(sc);
		system.assertEquals(true, controller.result);
		system.assertEquals(10, controller.allquotes.size());

	}
	
	
	public static testMethod void TestDisplayQuote3() {
		
		// delete all quotes so that controller returns empty list
		List<Quote> lstQuoteToDelete = [SELECT Id FROM Quote];
		if(!lstQuoteToDelete.isEmpty())
			delete lstQuoteToDelete;
		
		PageReference pageRef = Page.REPLACE_WITH_YOURPAGE_NAME_HERE;
        Test.setCurrentPage(pageRef);
		
		StandardController sc = new StandardController(new Quote());
		// Add parameters to page URL
        ApexPages.currentPage().getParameters().put('rows', 'All');
		
		DisplayQuote controller = new DisplayQuote(sc);
		system.assertEquals(false, controller.result);
		system.assertEquals(0, controller.allquotes.size());
	}
}

Please check if this works.