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
jobojobo 

Simple Testclass


Update:
I tried a bit an made the method with queries alle case-emails returning the value itself:

 public List<EmailMessage> ViewData(string incId) {

 
  String query = 'Select ToAddress, TextBody, SystemModstamp, Subject, Status, ParentId, '
  + 'MessageDate, LastModifiedDate, LastModifiedById, IsDeleted, Incoming, '
  + 'Id, HtmlBody, Headers, HasAttachment, FromName, FromAddress, CreatedDate, '
  + 'CreatedById, CcAddress, BccAddress, ActivityId '
  + 'From EmailMessage WHERE '
  + 'ParentId = \''+ incId + '\'';
  //+ 'ParentId = \''+ incident.Id + '\''; 
 
 myquery = query;
 myEmail = Database.query(query);
 return myEmail;
 }
 
 
but now my apex page can't access the controller extension:

<apex: page standardController="Case" extensions="mySendMail" action="{!ViewData}" id="mySendMail_PageLoad">
results in >>> Unknown method 'CaseStandardController.ViewData() <<<<

why is that? i did this because i wanted to access the method from my testclass with a case id 
and compare the anticipated results.

please note that incident and case are the for the same thing

__________________________________________________________________________________________

Hi there, i'm a bloody beginner in apex and i don't know how to write a simple test-class 
for the following controller extension of the case controller : 

it was written to avoid an "email to:" field from beeing filled with the
standard contact for the account associated with the case when replying to a mail
in the case. it is called in a special section within the case-vf page. all emails
for this case are displayed an can be replyed by link. it works perfectly but must be
tested as you know. 


public class mySendMail {
 
  private List<EmailMessage> myEmail;
  public List<EmailMessage> getmyEmail() {return myEmail;}
  public string myquery {get;set;} // testqueryoutput in page
  public string caseId {get;set;} // caseId-exchange
 
 
 //standardcontrollerfunctionality
  Case incident;
  ApexPages.StandardController controller;
  public mySendMail(ApexPages.StandardController controller) {
  this.controller = controller;
  this.incident = (Case)controller.getRecord();
  } 
 
 public PageReference ViewData() {
 
  String query = 'Select ToAddress, TextBody, SystemModstamp, Subject, Status, ParentId, '
  + 'MessageDate, LastModifiedDate, LastModifiedById, IsDeleted, Incoming, '
  + 'Id, HtmlBody, Headers, HasAttachment, FromName, FromAddress, CreatedDate, '
  + 'CreatedById, CcAddress, BccAddress, ActivityId '
  + 'From EmailMessage WHERE '
  + 'ParentId = \''+ incident.Id + '\''; 
 
 myquery = query;
 myEmail = Database.query(query);
 return null;
  
  
 }

}


the code below is my testclass attempt so far. i have no idea how to call
a testcase of mine from which i know what emails and their subjects are.
i simply wanna compare the amount of emails or some or one subject.  


@IsTest private class mySendMailTest {
 
   
  //initializing the standard case controller  
  ApexPages.StandardController con = new ApexPages.StandardController(new Case());
   
  //initializing the controllerextension
  mySendMail ext = new mySendMail(con);

  public static testmethod void basicTest() {

   
   
   
  }



}

 

 

 

 

 

Message Edited by jobo on 02-26-2009 09:21 AM
Best Answer chosen by Admin (Salesforce Developers) 
gtuerkgtuerk

PageAction methods must return PageReference or void.  You can't return a list of anything to a page from a PageAction method.  If what you are trying to do is test the ActionMethod, use:

 

Test.setCurrentPageReference(ApexPages.PageReference)

 

Set this to your initial context (your wrapper page) and set the return of a local PageReference variable to the results of your action method

 

PageReference testPage =  ext.ViewData();

All Answers

gtuerkgtuerk

PageAction methods must return PageReference or void.  You can't return a list of anything to a page from a PageAction method.  If what you are trying to do is test the ActionMethod, use:

 

Test.setCurrentPageReference(ApexPages.PageReference)

 

Set this to your initial context (your wrapper page) and set the return of a local PageReference variable to the results of your action method

 

PageReference testPage =  ext.ViewData();

This was selected as the best answer
jobojobo

Thanks! This is my testclass now and it covers 86% of code.
that's fine but i'm open for suggestions how to cover more code.


@IsTest private class mySendMailTest {

  public static testmethod void basicTest() {
   
  Case incident;
  incident = [SELECT Id FROM Case LIMIT 1];  
  
  ApexPages.StandardController con = new ApexPages.StandardController(incident);
  System.debug(incident.id);  
   
  mySendMail ext = new mySendMail(con);
   
  PageReference mySendMail = ext.ViewData();
  List<EmailMessage> myEmail;
   
  }



}
 

Message Edited by jobo on 02-28-2009 04:47 AM
gtuerkgtuerk
call 'getMyEmail' from your test class after calling the viewdata method.  be sure to test the return, like a good tester