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
BrettBBrettB 

test method assistance

Im hitting my head against the wall and need some  assistance... in regards to a  test method...

Im trying to add a button to my production environment, that uses a controller to run some apex and move a S2S opportunity into a partner account.

The problem is i have ZERO percent coverage...below is my code...I know its bad, bu im just starting at this testMethod thing...Please help with what im missing,

Following is my controller and test method



 

public class VFController {
 
      private final Opportunity o;
    public VFController(ApexPages.StandardController stdController) {
        this.o = (Opportunity)stdController.getRecord();
    }
 
    // Code we will invoke on page load.
    public PageReference autoRun() {
 
        String theId = ApexPages.currentPage().getParameters().get('id');
 
        if (theId == null) {
            // Display the Visualforce page's content if no Id is passed over
            return null;
        }
 
        for (Opportunity o:[select id from Opportunity where id =:theId]) {
        PartnerNetworkRecordConnection newConnection =
                    new PartnerNetworkRecordConnection(
                        connectionId = '04PM0000000CaRqMAK',
                        LocalRecordId = o.Id);
                        insert newConnection;
           
        }
 
        // Redirect the user back to the original page
        PageReference pageRef = new PageReference('/' + theId);
        pageRef.setRedirect(true);
        return pageRef;
 
    }
     static TestMethod void test()
     {
      
List<PartnerNetworkRecordConnection> pncs = new List<PartnerNetworkRecordConnection>{};
  for (Opportunity o:[select id from Opportunity]) {
 PartnerNetworkRecordConnection pnc = new  PartnerNetworkRecordConnection ( connectionId = '04PM0000000CaRqMAK',
                        LocalRecordId = o.Id);
                        pncs.add(pnc);
                       
                        insert pnc;
                        
                       List<PartnerNetworkRecordConnection> insertedPncs = [select connectionId, localrecordId from PartnerNetworkRecordConnection where Id in :pncs];
                       }
     }
    }
Best Answer chosen by BrettB
Shashikant SharmaShashikant Sharma

try this

 

static testmethod void testVFController() {
 
Opportunity o = new Opportunity();
o.Name='Test';
o.StageName = 'F6-Prospecting'; 
o.CloseDate = Date.valueOf('2011-07-01'); 
o.MRC__c = 100.00; 
o.NRC__c = 100.00; 
o.Required_Proposal_Date__c = Date.valueOf('2011-07-01');
        

insert o;


ApexPages.StandardController sc = new ApexPages.StandardController(o);
//THIS LINE THROWS AN ERR
ApexPages.currentpage().getparameters().put('id' , o.id);
VFController objCls = new VFController(sc);

objCls.autoRun();



}

  Add one more for better coverage

 

static testmethod void testVFController2() {
 
 
Opportunity o = new Opportunity();
o.Name='Test';
o.StageName = 'F6-Prospecting'; 
o.CloseDate = Date.valueOf('2011-07-01'); 
o.MRC__c = 100.00; 
o.NRC__c = 100.00; 
o.Required_Proposal_Date__c = Date.valueOf('2011-07-01');
        

insert o;


ApexPages.StandardController sc = new ApexPages.StandardController(o);
//THIS LINE THROWS AN ERR

VFController objCls = new VFController(sc);

objCls.autoRun();



}

 

All Answers

Shashikant SharmaShashikant Sharma

Your test method should be like

 

static testmethod void testVFController() {

Opportunity o = new Opportunity();
//Fill all req fields

insert o;

ApexPages.StandardController sc = new ApexPages.StandardController(o);

ApexPages.currentpage().getparameters().put(id , o.id);
VFController objCls = new VFController(sc);

objCls.autoRun();



}

It should increase your code coverage,  Please asserty your result in this test method

BrettBBrettB

ThankYou plenty for helping out...

Good news i have 53 coverage...

Bad news i need 75...

 

I added the code below and got this error when running the test

System.NullPointerException: Key value cannot be null

 

When I remove the line totoaly test= success but only 53 coverage

 

Here is the code implimented:

 

static testmethod void testVFController() {
 String Id = ApexPages.currentPage().getParameters().get('id');
 
Opportunity o = new Opportunity();
o.Name='Test';
o.StageName = 'F6-Prospecting'; 
o.CloseDate = Date.valueOf('2011-07-01'); 
o.MRC__c = 100.00; 
o.NRC__c = 100.00; 
o.Required_Proposal_Date__c = Date.valueOf('2011-07-01');
        

insert o;


ApexPages.StandardController sc = new ApexPages.StandardController(o);
//THIS LINE THROWS AN ERR
ApexPages.currentpage().getparameters().put(id , o.id);
VFController objCls = new VFController(sc);

objCls.autoRun();



}



Please advise at your convenience .

b

 

Shashikant SharmaShashikant Sharma

try this

 

static testmethod void testVFController() {
 
Opportunity o = new Opportunity();
o.Name='Test';
o.StageName = 'F6-Prospecting'; 
o.CloseDate = Date.valueOf('2011-07-01'); 
o.MRC__c = 100.00; 
o.NRC__c = 100.00; 
o.Required_Proposal_Date__c = Date.valueOf('2011-07-01');
        

insert o;


ApexPages.StandardController sc = new ApexPages.StandardController(o);
//THIS LINE THROWS AN ERR
ApexPages.currentpage().getparameters().put('id' , o.id);
VFController objCls = new VFController(sc);

objCls.autoRun();



}

  Add one more for better coverage

 

static testmethod void testVFController2() {
 
 
Opportunity o = new Opportunity();
o.Name='Test';
o.StageName = 'F6-Prospecting'; 
o.CloseDate = Date.valueOf('2011-07-01'); 
o.MRC__c = 100.00; 
o.NRC__c = 100.00; 
o.Required_Proposal_Date__c = Date.valueOf('2011-07-01');
        

insert o;


ApexPages.StandardController sc = new ApexPages.StandardController(o);
//THIS LINE THROWS AN ERR

VFController objCls = new VFController(sc);

objCls.autoRun();



}

 

This was selected as the best answer