+ Start a Discussion
ckellieckellie 

Test Code coverage Question

On the below test code I am covering 53% and have an error that says the following:

 

 

TestQuoteExt.basicAttachTest

	System.QueryException: List has no rows for assignment to 
SObject	

Class.quoteExt.attachQuote: line 17, column 23 Class.TestQuoteExt.basicAttachTest: line 42, column 32 External entry point

 

 

I am running agains a brick wall. How to I solve this error in the test class?

 

Test Class:

 

@IsTest private class TestQuoteExt {

    /* This is a basic test which simulates the primary positive case for the 
       save method in the quoteExt class. */
    public static testmethod void basicSaveTest() {

        /* Construct the standard controller for quote. */
        ApexPages.StandardController con = new ApexPages.StandardController(new quote());

        /* Switch to runtime context */
        Test.startTest();

        /* Construct the quoteExt class */
        QuoteExt ext = new QuoteExt(con);


        /* Switch back to test context */
        Test.stopTest();
    }

    /* This is a basic test which simulates the primary positive case for the 
       attachQuote method in the quoteExt class. */
    public static testmethod void basicAttachTest() {

        /* Construct the standard controller for quote. */
        ApexPages.StandardController con = new ApexPages.StandardController(new Quote());

        /* Construct the quoteExt class */
        QuoteExt ext = new QuoteExt(con);

        /* Call save on the ext */
        // ext.save();       

        /* Set the extension quote object using the id on the controller. */
        ext.q = new quote(id = con.getId());  

        /* Switch to runtime context */
        Test.startTest();

        /* Simulate the button invocation of the attachQuote action method 
           on the extension. */
        PageReference result = ext.attachQuote();

         /* Switch back to test context */
        Test.stopTest();

        /* Verify the navigation outcome is as expected */
        System.assertEquals(result.getUrl(), con.view().getUrl());

        /* Verify the attachment was created. */
       System.assert([select name from attachment where parentid =:con.getId()].name!= null);
    }


    /* This setup method will create an opportunity with line items and a primary
       contact role for use in various tests. */
    private static Opportunity setupTestOpportunity() {

        /* Create an account */
        Account a = new Account();
        a.name    = 'TEST';
        Database.insert(a);

        /* Get the standard pricebook. There must be a standard pricebook already 
           in the target org.  */
        Pricebook2 pb = [select name, isactive from Pricebook2 where IsStandard = true limit 1];

        if(!pb.isactive) {
            pb.isactive = true;
            Database.update(pb);
        }

        /* Get a valid stage name */
        OpportunityStage stage = [select MasterLabel from OpportunityStage limit 1];

        /* Setup a basic opportunity */
        Opportunity o  = new Opportunity();
        o.Name         = 'TEST';
        o.AccountId    = a.id;
        o.CloseDate    = Date.today();
        o.StageName    = stage.masterlabel;
        o.Pricebook2Id = pb.id;

        /* Create the opportunity */
        Database.insert(o);

        /* Setup a basic Template*/
        PDF_Template__c pd  = new PDF_Template__c();
        pd.Name         = 'English';
        pd.Template_Address__c = 'apex/English';
        
        /* Create the Template */
        Database.insert(pd);        

        /* Setup a basic quote*/
        Quote q  = new Quote();
        q.Name         = 'TEST';
        q.OpportunityId    = o.id;
        q.PDF_Template__c = pd.id;


        /* Create the opportunity */
        Database.insert(q);

        /* Create a contact */
        Contact c   = new Contact();
        c.lastname  = 'LASTNAME';
        c.firstname = 'FIRSTNAME';

        Database.insert(c);

        /* Create the opportunity contact role */
        OpportunityContactRole r = new OpportunityContactRole();
        r.ContactId     = c.id;
        r.OpportunityId = o.id;
        r.IsPrimary     = true;
        r.role          = 'ROLE';

        Database.insert(r);

        /* Create a product2 */
        Product2 p = new Product2();
        p.Name     = 'TEST';

        Database.insert(p);

        /* Create a pricebook entry. */
        PricebookEntry pbe = new PricebookEntry();
        pbe.Pricebook2Id = pb.id;
        pbe.Product2Id   = p.id;
        pbe.IsActive     = true;
        pbe.UnitPrice    = 1;

        Database.insert(pbe);

        /* Create a line item */
        OpportunityLineItem i = new OpportunityLineItem();
        i.opportunityId       = o.id;
        i.pricebookentryid    = pbe.id;
        i.quantity            = 1;
        i.unitprice           = 1;

        Database.insert(i);

        /* Set up the opportunity with the related records */
        r.Contact        = c;
        r.Opportunity    = o;
        o.Account        = a;
        i.Opportunity    = o;
        pbe.Product2     = p;
        pbe.Pricebook2   = pb;
        i.PricebookEntry = pbe;

        return o;
    }
}

 

 

