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

Test class for this apex class
Hi All,
i am new to testclasses i dont know how to write test classes exactly...but i am tried but i am geting 80% code coverage only..but i need 100% can any one help this please??
public with sharing class accounts_based_on_months
{
public String selected_month { get; set; }
public list<account>acc;
public list<account>accounts;
public PageReference getrecords()
{
string dat='';
accounts=new list<account>();
//acc.clear();
acc=new list<account>();
acc=[select id,Createddate,name,phone from account];
for(account a:acc)
{
dat=a.Createddate.format('MMMM');
if(selected_month ==dat)
{
accounts.add(a);
}
}
return null;
}
public list<account> getAccounts()
{
return accounts;
}
}
thanks,
bhagi
Right. The account isn't being added because selected_month hasn't been set. Try this:
All Answers
This isn't a full test because you need to insert some accounts before calling the methods. Even then it would just be a smoke test but it should get you started.
Thnx for your replay colemab
i am also writing same thing just create instance of class and calling methods of my class for that i am getting 80 % code coverage,,
but i dont know how to write class logic in my test class
can you write my class logic for test class please
Here is a better test class. Just understand that if you have any custom validation rules, you may need to modify the test data to meet them or you will get an exception while running the test.
when i compiled with test.starttest(),test.stoptest() it throws errors....when i remove these methods after the test class is saved and giving code coverage 92%...colemab
but its not giving 100%..what shall i do??
Those statements aren't strictly required so drop them if you want.
When you say you don't get 100% coverage, is that the total coverage? Or is that just the coverage on the class? They are different.
It is most likely that you are getting 100% coverage on the class but not in the system - i.e. you have other code out side of this class in the system that this class doesn't provide test coverage for and you averaging out to 92%.
100% coverage is not required to deploy to production - only 75% is required.
If you aren't getting 100% coverage on the class:
Scroll down to the class in the list and click on the text of the percentage (i.e. where it says 92%) and after a couple second delay, it should open a new window that will show which lines got code coverage (they will be highlighted in red). Please reply back and paste the line(s) that don't have coverage.
it shows the red line below statement...i think accounts are not added to list..
accounts.add(a);
Right. The account isn't being added because selected_month hasn't been set. Try this:
Hi Colemab
i came across a situation where i need to create pageblocktable many times so i want to create a component which will create pageblock table dynamically i have this idea how to resolve it...
My Approch is
Here i am retrieving the object id from the url in visualforce page through object id i have the code to retrieve the object name and fields of that object dynamically...and also i retrieving the records through dynamic soql.....But i stuck How to Display These Records in PageBlockTable......
help me out in this scenario colemab...
visualforce page:
// here we get the object from url dynamically through objid....
Component:
Please start a new thread for your page block issue. This will help keep the current thread and the new thread on topic for those that are searching.