Find your content:

Search form

You are here

How to write test for following apex code

 
Share

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){
   this.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]);
 var1.add(var);
 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;
 var.setType(str);
 Integer cnt = 0;
 for (Account acc : accounts) {
  if (acc.DD_Segment__c != null && acc.DD_Segment__c.contains(str)) {
    cnt = cnt+1;
 }
}
var.setTotal(cnt);
var1.add(var);
}
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.

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods http://wiki.developerforce.com/page/How_to_Write_Good_Unit_Tests http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#CSHID=apex_testing.htm|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.

http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods

TDD. You may also want to checkout...

http://en.wikipedia.org/wiki/Test-driven_development

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 https://salesforce.stackexchange.com/questions/4687

My Block Status

My Block Content