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
notsosmartnotsosmart 

Test Methods

I don't believe I grasp the Test Method Function.  I've Added @isTest to my high level class of the controller but when I run the Test function, it returns - 'No Methods found'

 

Is there a resource that would help here?

 

Thanks.

Suresh RaghuramSuresh Raghuram

coluld you please post your class and the testmethods

notsosmartnotsosmart

Thanks

 

/**
* Purpose : This class is a controller Extension for the Trip Report custom UI.
*/
@isTest public with sharing class TripRptExt {

public TripRptExt() {

}

public Attachment attachment {
get {
if (attachment == null)
attachment = new Attachment();
return attachment;
}
set;
}

//Public attributes.
public ApexPages.StandardController controller;
public static final Integer MAX_ATTENDEE_ITEM_NUM = 50;
public static final Integer MAX_OPPORTUNITY_ITEM_NUM = 50;
public Boolean CancelConfirm = false;

//Private attributes.
private Trip_Report__c tripReport;
private String reportItemsMessage;
private Integer attendeeLineCount = 0;
private Integer opportunityLineCount = 0;

//Controller extension constructor.
public TripRptExt(ApexPages.StandardController stdController) {

//Store the passed controller.
controller = stdController;

//Default the id.
reportId = controller.getId();

//Set the record.
if (reportId != null){
tripReport = TripRptManager.getTripReport(reportId);
isUpdate = true;
} else {
tripReport = new Trip_Report__c();
isUpdate = false;
}

}

//===============================================================
//Public properties for display management
//===============================================================

public Trip_Report__c getTripReport(){
return tripReport;
}

public ID reportId{
get {
return ApexPages.currentPage().getParameters().get('id');
}
set {
if (reportId != null) {
//Add to the parm.
ApexPages.currentPage().getParameters().put('id', reportId);
}
}
}

public Boolean isUpdate{
get;
set;
}

public String getReportItemsMessage(){
return reportItemsMessage;
}

//===============================================================
//Public properties for data and logic
//===============================================================

public List<AttendeeLineItem> getAttendeeItemListNotDeleted(){
//List for display on the view.
List<AttendeeLineItem> listToDisplay = new List<AttendeeLineItem>();
for (AttendeeLineItem item : attendeeLineItemList){
if (item.isDeleted != true){
listToDisplay.add(item);
}
}

return listToDisplay;
}

public List<OpportunityLineItem> getOpportunityItemListNotDeleted(){
//List for display on the view.
List<OpportunityLineItem> listToDisplay = new List<OpportunityLineItem>();
for (OpportunityLineItem item : opportunityLineItemList){
if (item.isDeleted != true){
listToDisplay.add(item);
}
}

return listToDisplay;
}

public Integer selectLineNumber{
get{
if (selectLineNumber == null) {
selectLineNumber = 0;
}
return selectLineNumber;
}
set;
}

//===============================================================
//Private methods for line items
//===============================================================

private List<TripContactAssociation__c> attendeeList {
get{
List<TripContactAssociation__c> returnValue = new List<TripContactAssociation__c>();
if(attendeeList == null && reportId != null) {
returnValue = TripRptManager.getAttendeesForReport(reportId);
}
return returnValue;
}
set;
}

//List of wrapper objects.
private List<AttendeeLineItem> attendeeLineItemList{
get{
if (attendeeLineItemList == null){
attendeeLineItemList = new List<AttendeeLineItem>();

//Only loaded on isUpdate.
if (isUpdate){

for(TripContactAssociation__c attendee : attendeeList ){
attendeeLineCount ++;
AttendeeLineItem lineItem = new AttendeeLineItem( attendeeLineCount , attendee);
attendeeLineItemList.add(lineItem);
}
}
}
return attendeeLineItemList;
}
set;
}

//Renumber the lines in the list.
private void renumberAttendeeLineItemList(){
attendeeLineCount = 0;
for ( AttendeeLineItem item : attendeeLineItemList ){
item.lineNumber = 0;
if (item.isDeleted == false){
attendeeLineCount++;
item.lineNumber = attendeeLineCount;
}
}
}

private List<TripOpportunityAssociation__c> opportunityList {
get{
List<TripOpportunityAssociation__c> returnValue = new List<TripOpportunityAssociation__c>();
if(opportunityList == null && reportId != null) {
returnValue = TripRptManager.getOpportunitiesForReport(reportId);
}
return returnValue;
}
set;
}

//List of wrapper objects.
private List<OpportunityLineItem> opportunityLineItemList{
get{
if (opportunityLineItemList == null){
opportunityLineItemList = new List<opportunityLineItem>();

//Only loaded on isUpdate.
if (isUpdate){

for(TripOpportunityAssociation__c opportunity : opportunityList ){
opportunityLineCount ++;
OpportunityLineItem lineItem = new OpportunityLineItem( opportunityLineCount , opportunity);
opportunityLineItemList.add(lineItem);
}
}
}
return opportunityLineItemList;
}
set;
}

//Renumber the lines in the list.
private void renumberOpportunityLineItemList(){
opportunityLineCount = 0;
for ( OpportunityLineItem item : opportunityLineItemList ){
item.lineNumber = 0;
if (item.isDeleted == false){
opportunityLineCount++;
item.lineNumber = opportunityLineCount;
}
}
}

//===============================================================
//Public methods for line items
//===============================================================

//Action method to add a new attendee item in the view.
public PageReference addAttendee(){

reportItemsMessage = null;
ApexPages.getMessages().clear();

if ( attendeeLineCount < MAX_ATTENDEE_ITEM_NUM ){
//Add a new empty row to the list.
attendeeLineCount++;
attendeeLineItemList.add(new AttendeeLineItem( attendeeLineCount, new TripContactAssociation__c( Attendee__c = reportId ) ));
reportItemsMessage = null;
} else {
//Display a message.
reportItemsMessage = 'No more than ' + String.valueOf(MAX_ATTENDEE_ITEM_NUM) + ' attendees are permitted.';
}

return null;
}

//Action method to add a new opportunity item in the view.
public PageReference addOpportunity(){

reportItemsMessage = null;
ApexPages.getMessages().clear();

if ( opportunitylineCount < MAX_OPPORTUNITY_ITEM_NUM ){
//Add a new empty row to the list.
opportunitylineCount++;
opportunityLineItemList.add(new OpportunityLineItem( opportunityLineCount, new TripOpportunityAssociation__c( Trip_Opportunity__c = reportId ) ));
reportItemsMessage = null;

} else {
//Display a message.
reportItemsMessage = 'No more than ' + String.valueOf(MAX_OPPORTUNITY_ITEM_NUM) + ' opportunites are permitted.';
}

return null;
}

//Action method to remove an attendee item from display by issuing a soft delete.
public PageReference removeAttendee(){

reportItemsMessage = null;
ApexPages.getMessages().clear();

if (selectLineNumber != Null ){

//Soft delete the line and renumber.
for ( AttendeeLineItem item : attendeeLineItemList ){
if (item.lineNumber == selectLineNumber){
item.isDeleted = true;
break;
}
}
renumberAttendeeLineItemList();
}

reportItemsMessage = null;
return null;
}

//Action method to remove an opportunity item from display by issuing a soft delete.
public PageReference removeOpportunity(){

reportItemsMessage = null;
ApexPages.getMessages().clear();

if (selectLineNumber != Null ){

//Soft delete the line and renumber.
for ( OpportunityLineItem item : opportunityLineItemList ){
if (item.lineNumber == selectLineNumber){
item.isDeleted = true;
break;
}
}
renumberOpportunityLineItemList();
}

reportItemsMessage = null;
return null;
}

//===============================================================
//Public action and navigation methods
//===============================================================
//Action method to Exit List View. Exit Stnd Vf not used yet
public PageReference Exit() {
// ApexPages.StandardSetController ssc = new ApexPages.StandardSetController(tabTrip_Report);
// this does not work ??
return controller.View().setRedirect(true);
}

public PageReference Stnd() {
reportItemsMessage = null;
ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.INFO, 'Standard Reports');

return controller.view().setRedirect(true);

}

