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
SlanganSlangan 

Test Method only showing 25% Coverage???

Hello everyone & thanks again for a great forum. Your help and advice is always very appreciated!

 

I have written some code with a test method, but it is only showing up with 25% coverage. This is my first crack at writing tests, so I am not sure what I am missing. If someone could please help me understand what I am missing I would be really appreciative. 

 

Thanks!

Shannon

 

 

public class String_functions{
Opportunity opp;

public String_functions(ApexPages.StandardController controller) {
opp = (Opportunity)controller.getRecord();
opp = [select id, StageName, CloseDate, name,Special__c, Adders_Optional_Scope_of_Supply__c, Tender_Name_or_Reason_for_Quotation__c, Commissioning_Start_up_Training__c from opportunity where id = :opp.id];
}

public string getReasonText()
{
string paragraph = opp.Tender_Name_or_Reason_for_Quotation__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getSpecialText()
{
string paragraph = opp.Special__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getTrainingText()
{
string paragraph = opp.Commissioning_Start_up_Training__c.replaceAll('\n','<br/>');
return paragraph;
}

public string getAddersText()
{
string paragraph = opp.Adders_Optional_Scope_of_Supply__c.replaceAll('\n','<br/>');
return paragraph;
}


//******************
//Test Method
//******************
public static testMethod void TestString_functions() {

// Insert test Opportunity
Opportunity testOppty = new opportunity (
Name = 'Test Opportunity',
Amount = 5000,
StageName = 'Closed Won',
CloseDate = System.today(),
Tender_Name_or_Reason_for_Quotation__c = 'test\ntest2\ntest3\ntest4',
Commissioning_Start_up_Training__c = 'test\ntest2\ntest3\ntest4',
Special__c = 'test\ntest2\ntest3\ntest4',
Adders_Optional_Scope_of_Supply__c ='test\ntest2\ntest3\ntest4');
insert testOppty;

// Instantiate VisualForce Page
PageReference pg = Page.Quote;
Test.setCurrentPage(pg);
ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate String_function controller
ApexPages.StandardController s = new ApexPages.standardController(new Opportunity());
String_functions qe = new String_functions(s);

// Test: getReason_Text
string paragraphTest1= qe.getReasonText();
System.assertEquals (paragraphTest1, 'test<br/>test2<br/>test3<br/>test4');

// Test: getSpecial_Text
string paragraphTest3= qe.getSpecialText();
System.assertEquals (paragraphTest3, 'test<br/>test2<br/>test3<br/>test4');

// Test: getTraining_Text
string paragraphTest2= qe.getTrainingText();
System.assertEquals (paragraphTest2, 'test<br/>test2<br/>test3<br/>test4');

// Test: getAdders_Text
string paragraphTest4= qe.getAddersText();
System.assertEquals (paragraphTest4, 'test<br/>test2<br/>test3<br/>test4');
}
}

//The End

 

The results of the test are below:

 

 

*** Beginning Test 1: String_functions.public static testMethod void TestString_functions() 20090720142314.487:Class.String_functions.TestString_functions: line 49, column 4: Insert: SOBJECT:Opportunity *** Beginning Workflow Evaluation User: Shannon Langan Start Time: 20090720142314.871 Starting All Rules Evaluation Starting evaluation of rule type Assignment Starting evaluation of rule type Response Starting evaluation of rule type Workflow [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Shipping Address Trigger type: ON_ALL_CHANGES Evaluating Workflow Entry Criteria: [Opportunity : Shipping Address equals null] Value found: null Criteria evaluates to true [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Order Approved Notification to MGMT Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: [Opportunity : Amount less than CAD 100000] AND [Opportunity : Order Status equals SOA Approved (Mgmt & Finance)] Value found: 5000 Value found: 1 Criteria evaluates to false [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Payment Terms Overide Default Values Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: [Opportunity : Payment Terms Override equals NULL] Value found: null Criteria evaluates to false [Opportunity: Test Opportunity 006S0000002eI82] Rule Name: Payment Term Change - ReApprove Trigger type: ON_CREATE_OR_TRIGGERING_UPDATE Evaluating Workflow Entry Criteria: Formula: IF(AND(Payment_Terms_Override__c<> NULL, NOT(ISPICKVAL(Order_Status__c,"Not Submitted"))),true,false) Value(s) Found: Order_Status__c=Not Submitted, Payment_Terms_Override__c=null Criteria evaluates to false Spooling All Immediate Actions [Opportunity: Test Opportunity 006S0000002eI82] Field Update Field: Opportunity: Shipping Address Value: , , , , Ending All Rules Evaluation Bulk Execute all Immediate Actions Starting evaluation of rule type Escalation End Time: 20090720142314.921 *** Ending Workflow Evaluation20090720142314.487:Class.String_functions.TestString_functions: line 49, column 4: DML Operation executed in 381 ms 20090720142314.487:Class.String_functions.: line 6, column 11: SOQL query with 0 rows finished in 6 ms System.QueryException: List has no rows for assignment to SObject Class.String_functions.: line 6, column 11 Class.String_functions.TestString_functions: line 58, column 26 Cumulative resource usage: Resource usage for namespace: (default) Number of SOQL queries: 1 out of 100 Number of query rows: 0 out of 500 Number of SOSL queries: 0 out of 20 Number of DML statements: 1 out of 100 Number of DML rows: 1 out of 500 Number of script statements: 10 out of 200000 Maximum heap size: 0 out of 1000000 Number of callouts: 0 out of 10 Number of Email Invocations: 0 out of 10 Number of fields describes: 0 out of 10 Number of record type describes: 0 out of 10 Number of child relationships describes: 0 out of 10 Number of picklist describes: 0 out of 10 Number of future calls: 0 out of 10 Number of find similar calls: 0 out of 10 Number of System.runAs() invocations: 0 out of 20 Total email recipients queued to be sent : 0 Stack frame variables and sizes: Frame0 *** Ending Test String_functions.public static testMethod void TestString_functions()

 

 

 

 

 

Message Edited by Slangan on 07-20-2009 07:33 AM
Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

Hey

 

Your test is failing here

 

20090720142314.487:Class.String_functions.: line 6, column 11: SOQL query with 0 rows finished in 6 msSystem.QueryException: List has no rows for assignment to SObjectClass.String_functions.: line 6, column 11 

Class.String_functions.TestString_functions: line 58, column 26

 

It seems that the query in your constructor is returning no rows.  You should change your testing thus:

 

// Instantiate VisualForce Page
// PageReference pg = Page.Quote;
// Test.setCurrentPage(pg);
// ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate String_function controller
ApexPages.StandardController s = new ApexPages.standardController(testOppty);

String_functions qe = new String_functions(s); 

 

Here I am passing teh record to the standardController directly. This mimics what happens in your class, there is no need to use parameters in your case.

 

Cheers,

Wes 

All Answers

wesnoltewesnolte

Hey

 

Your test is failing here

 

20090720142314.487:Class.String_functions.: line 6, column 11: SOQL query with 0 rows finished in 6 msSystem.QueryException: List has no rows for assignment to SObjectClass.String_functions.: line 6, column 11 

Class.String_functions.TestString_functions: line 58, column 26

 

It seems that the query in your constructor is returning no rows.  You should change your testing thus:

 

// Instantiate VisualForce Page
// PageReference pg = Page.Quote;
// Test.setCurrentPage(pg);
// ApexPages.currentPage().getParameters().put('id', testOppty.id);

// Instantiate String_function controller
ApexPages.StandardController s = new ApexPages.standardController(testOppty);

String_functions qe = new String_functions(s); 

 

Here I am passing teh record to the standardController directly. This mimics what happens in your class, there is no need to use parameters in your case.

 

Cheers,

Wes 

This was selected as the best answer
SlanganSlangan

Yup, that did it.

 

So why didn't I need to instantiate a test VF Quote page? Don't you need to actually create the page to test it? 

 

Sorry for the probably really silly questions, but as I said, I am very new at this.

wesnoltewesnolte

There are no silly questions:)

 

You don't test actual pages (I think this is because they're the view of the MVC and shouldn't have any logic) only controllers. If your apex code fetched parameters using ApexPages.currentPage().getParameters().get() then you would use the code I've commented out(or something like it).

 

In your case though it is unnecessary because when viewing the page, behind the scenes an object is passed to your controller. You simply have to perform that bit manually for testing.

 

Wes

SlanganSlangan

You are amazing. Thank-you so much for the info.

 

Shannon

wesnoltewesnolte

Lil' ol me? *blushes*

 

SlanganSlangan

Hey again -

 

I have another question for you on test classes I was wondering if you could help me with (please let me know if I am overstepping at all or bugging you).

 

I have a class that I wrote to create a pdf email attachment and then save the PDF to the Notes & Attachments area of an opportunity (see below). Where the last test class I wrote was relatively obvious I have no idea where to start with this one.

 

If you could offer any advice on this it would be absolutely the best thing that happened to me today.

 

 

public class addERF { ApexPages.StandardController controller; public addERF(ApexPages.StandardController c) { controller = c;} public PageReference emailERF() { // Get the opportunity Id and name Opportunity opportunity = [SELECT id, name,Opportunity_No__c FROM opportunity WHERE Id = :System.currentPageReference().getParameters().get('id')]; // Create PDF // Reference the page, pass in a parameter to force PDF PageReference pdf = new PageReference('/apex/ERFpdf' + '?id=' + opportunity.id); pdf.getParameters().put('p','p'); pdf.setRedirect(true); // Grab the PDF! Blob b = pdf.getContent(); //Create the attachment against the Opportunity

Attachment a = new Attachment(parentId = opportunity.id, name=opportunity.name + '-'+ opportunity.Opportunity_No__c+'-'+'ERF.pdf', body = b); //Insert the attachment

insert a; // Create an email Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage (); email.setSaveAsActivity(true); email.setToAddresses(addresses); email.setSenderDisplayName('New Order'); email.setSubject('New ERF Created for Opportunity # ' + opportunity.Opportunity_No__c); email.setPlainTextBody('A new order has been created for ' + opportunity.Name +' , '+ opportunity.Opportunity_No__c+'. The ERF is attached.'); email.setHtmlBody('A new order has been created for' + '&nbsp;'+'<b>'+ opportunity.Name +'&nbsp;'+'</b>'+ opportunity.Opportunity_No__c+'. The ERF is attached.'+ ' To view this Opportunity <a href=https://na1.salesforce.com/'+ opportunity.Id +'><b>click here</b></a>'); // Create an email attachment Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment(); // Set name of PDF efa.setFileName(opportunity.Opportunity_No__c+'ERF.pdf'); // Set body of PDF efa.setBody(b); // Attach the PDF to your email email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa}); // Send email & return to Opportunity Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); // Send the user back to the opportunity detail page return controller.view(); } String[] addresses = new String[]{}; public List<SelectOption> getItems() { List<SelectOption> options = new List<SelectOption>(); options.add(new SelectOption('thisGuy@ourcompany.com','His Name')); return options; } public String[] getAddresses() { return addresses; } public void setAddresses(String[] addresses) { this.addresses = addresses; } }

 

 

mavsmavs

Hi

 

Did you find the solution on how to write test methods for send emails in Apex?

 

If so and if you dont mind can you please post your code/solution here.

 

Thanks

mavs