Find your content:

Search form

You are here

How to write test for following apex code


How to write test for following part of apex code? I am fairly new to sfdc development.

public with sharing class ddDashboard {

 public class phonesterDat{

  String type;
  Integer total;
  Integer valid;

  public String getType() {
   return type;

  public void setType(String type){
   this.type= type;

  public Integer getTotal() {
   return total;
  public void setTotal(Integer total){ total;

  public Integer getValid() {
   return valid;
  public void setValid(Integer valid){
   this.valid= valid;

 public List<phonesterDat> getEmailData() {
 List<phonesterDat> var1 = new List<phonesterDat>();
 phonesterDat var = new phonesterDat();
 var.setType('Contact Email');
 var.setTotal([select count() from Contact where Email != null]);
 var.setValid([select count() from Contact where Email_Valid__c = true]);
 return var1;

public List<phonesterDat> getCompanyData() {
List<phonesterDat> var1 = new List<phonesterDat>();
List<Account> accounts = [select DD_Segment__c from Account];
List<Keyword__c> keywords = [select kw__c from Keyword__c limit 50000];
for (Keyword__c keyword : keywords) {
 phonesterDat var = new phonesterDat();
 String str = keyword.kw__c;
 Integer cnt = 0;
 for (Account acc : accounts) {
  if (acc.DD_Segment__c != null && acc.DD_Segment__c.contains(str)) {
    cnt = cnt+1;
return var1;


Attribution to: Bhushan Lodha

Possible Suggestion/Solution #1

So, How to write test code ?

In short, you'll want to start your test code with generating or loading test data, which will be diverse enough to test all the business logic in your code. Once that is done you can use the class like you would use it outside test context, and try to cover all business rules/ conditional code, and assert all output or class behavoir is as you expect it to be.

Your class is rather basic, and should not form a real challenge to cover by test code as far as I can see.

The documentation contains a lot of info on how to write test code, dive in there and try out some of it.|StartTopic=Content%2Fapex_testing.htm|SkinName=webhelp

Attribution to: Samuel De Rycke

Possible Suggestion/Solution #2

In short.

You need to add methods to your class or a new class that have the @IsTest annotation on them and then ensure that the code in those methods executes the methods on the code you want to test. Making sure to add test code that confirms that the code has and more importantly continues to do what you expected at the time you wrote the test. This is done via assertions you place in the test code. The more of these you write the better and more protected from regressions your code will be in the future.

In full.

TDD. You may also want to checkout...

I always try to do this and recommend this approach when possible. To do this you need to decide what your tests are acting against. Is that SOBject's or Controllers or some specific Class? Then if needed, skeleton / stub implementations (fields, methods etc) of those in place and get writing your first test. Because you have the skeleton in place, you test will compile, but will not run, this is good! The next step is to write enough code to make the test pass! You then repeat as you go.

Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content