public PageReference vf() {
ApexPages.Message msg = new ApexPages.Message(ApexPages.severity.INFO, 'Visual Force Reports');

return controller.view().setRedirect(true);
// return page.TripRptListView;

}
public PageReference done() {

if (isUpdate)
{
return controller.view().setRedirect(true);
}
else
{
return Page.TripRptDummy;
}
}

public PageReference step1() {
reportItemsMessage = null;
return Page.TripRptStep1;
}

public PageReference step2() {
reportItemsMessage = null;
return Page.TripRptStep2;
}

public PageReference step3() {
reportItemsMessage = null;
return Page.TripRptStep3;
}

public PageReference step4() {
reportItemsMessage = null;
return Page.TripRptStep4;
}

public PageReference step5() {
reportItemsMessage = null;
return Page.TripRptStep5;
}

public PageReference step6() {
reportItemsMessage = null;
return Page.TripRptStep6;
}

public PageReference viewPdf() {
return Page.TripRptPdf;
}


//Action method to confirm Cancel.
public PageReference cancel() {
// Doesn't work ApexPages.addMessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Are you Sure?'));

// return controller.view().setRedirect(true);
// return page.TripRptListView;
// Redirect accordingly.
// if (isUpdate){
// return controller.View().setRedirect(true);
// } else {
// return new ApexPages.StandardController(tripreport).View(); // .setRedirect(true);
// SelectOption option = new SelectOption(tripreport, 'list');
if (tripReport.Unique_Name__c != null)
{
return Page.TripRptCancel;
}
else
{
return Page.TripRptDummy;
}
// }

}

