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
MigMig 

Dynamic Apex Test Coverage

Hi all,I'm having some difficulties to get succees with my Test coverage: 

 

The Controler :

 

public PageReference autoRun() {
String oppID = ApexPages.currentPage().getParameters().get('id');
if (oppID == null) {
// Display the Visualforce page's content if no Id is passed over
return null;
}
// Get the sObject describe result for the Opportunity object
Schema.DescribeSObjectResult r = Opportunity.sObjectType.getDescribe();
Map<String, Schema.SObjectField> M = r.fields.getMap(); //Generate a Map of the fields

//Now loop through the list of Field Names and concatenate the SOQL query string
String SOQL = 'Select ';
for (String fieldName : M.keySet()){
System.debug('fieldName: ' + fieldName);
SOQL += fieldName + ',';
}
SOQL = SOQL.substring(0,SOQL.length()-1); //Remove the last , unnecessary comma
SOQL += ' From Opportunity o where id=\''+getOpportunityId()+'\' LIMIT 1' ;

//Execute SOQL & Cast the sObject type into an Opportunity
sObject S = Database.query(SOQL);
Opportunity opp = (Opportunity)S;

//Clone the Opportunity - 2 parameters (PreserveId or not, IsDeepClone or not )
Opportunity newOpp = opp.clone(false,true);
newOpp.Name=opp.Name + ' v2';
insert newOpp;

/*.... All the other related objects like OpportunityLineItems : */

// Go to the new Opportunity
PageReference pageRef = new PageReference('/' + newOpp.Id);
pageRef.setRedirect(true);
return pageRef;


}

  

With my test coverage, the class autorun() is not covered at all !! I've got 28 % but is only the rest of the controler  ... 

 

public class TestOpportunityClones {

static testMethod void validateOpportunityClones(){
Account acc1 = new Account();
acc1.Name = 'Test' ;
insert acc1 ;

/*** To create a Opportunity Line Item, other fields are required : **/
Opportunity opp1 = new Opportunity();
opp1.Name ='Oppty test converage ' ;
opp1.AccountId = acc1.Id ;
opp1.StageName = 'Prospecting' ;
Date d1 = Date.newInstance(2009,1,1);
opp1.CloseDate = d1;
insert opp1 ;

Product2 prod = new Product2();
prod.Name = 'Product Name test' ;
prod.IsActive = true ;
insert prod ;


// Each Product has a standard unit price in the standard PriceBook. This value is required !!!!
Pricebook2 [] pbookstd = [ Select p.id from Pricebook2 p where p.isstandard =: true limit 1] ;

PricebookEntry PriceEntry = new PricebookEntry();
priceEntry.isActive = true ;
PriceEntry.UnitPrice = 250 ;
PriceEntry.PriceBook2Id = pbookstd[0].Id ;
PriceEntry.Product2Id = prod.Id ;
insert PriceEntry ;


OpportunityLineItem oppLineItem1 = new OpportunityLineItem();
oppLineItem1.OpportunityId = opp1.Id ;
oppLineItem1.PricebookEntryId = PriceEntry.Id ;
oppLineItem1.UnitPrice = 500 ;
oppLineItem1.Quantity = 1 ;
insert oppLineItem1 ;


ApexPages.StandardController ac = new ApexPages.StandardController(opp1); //set up the standardcontroller
Opportunityclone1 controler = new Opportunityclone1(ac); //and set up the extensions to the standardcontroller

controler.getOpportunityId() ;
controler.setOpportunityId(opp1.Id);
controler.autoRun();

SObject s = Database.query('select id from Opportunity limit 1');
System.assertEquals(s.getSObjectType(), Opportunity.sObjectType); //

// Create a list of generic sObjects
List<sObject> oppType = new Opportunity[]{};
System.assertEquals(oppType.getSObjectType(), Opportunity.sObjectType);

Do you know how to cover Describe Sobject functions ... 

I continue trying to discover the solution ...


Thanks for all your help.

 

 

 

______________________________________ 

This conversation has began here : (tkx aalbert)

http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=11568#M11568

 

Message Edited by Mig on 01-30-2009 04:49 PM
Message Edited by Mig on 01-30-2009 04:52 PM