You need to sign in to do that
Don't have an account?
Unable to write test Methods.
Hi,
I want to write test method for the following class. I have tried a lot but I didn't able to do it. Code Coverage remains 30% only.
I am not be able to write test methods for save,search and AdvanceSeach methods. Please help.
Class :
public class EditSubmodulesController {
private ApexPages.StandardController controller {get; set;}
public List<Submodule__c> searchResults {get;set;}
public string searchText {get;set;}
// standard controller - could also just use custom controller
public EditSubmodulesController(ApexPages.StandardController controller) { }
public EditSubmodulesController()
{
}
Public String message = System.currentPagereference().getParameters().get('msg');
// fired when the search button is clicked
public void search() {
String qry = 'select Module__r.Name__c,Hours__c,Name__c,Level__c,Link__c,Source_Type__c,module__c from Submodule__c where Name =:message';
searchResults = Database.query(qry);
}
public void Advancedsearch() {
String qry = 'select Module__r.Name__c,Hours__c,Name__c,Level__c,Link__c,Source_Type__c,module__c from Submodule__c where Name =:message where Name LIKE \'%'+searchText+'%\' or Name__c LIKE \'%'+searchText+'%\'';
searchResults = Database.query(qry);
}
// fired when the save records button is clicked
public PageReference save() {
try {
upsert searchResults;
} Catch (DMLException e) {
ApexPages.addMessages(e);
return null;
}
return new PageReference('/apex/CRUDSubmodule');
// return new PageReference('/'+ApexPages.currentPage().getParameters().get('id'));
}
// takes user back to main record
public PageReference cancel() {
return new PageReference('/apex/CRUDSubmodule');
}
static testMethod void verifyIsEditSubmodules ()
{
String nextPage='/apex/CRUDSubmodule';
PageReference pageRef = Page.EditSubmodules;
Test.setCurrentPage(pageRef);
EditSubmodulesController controller = new EditSubmodulesController();
nextPage = controller.cancel().getUrl();
System.debug('nextPage is'+nextPage);
System.assertEquals('/apex/CRUDSubmodule',nextPage);
}
}
Thank you
Amol Dixit.
You are missing couple of things -
1. The parameter 'msg' in your VF page (you are retreiving in your class Public String message = System.currentPagereference().getParameters().get('msg');), while declaring the page suplly this as a parameter.
2. Org data is not available to test classes from Spring'12 onwards. So better use separate class.
Try removing SeeAllData, and see the results.
All Answers
You are missing couple of things -
1. The parameter 'msg' in your VF page (you are retreiving in your class Public String message = System.currentPagereference().getParameters().get('msg');), while declaring the page suplly this as a parameter.
2. Org data is not available to test classes from Spring'12 onwards. So better use separate class.
Try removing SeeAllData, and see the results.
Thank you so much for your guidance. It works properly. I wrote,
static testMethod void verifyIsEditSubmodules ()
{
Submodule__c sub=new Submodule__c();
sub.Name__c='Competency';
sub.Hours__c=Double.valueof('3.5');
sub.Level__c='s';
sub.Link__c='as';
sub.Source_Type__c='asd';
insert sub;
PageReference pageRef = Page.EditSubmodules;
pageRef.getParameters().put('msg',sub.Name);
Test.setCurrentPage(pageRef);
EditSubmodulesController esc = new EditSubmodulesController ();
esc.AdvancedSearch();
esc.save();
esc.search();
}
But the thing is that Although I have done it, the code coverage is 80%. WHy it's not 100%. Is there anything remained to test? Please help....
Thank you so much.
Thats great.
Click on the percentage (here 80%), you will see the lines covered and not covered. Whichever lines are not covered check the reason- some may be because there is no exception while upsert searchResults;