// Confirm Cancel
public PageReference ConfCancel()
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Cancelled'));
if (isUpdate)
{
return controller.View().setRedirect(true);
}
else
{
return Page.TripRptDummy;
}
}

public PageReference NoCancel()
{
CancelConfirm = false;
return Page.TripRptStep1;

}
//Action method to override Save.
public PageReference save() {

return controller.view();
// return page.TripRptListView;
}


//Action method to override.
public PageReference saveReport(){

try {
upsert tripReport;

// removed due to navigation issues, add when fix list view return isUpdate = true;

} catch (system.Exception e) {
//Abandon.
// if not an id issue - assume not unique name
if(reportid != null)
{
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Report Name Must be Unique.'));
return Page.TripRptStep1;
}

}


List<TripContactAssociation__c> attendeeItemsToDelete = new List<TripContactAssociation__c>();
List<TripContactAssociation__c> attendeeItemsToUpsert = new List<TripContactAssociation__c>();

//Loop thru the lines and capture those needing deletion or upsert.
//There may be some to ignore - newly added and subsequently removed,
for ( AttendeeLineItem item : attendeeLineItemList ){
if (item.isDeleted == true && item.attendeeitem.id != null){
attendeeItemsToDelete.add(item.attendeeitem);
}
if (item.isDeleted == false){
attendeeItemsToUpsert.add(item.attendeeitem);
}
}

List<TripOpportunityAssociation__c> opportunityItemsToDelete = new List<TripOpportunityAssociation__c>();
List<TripOpportunityAssociation__c> opportunityItemsToUpsert = new List<TripOpportunityAssociation__c>();

//Loop thru the lines and capture those needing deletion or upsert.
//There may be some to ignore - newly added and subsequently removed,
for ( OpportunityLineItem item : opportunityLineItemList ){
if (item.isDeleted == true && item.opportunity.id != null){
opportunityItemsToDelete.add(item.opportunity);
}
if (item.isDeleted == false){
opportunityItemsToUpsert.add(item.opportunity);
}
}

//Validate for one attendee item.
if (attendeeItemsToUpsert.size() == 0 ){
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'At least one Attendee is required.'));
return Page.TripRptStep2;

}