Class:

 

 

    ApexPages.StandardController controller;

    public Quote q {get;set;}

    public quoteExt (ApexPages.StandardController c) {
        controller = c;
        q          = (Quote) c.getRecord();

    }

public PageReference attachQuote() {

        PageReference pdfPage;

        Quote quote = [select id, PDF_Template__r.id, PDF_Template__c, PDF_Template__r.Template_Address__c from Quote 
                where id = :ApexPages.currentPage().getParameters().get('id') ];
   
        if(Quote.PDF_Template__c != null) {
            pdfPage = new PageReference('/apex/' + quote.PDF_Template__r.Template_Address__c);
        }
         
        pdfPage.getParameters().put('id',q.id);
        
        Blob pdfBlob = pdfPage.getContentAsPDF();
        
        QuoteDocument a = new QuoteDocument(quoteId= q.id, Document = pdfBlob);
        
        insert a;
        
        return controller.view();
    }
}

 

Thank you

 

Best Answer chosen by Admin (Salesforce Developers) 
Sonali BhardwajSonali Bhardwaj

Try

 

 

public static testmethod void basicAttachTest() {

Quote q = new Quote(); // enter required field here
insert q;
ApexPages.StandardController con = new ApexPages.StandardController(q);

/* Construct the quoteExt class */
QuoteExt ext = new QuoteExt(con);

/* Call save on the ext */
// ext.save();

/* Set the extension quote object using the id on the controller. */
ext.q = new quote(id = con.getId());
ApexPages.currentPage().getParameters().put('id', q.id);

/* Switch to runtime context */
Test.startTest();

/* Simulate the button invocation of the attachQuote action method
on the extension. */
PageReference result = ext.attachQuote();

/* Switch back to test context */
Test.stopTest();

/* Verify the navigation outcome is as expected */
System.assertEquals(result.getUrl(), con.view().getUrl());

/* Verify the attachment was created. */
System.assert([select name from attachment where parentid =:con.getId()].name!= null);
}

}

 

 

All Answers

Sonali BhardwajSonali Bhardwaj

Try

 

 

public static testmethod void basicAttachTest() {

Quote q = new Quote(); // enter required field here
insert q;
ApexPages.StandardController con = new ApexPages.StandardController(q);

/* Construct the quoteExt class */
QuoteExt ext = new QuoteExt(con);

/* Call save on the ext */
// ext.save();

/* Set the extension quote object using the id on the controller. */
ext.q = new quote(id = con.getId());
ApexPages.currentPage().getParameters().put('id', q.id);

/* Switch to runtime context */
Test.startTest();

/* Simulate the button invocation of the attachQuote action method
on the extension. */
PageReference result = ext.attachQuote();

/* Switch back to test context */
Test.stopTest();

/* Verify the navigation outcome is as expected */
System.assertEquals(result.getUrl(), con.view().getUrl());

/* Verify the attachment was created. */
System.assert([select name from attachment where parentid =:con.getId()].name!= null);
}

}

 

 

This was selected as the best answer
ckellieckellie

I have had to run Amend your suggestion for additional code coverage, but this worked beautifully.

 

Thank you so much.

ckellie