function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
LosintikfosLosintikfos 

Apex Coverage?

hi Experts,

Do anyone knows how to write a test method for the code below?

Code:
public class addParent{

 public static void addAddress(Account[] acc)
   {
    /**Set Set variables to hold the result as class members**/
    String parent;
    String pCode;
  
            parent = acc[0].ParentId;
           
         for (Account ac : [select Billing PostalCode where Id = :parent]) {
               pCode = ac.BillingPostalCode;
              }
              acc[0].BillingPostalCode = pCode;
          }
     }

 Really need your help :smileywink: with some psuedo to kick this running!
    
mikefmikef
Question: is this class being called from an Account Trigger?

If so then the test is easy.

Code:
static void testMethod testInsert(){
Account parent = new Account();
//populate your account fields here

insert parent;
System.assertNotEquals(null,acc.id); //checks to see if the account was inserted correctly

Account acc = new Account();
acc.ParentId = parent.id; // make sure you do this one.
//populate your account fields hereacc.ParentId = parent.id;
insert acc;
System.assertNotEquals(null,acc.id); //checks to see if the account was inserted correctly

//query for the parent
//test make sure you business logic works
}

Also your code doesn't support bulk inserts of updates of accounts. Your code will not error on bulk operations,
but you will skip processing the other accounts in your Trigger.
This line, acc[0].BillingPostalCode = pCode; and the other line where you use acc[0],
means to me that you are preforming all your actions on the first account in the array.
But what is there are 200 accounts in your array. Only the first one will get updated.

Check out this post, http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=342#M342
LosintikfosLosintikfos
mikef,

I am using an apex class and  invoking class with a trigger anytime a new account is created. So you right! i am alway targeting the newly created account, not doing bulk.

Following your code, i am doing something like this;
Code:
static void testMethod testInsert(){
Account acc = new Account();
acc.ParentId = parent.id; //populate your account
String pCode = acc.BillingPostalCode;

insert acc;
System.assertNotEquals(null,acc.id); //checks to see if the account was inserted correctly

//query for the parent
//test make sure you business logic works

Am a bit confuse here! do i need the same query?
Also, do i use the System.debug() method for testing the logic?
}

help mikef:smileywink:




Message Edited by Losintikfos on 09-22-2008 10:00 AM
mikefmikef
First read the section on testMethods in the Apex docs. This will get you off on the
right foot.

To answer your question quickly you would query and use the
System.assertEquals, and System.assertNotEquals to do your tests.

Reading the docs will really help and give you a good example of how to write tests.
Just search on 'Testing and Code Coverage'.

Message Edited by mikef on 09-23-2008 08:30 AM
LosintikfosLosintikfos
Hi Experts,

I am trying to do something like this for the coverage of the class defined above, but i woun't invoke the instance of the addAddress() method. I end up with;

Save error: Method does not exist or incorrect signature: [addParent].addAddress(SOBJECT:Account)   

Upon further investigation i realised its' got something to do with my trigger class;


Do you anyone knows how to invoke the trigger too to avoid spitting this trace! Please help

Code:
trigger addAddressTrigger on Account
                            (Before insert, Before update) {
  addParent.addAddress(Trigger.new);
}

 



public class testAddParentAddress {
    
    public static testMethod void testAddParent(){
    List<Account> accl = new Account[]{new Account()};
    accl[0] = new Account(Name = 'test', ParentId = '001R000000955CDIAY', BillingState = 'MASS', BillingCity = 'Tazmania',    BillingCountry = 'AUS', BillingPostalCode = 'MJ 25355', BillingStreet ='St. Kilda');

    insert accl;
    addParent ad = new addParent();
    ad.addAddress(accl);
            
     System.debug(ad);
    }

}




Message Edited by Losintikfos on 09-23-2008 04:47 AM
LosintikfosLosintikfos
Need your help experts!
LosintikfosLosintikfos
Please Help
mikefmikef
It shows that you are not reading the docs on this one.

After you insert your account you don't need to invoke your class.
The error you are getting is because you are trying to instantiate an object from a class,
then you are using that classes static method. You can't do that.

After the insert call in your test method use the System.assertEquals methods to test your
business logic.
LosintikfosLosintikfos
Sorry this is sorted! i should have informed you ages ago. Ta!