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

Help on test class
Hello,
I am trying to write a test class for the follwing Apex class controller but getting only 61% code coverage. Can some one please help to get the maximum?
Thanks,
Dinesh
Apex class:
Public Class AddItemsOnMarktActivities { Public List<Marketing_Activities__c> MAToInsert { get; set; } Public Integer numberOfRowToRemove { get; set; } Public Online_events__c opp { get; set; } Public Integer num { get; set; } Public AddItemsOnMarktActivities(ApexPages.StandardController stdController) { MAToInsert = new List<Marketing_Activities__c>(); this.opp = (Online_events__c)stdcontroller.getRecord(); } // The method to add a new item(s) to the list Public PageReference addNewMAItem(){ /******* NOT GETTING TEST COVERAGE FOR THIS SECTION ******************/ for(Integer i = 0; i < num ; i++) { Marketing_Activities__c MAnewItems = new Marketing_Activities__c(Online_Events__c = opp.id); MAToInsert.add(MAnewItems); } return null; } // The method to remove an item(s) from the list Public PageReference removeNewMA(){ /******* NOT GETTING TEST COVERAGE FOR THIS SECTION ******************/ MAToInsert.remove(numberOfRowToRemove); return null; } // The method to save an item(s) to the sObject. public PageReference onSave() { try { if(MAToInsert.size()>0) insert(MAToInsert); } catch(Exception e) { /******* NOT GETTING TEST COVERAGE FOR THIS SECTION ******************/ ApexPages.addMessages(e); return null; } return new PageReference('/' + ApexPages.currentPage().getParameters().get('Id')); } }
Test class so far:
@isTest private class AddItemsMarktActivitiesTest { static testMethod void myUnitAddItemsMarktActivitiesTest() { Online_Events__c oe = new Online_Events__c(); oe.Name = 'Add Items MA'; oe.Event_Title__c = 'Add Items MA Title'; oe.Event_Date__c = Date.today(); oe.Event_type__c = 'Test'; insert (oe); List<Marketing_Activities__c> marc = new List<Marketing_Activities__c>{}; for(Integer i = 0; i < 2 ; i++) { Marketing_Activities__c marcItems = new Marketing_Activities__c(Online_Events__c = oe.id); marc.add(marcItems); } insert (marc); PageReference MAref = new PageReference('/apex/NewMarketingActivities?Id=' + oe.Id); Test.setCurrentPage(MAref); Apexpages.Standardcontroller controllers = new Apexpages.Standardcontroller(oe); AddItemsOnMarktActivities addItemsMar = new AddItemsOnMarktActivities(controllers); ApexPages.currentPage().getParameters().put('id',+oe.Id); addItemsMar.addNewMAItem(); addItemsMar.onSave(); } }
Try below code:
All Answers
Hi,
Try to use following Test class. i have made two changes in it.
Test Class:
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/
Thanks Hitesh for the help.
But I have got the failures message for the follwing line from your solution.
Errror: System.NullPointerException: Attempt to de-reference a null object
Thanks again.
Try to use following..
It does solve the error but now
onSave() doesn't get any test code coverage. Any reason?
Thanks
Try below code: