+ Start a Discussion
MikeGillMikeGill 

Only hitting 37% test coverage

Only hitting 37% test coverage.

 

Stumped as System.assert confirms that the order status is updated.

 

Can anyone spot the issue?

 

Page:

 

 

<apex:page standardController="Order__c" extensions="UpdateOrderStatus" action="{!save}">

        <apex:sectionHeader title="Auto-Running Apex Code"/>
          <apex:outputPanel >
              You tried calling Apex Code from a button.  If you see this page, something went wrong.  You should have
              been redirected back to the record you clicked the button from.
          </apex:outputPanel>


</apex:page>
		

 

Class:

 

public class UpdateOrderStatus {
	
	private final Order__c order;
	
	public UpdateOrderStatus(ApexPages.StandardController createContract){
		
		this.order = (Order__c)createContract.getRecord();
	
	}
	
	public PageReference save(){
		
		
		Order__c orderChecks = [select Accepted_Terms__c,Payment_Terms_Agreed__c from Order__c where Id=:order.Id ];
		
		if ( orderChecks.Accepted_Terms__c == true && orderChecks.Payment_Terms_Agreed__c == true){
			autoRun();
			PageReference ordPage =  new ApexPages.StandardController(order).view();
      		ordPage.setRedirect(true);
      		return ordPage;
		}
		
		else {
			PageReference ordPage =  new ApexPages.StandardController(order).view();
      		ordPage.setRedirect(true);
      		return ordPage;
		}
	
	}
	

	public void autoRun(){

		order.Order_Status__c = 'Closed - On Contract';
		update order;
	
	}
	

}

 

 

Test Class:

 

@isTest
private class testControllers {

	
	static PageReference pref;
	static UpdateOrderStatus ext;
	static Order__c orderObject;
	static OrderItems__c orderItemsObject;
	
	static {
		
		Account acc = new Account(name = 'XXX', type = 'Client');	
		insert acc;
		
				
		orderObject = new Order__c();
		orderObject.Account__c = acc.Id;
		orderObject.Order_Status__c = 'Open - Awaiting Contract';
		orderObject.Accepted_Terms__c = true;
		orderObject.Payment_Terms_Agreed__c = true;
		insert orderObject;
		
		orderItemsObject = new OrderItems__c();
		orderItemsObject.Order__c = orderObject.Id;
		orderItemsObject.Quantity__c = 1;
		orderItemsObject.Product_Type__c = 'Single';
		insert orderItemsObject;
		

	} 
	


	static testMethod void testOrderController(){
		
	test.startTest();
	ApexPages.StandardController con = new ApexPages.StandardController(orderObject);
	ext = new UpdateOrderStatus(con);
	
	if ( orderObject.Accepted_Terms__c == true && orderObject.Payment_Terms_Agreed__c == true){
			con.save();
			ext.autoRun();
			pref = con.view();
    		pref.setRedirect(true);
	}
	
	else{
			pref = con.view();
    		pref.setRedirect(true);
	}
	

    
    System.assertEquals(orderObject.Order_Status__c, 'Closed - On Contract');
    
    test.stopTest();  
	}
	
	
	
	static testMethod void testRefundController(){
		test.startTest();
		ApexPages.StandardController refundController = new ApexPages.StandardController( new Refund__c() );
		PreloadRefund preloadRefund = new PreloadRefund( refundController );
    	test.stopTest();  
	}
	
	static testMethod void testQuickContactController(){
		test.startTest();
		ApexPages.StandardController quickContactController = new ApexPages.StandardController( new Contact() );
		QuickContact quickContact = new QuickContact( quickContactController );
		test.stopTest();  
		}
}

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jeremyyjeremyy

Yes.. in your test code you're instantiating 2 different controllers:

 

 

ApexPages.StandardController con = new ApexPages.StandardController(orderObject);
ext = new UpdateOrderStatus(con);

Then you exercise the system under test:

 

if ( orderObject.Accepted_Terms__c == true && orderObject.Payment_Terms_Agreed__c == true){
			con.save();
			ext.autoRun();
			pref = con.view();
    		pref.setRedirect(true);
	}

The problem above is that you're exercising the save() method in the standard controller, NOT the save() method in your controller extension. If I'm correct, the test coverage report should make this pretty clear.

 

 

 

All Answers

jeremyyjeremyy

When you run tests, the test report will show you which lines are not covered. Have you looked at this?

MikeGillMikeGill

Yep have looked - but cannot see where the issue is, as the test class appears to cover it all.

 

Can you see anything wrong?

jeremyyjeremyy

Yes.. in your test code you're instantiating 2 different controllers:

 

 

ApexPages.StandardController con = new ApexPages.StandardController(orderObject);
ext = new UpdateOrderStatus(con);

Then you exercise the system under test:

 

if ( orderObject.Accepted_Terms__c == true && orderObject.Payment_Terms_Agreed__c == true){
			con.save();
			ext.autoRun();
			pref = con.view();
    		pref.setRedirect(true);
	}

The problem above is that you're exercising the save() method in the standard controller, NOT the save() method in your controller extension. If I'm correct, the test coverage report should make this pretty clear.

 

 

 

This was selected as the best answer
Imran MohammedImran Mohammed

In the testMethod, did you add this code and checked

        PageReference pg = Page.VF_Page_Name;

        Test.setCurrentPage(pg);

 

Are you getting any Test Failed errors and can you check if any trigger is getting fired due to an update that is leading the coverage to 37%.
        

 

For ex:

 

	static testMethod void testOrderController(){
	PageReference pg = Page.VF_Page_Name;																Test.setCurrentPage(pg);
	test.startTest();
	ApexPages.StandardController con = new ApexPages.StandardController(orderObject);
	ext = new UpdateOrderStatus(con);
	
	if ( orderObject.Accepted_Terms__c == true && orderObject.Payment_Terms_Agreed__c == true){
			con.save();
			ext.autoRun();
			pref = con.view();
    		pref.setRedirect(true);
	}
	
	else{
			pref = con.view();
    		pref.setRedirect(true);
	}
	

    
    System.assertEquals(orderObject.Order_Status__c, 'Closed - On Contract');
    
    test.stopTest();  
	}
	

 

MikeGillMikeGill

Thanks Jeremy - that solved my issue

MikeGillMikeGill

Cheers Imran

 

Added that too - I'm learning