You need to sign in to do that
Don't have an account?

stuck on testMethod
Hi Everyone,
I need little help here with 2 apex classes I need to test and deploy to production.
I have attempted this by reading developer guide and got no where. I guess I still have a lot to learn and just got through learning about visualforce and controllers.
Here is my code and any help would be appreciated thanks in advance.
public class TradeAccountController { // Set class variable public Account nitch; //set List variables Trade_Calender__c[] recList; //STANDARD CONTROLLER SECTION public TradeAccountController(Account item){ this.nitch = item; } public String getParam(String name) { return ApexPages.currentPage().getParameters().get(name); } public TradeAccountController(ApexPages.StandardController stdController) { this.nitch = (Account)stdController.getRecord(); recList = [ Select id, Year__c From Trade_Calender__c WHERE Account__c = :nitch.id order by Year__c desc]; } // returns Record information public Trade_Calender__c[] getRecList() { return recList; } // Deletes selected row public PageReference del() { try { String delid = getParam('delid'); Trade_Calender__c event = [SELECT Id FROM Trade_Calender__c WHERE ID=:delid]; DELETE event; } catch (Exception e) { ApexPages.addMessages(e); } recList = [ Select id, Year__c From Trade_Calender__c WHERE Account__c = :nitch.id ]; getRecList(); return null; } }
public class TradeCalendarController { // Set class variable public Trade_Calender__c nitch; public Trade_Events__c trade; //set List variables Trade_Events__c[] recList; Trade_Events__c[] recDelete; //STANDARD CONTROLLER SECTION public TradeCalendarController(Trade_Calender__c item){ this.nitch = item; } public String getParam(String name) { return ApexPages.currentPage().getParameters().get(name); } public TradeCalendarController(ApexPages.StandardController stdController) { this.nitch = (Trade_Calender__c)stdController.getRecord(); recList = [ Select Trade_Calender__c, Name, Account__c, Year__c, Category__c, Vendor__c, Period__c, Pack__c, Promo_Price__c, Merch_Type__c, More_than_1_event_in_period__c, Event_start_in_prior_period__c, Events_del__c From Trade_Events__c WHERE Trade_Calender__c = :nitch.id order by Pack__c, period__c]; if(recList.size()==0){ newRow(); } } // returns Record information public Trade_Events__c[] getRecList() { return recList; } // Inserts all changes into records public pagereference saveChanges() { update nitch; try { upsert recList; } catch (DMLException e){ ApexPages.addMessages(e); return null; } // after save refreshes data on page recList = [ Select Trade_Calender__c, Name, Account__c, Year__c, Category__c, Vendor__c, Period__c, Pack__c, Promo_Price__c, Merch_Type__c, More_than_1_event_in_period__c, Event_start_in_prior_period__c, Events_del__c From Trade_Events__c WHERE Trade_Calender__c = :nitch.id order by Pack__c, period__c]; getRecList(); return null; } // Inserts a new row public pagereference newRow() { Trade_Events__c d = new Trade_Events__c(); for(Trade_Calender__c trade:[Select id, name, account__c, Year__c from Trade_Calender__c where id = :nitch.id]){ d.Account__c = trade.account__c; d.Name = trade.name; d.Year__c = trade.Year__c; d.Trade_Calender__c = nitch.id; recList.add(d); } return null; } // clones a row public PageReference cl() { String delid = getParam('delid'); Trade_Events__c d = new Trade_Events__c(); for(Trade_Events__c trade:[Select id, Pack__c, Vendor__c, Merch_Type__c, Promo_Price__c, Category__c, name, account__c, Year__c from Trade_Events__c where Trade_Calender__c=:nitch.id and ID=:delid]){ d.Account__c = trade.account__c; d.Name = trade.name; d.Year__c = trade.Year__c; d.Trade_Calender__c = nitch.id; d.Pack__c = trade.Pack__c; d.Merch_Type__c = trade.Merch_Type__c; d.Promo_Price__c = trade.Promo_Price__c; d.Promo_Price__c = trade.Promo_Price__c; d.Category__c = trade.Category__c; d.Vendor__c = trade.Vendor__c; recList.add(d); } return null; } // delete row public PageReference del() { try { String delid = getParam('delid'); Trade_Events__c event = [SELECT Id FROM Trade_Events__c WHERE ID=:delid]; DELETE event; } catch (Exception e) { ApexPages.addMessages(e); } // after del refreshes the data on page recList = [ Select Trade_Calender__c, Name, Account__c, Year__c, Category__c, Vendor__c, Period__c, Pack__c, Promo_Price__c, Merch_Type__c, More_than_1_event_in_period__c, Event_start_in_prior_period__c, Events_del__c From Trade_Events__c WHERE Trade_Calender__c = :nitch.id order by Pack__c, period__c]; getRecList(); return null; } }
In the test method create an instance for class
and call each and every method. You need to insert records of objects(Which you have used in class) to cover methods.
Write Like this
eg:
public static testmethod void test_eg(){
Account acc=new Account(Name='Radhika');
insert acc;
ApexPages.currentPage().getParameters().put('name',acc.name);//your parameter
ApexPages.StandardController sc = new ApexPages.StandardController(acc);//acc is account instance this is account standard controller
TradeAccountController t=new TradeAccountController(sc);
//call methods
t.del();
}
All Answers
In the test method create an instance for class
and call each and every method. You need to insert records of objects(Which you have used in class) to cover methods.
Write Like this
eg:
public static testmethod void test_eg(){
Account acc=new Account(Name='Radhika');
insert acc;
ApexPages.currentPage().getParameters().put('name',acc.name);//your parameter
ApexPages.StandardController sc = new ApexPages.StandardController(acc);//acc is account instance this is account standard controller
TradeAccountController t=new TradeAccountController(sc);
//call methods
t.del();
}
Thank you very much I now understand how test methods work and was able to deploy my code to production.
Does the same concept work for triggers as well?