You need to sign in to do that
Don't have an account?
beatit
Test case % coverage Problem
public class PlotWithrelatedpaymentsController { //Testing from IDE //added an instance varaible for the standard controller private ApexPages.StandardController controller {get; set;} // add the instance for the variables being passed by id on the url private plot__c po {get;set;} private plot__c newpo {get;set;} // set the id of the record that is created -- ONLY USED BY THE TEST CLASS public ID newRecordId {get;set;} // initialize the controller public PlotWithrelatedpaymentsController(ApexPages.StandardController controller) { //initialize the stanrdard controller this.controller = controller; // load the current record po = (plot__c)controller.getRecord(); //newpo = (plot__c)controller.getRecord(); } // method called from the VF's action attribute to clone the Payments public PageReference cloneWithItems() { System.debug('Clone method'); System.debug('old id'+po.id); //RETRIEVE the Plot - ONLY INCLUDE THE FIELDS YOU WANT TO RETRIEVE po = [select id ,Venture__c,plot_status__c ,PLOT__c ,Plot__r.name, CUSTOMER_del__c from Plot__c where id = :po.id]; System.debug('old Customer name'+po.CUSTOMER_del__c); System.debug('new Plot Name'+po.PLOT__c); // set the id of the new po created for testing(u can remove comments subhash) // newRecordId = po.plot__c; //Kishore code newpo = [select id ,Venture__c,plot_status__c ,PLOT__c, CUSTOMER_del__c from Plot__c where id = :po.PLOT__c]; System.debug('new Customer Name'+newpo.CUSTOMER_del__c); //if( newpo.plot_status__c == 'Sold'){ //return new PageReference('/'+po.PLOT__c ); //} newpo.plot_status__c = 'Sold'; newpo.CUSTOMER_del__c = po.CUSTOMER_del__c; update newpo; //end//////////////// // copy over the Payments - ONLY INCLUDE THE FIELDS YOU WANT TO COPY List<payment__c> items = new List<payment__c>(); for (payment__c pi : [Select p.Id, p.name,p.Payment_Date__c,customer__c,p.Receipt_Number__c,p.Payment_Amount__c,p.Purpose_of_Payment__c,p.Plot__c From payment__c p where plot__c = :po.id]) { if(po.customer_del__c == pi.customer__c){ payment__c newPI = pi.clone(false); newPI.plot__c = po.PLOT__c ; newPI.customer__c = pi.customer__c; newPI.Payment_Date__c =newpi.Payment_Date__c; newPI.Receipt_Number__c= newpi.Receipt_Number__c; newPI.customer__c=po.CUSTOMER_del__c; items.add(newPI); delete pi; } } insert items; po.CUSTOMER_del__c = null; po.plot_status__c = 'vacant'; po.PLOT__c = null; update po; return new PageReference('/'+newpo.id+'/e?retURL=%2F'+newpo.id); } }
the Above one is my class for cloning payments for plots...the below is test case coverage is <60 % please help me to improve th coverage
@isTest private class PlotWithrelatedpaymentsControllersuite { static testMethod void validatePOController() { // setup a reference to the page the controller is expecting with the parameters //PageReference pref = Page.transfer; // Test.setCurrentPage(pref); // create new po record customer__c cs =[select id ,name from customer__c LIMIT 1]; Venture__c v = [Select Name,id From Venture__c LIMIT 1]; plot__c po = new plot__c(); po.CUSTOMER_del__c = cs.id ; po.venture__c = v.id ; po.plot_status__c = 'none'; po.Mode_of_Sale__c = 'installment'; insert po; // create a line item for the po // Payment__c pi1 = [select id ,name ,Payment_Date__c,Receipt_Number__c from payment__c LIMIT 2]; payment__c pi1 = new payment__c(); pi1.Purpose_of_Payment__c = 'Installment'; pi1.plot__c = po.id; pi1.Payment_Amount__c = 10; pi1.Customer__c = po.customer_del__c; pi1.Payment_Date__c = Date.newInstance(2020,01,01); pi1.Receipt_Number__c ='0000000000'; insert pi1; // Construct the standard controller ApexPages.StandardController con = new ApexPages.StandardController(po); // Switch to test context Test.startTest(); // check that the new po was created successfully plot__c newPO = [select id from plot__c where id = :po.id]; System.assertNotEquals(newPO, null); // check that the line item was created List<payment__c> newItems = [Select p.Id From payment__c p where plot__c = :newPO.id]; System.assertEquals(newItems.size(),1); List<payment__c> newItems1 = [Select p.customer__c From payment__c p where plot__c = :newPO.id]; System.assertEquals(newItems.size(),1); List<payment__c> newItems2 = [Select p.plot__c From payment__c p where plot__c = :newPO.id]; System.assertEquals(newItems.size(),1); //venture,plot is inserted or not Venture__c ve = [Select Name,id From Venture__c LIMIT 1]; Plot__c p = new Plot__C(name =ve.id ,Plot_Status__c = 'vacant',VENTURE__c=ve.id); ve.name = p.VENTURE__c; insert p; System.assertEquals(p.name, p.VENTURE__c); // Switch back to runtime context Test.stopTest(); } }
Any suggestions please
Apply the following changes to your code:
@isTest
private class PlotWithrelatedpaymentsControllersuite {
static testMethod void validatePOController() {
// setup a reference to the page the controller is expecting with the parameters
//PageReference pref = Page.transfer;
// Test.setCurrentPage(pref);
// create new po record
customer__c cs =[select id ,name from customer__c LIMIT 1];
Venture__c v = [Select Name,id From Venture__c LIMIT 1];
plot__c po = new plot__c();
po.CUSTOMER_del__c = cs.id ;
po.venture__c = v.id ;
po.plot_status__c = 'none';
po.Mode_of_Sale__c = 'installment';
insert po;
// create a line item for the po
// Payment__c pi1 = [select id ,name ,Payment_Date__c,Receipt_Number__c from payment__c LIMIT 2];
payment__c pi1 = new payment__c();
pi1.Purpose_of_Payment__c = 'Installment';
pi1.plot__c = po.id;
pi1.Payment_Amount__c = 10;
pi1.Customer__c = po.customer_del__c;
pi1.Payment_Date__c = Date.newInstance(2020,01,01);
pi1.Receipt_Number__c ='0000000000';
insert pi1;
// Construct the standard controller
ApexPages.StandardController con = new ApexPages.StandardController(po);
PlotWithrelatedpaymentsController ctrl = new PlotWithrelatedpaymentsController(con);
ctrl.cloneWithItems();
// Switch to test context
Test.startTest();
// check that the new po was created successfully
plot__c newPO = [select id from plot__c where id = :po.id];
System.assertNotEquals(newPO, null);
// check that the line item was created
List<payment__c> newItems = [Select p.Id From payment__c p where plot__c = :newPO.id];
System.assertEquals(newItems.size(),1);
List<payment__c> newItems1 = [Select p.customer__c From payment__c p where plot__c = :newPO.id];
System.assertEquals(newItems.size(),1);
List<payment__c> newItems2 = [Select p.plot__c From payment__c p where plot__c = :newPO.id];
System.assertEquals(newItems.size(),1);
//venture,plot is inserted or not
Venture__c ve = [Select Name,id From Venture__c LIMIT 1];
Plot__c p = new Plot__C(name =ve.id ,Plot_Status__c = 'vacant',VENTURE__c=ve.id);
ve.name = p.VENTURE__c;
insert p;
System.assertEquals(p.name, p.VENTURE__c);
// Switch back to runtime context
PageReference pageref = new PageReference('/'+newpo.id+'/e?retURL=%2F'+newpo.id'); //sample URL
pageref.setRedirect(true);
return pageref;
Test.stopTest();
}
}
If you still cannot improve the code coverage, try to perform the same operations that you did in the cloneWithItems method.
Hope this helps you..........