3

So up until now every time I make an apex class, I make a separate class that tests that class (usually the same name with the suffix "TestClass"), which is what I've seen a lot of people do. However I am now aware that the same thing can be accomplished by annotating test functions inside the main class, instead of having a whole other class (which takes up space in the apex class list).

My question is whether there is a recommended way of doing this or is it literally just user preference? Personally I would prefer inline test methods as it just seems cleaner but are there any repercussions or implications that I should be aware of before changing all my code?

  • 1
    I thought test methods had to be in a separate class now since summer 13. Assuming your class is using API 28 or above that is. – Girbot Oct 2 '14 at 11:47
  • Hmm, so it would appear :/ I thought otherwise considering some code generated by SF had inline tests in. – Adam Oct 2 '14 at 11:49
  • Maybe if it's using an older API – Girbot Oct 2 '14 at 11:52
  • 1
    Also, I think that test methods in not-test classes count against your apex size, but class marked @isTest don't. – Dominic Oct 2 '14 at 12:58
  • 2
    You used to have to do this to access private properties/methods when testing, but as part of API 28.0 the @TestVisible annotation means this is no longer necessary. – Bob Buzzard Oct 2 '14 at 15:10
7

For the sake of completeness I'll add my comment as an answer.

From Summer 13/API 28 and above test methods have to be in their own test class, using the @isTest annotation. As Dominic says this also has the benefit of not being included in the orgs apex character limit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.