// Create a savepoint for recovery.
Savepoint sp = Database.setSavepoint();
reportItemsMessage = null;

//Deletions should preceed report update.
if (attendeeItemsToDelete.size() > 0 ){

//Purge the removed items.
try{
delete attendeeItemsToDelete;
} catch (system.Exception e) {
//Abandon.
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Error deleting Attendee Items.'));
return null;
}
}

//Deletions should preceed report update.
if (opportunityItemsToDelete.size() > 0 ){

//Purge the removed items.
try{
delete opportunityItemsToDelete;
} catch (system.Exception e) {
//Abandon.
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Error deleting Opportunity Items.' ));
return null;
}
}

//Save the items.
//Populate new attendee items with report id.
for ( TripContactAssociation__c item : attendeeItemsToUpsert ){
if (item.Attendee__c == null){
item.Attendee__c = tripReport.id;
}
}

tripReport = TripRptManager.getTripReportId(tripReport.unique_Name__c);

//Items upsert should follow to insure report ids.
if (attendeeItemsToUpsert.size() > 0 ){

//Save the new or changed items.
try{
upsert attendeeItemsToUpsert;
} catch (system.Exception e) {
//Abandon.
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Error saving Attendee items.'));
return Page.TripRptStep2;
}
}

//Populate new opportunity items with report id. chgd to saveid as id was cleared

for ( TripOpportunityAssociation__c item : opportunityItemsToUpsert ){
if (item.Trip_Opportunity__c == null){
item.Trip_Opportunity__c = tripReport.id;

}
}

if (opportunityItemsToUpsert.size() > 0 ){

//Save the new or changed items.
try{
upsert opportunityItemsToUpsert;
} catch (system.Exception e) {
//Abandon.
Database.rollback(sp);
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'Error saving Opportunity items.'));
return Page.TripRptStep3;
}

}

//------------------------------------------------------------------------
//Send an email.
//TODO: Call the private method to execute the email send.
//------------------------------------------------------------------------

/* go to List View //Redirect accordingly.
if (isUpdate){
return controller.View().setRedirect(true);
} else {
return new ApexPages.StandardController(tripReport).View().setRedirect(true);
} */

// return page.TripRptListView;

reportItemsMessage = null;
return Page.TripRptStep6;

// return controller.view();

}
// Attachment handling

Public PageReference upload() {

attachment.OwnerId = UserInfo.getUserId();
attachment.ParentId = reportId; // rec id
attachment.IsPrivate = true;

try {
insert attachment;
}

catch (DMLException e) {
ApexPages.addMessage(new
ApexPages.message(ApexPages.severity.ERROR,'Error uploading attachment'));
return null;
}
finally {
attachment = new Attachment();
}

ApexPages.addMessage(new
ApexPages.message(ApexPages.severity.INFO,'Attachment uploaded successfully'));
return null;
}

}

Suresh RaghuramSuresh Raghuram

@isTest private class TestContactCtrl {      

        

   

static testMethod void testSaveNewContact() {     

testContactControllerExt = new ContactControllerExt(stdController);      

   Contact.Test = true;        

 Contact.testContId = '3454';       

  Contact.testStatusCode = '0';        

PageReference pageRef = testContactControllerExt.saveNewContact();

 

The above code is just for reference .

I observed standard controller in your class.

 

A simple procedure to create a test class.

 

Create Test class.

create an instance for that class as given in the above method,

if u need to submit values for insert (before update or delete), insert alone.

Insert hardcoded values.

then make a call to that method

 

I think u got some idea above explination.

If this answers your question make this as a solution