2

I'm on the Advanced Apex Specialist Superbadge step 8 and I'm writing the test class, but what they are asking for seems different from what I've been taught about test methods.

I thought that I should have one test method for each apex method I'm testing. They ask for one test method to cover the whole extension class and it's methods.

Should I just call helper/separate test methods from within one test method?

Do I test each method separately or just call one method at a time then test results?

Is this all completely different for Apex Page Extensions and StandardSetControllers? For example page next, first page etc.

Thank you

3

They ask for one test method to cover the whole extension class and it's methods.

When you're asked to do something specific, go ahead and do it, even if it's not your usual thing. As developers, we are likely in the real world to be told to do something a certain way, even if we don't like it. And we do it, because we like to keep our paychecks flowing.

Should I just call helper/separate test methods from within one test method?

For purposes of this test, yes, that's perfectly acceptable. In fact, any time you write a method anywhere (test or otherwise) that gets to more than about 50 lines of code, you should consider breaking up the method into smaller methods. It'll make your code more readable and manageable.

Do I test each method separately or just call one method at a time then test results?

In the interests of "failing fast", I always recommend that you execute one method, test the results, then do the next method. This way, if your test is going to fail validation (the assertions), it will do so as soon as possible in most cases. This design also typically lets you minimize heap usage, which may be important for algorithms that use a lot of memory.

Is this all completely different for Apex Page Extensions and StandardSetControllers? For example page next, first page etc.

Again, generally yes. Testing one piece at a time will allow the tests to fail faster if they're going to fail. Imagine you have a ten page wizard, but there's a logic error on page 2. If you call all the methods first, you've called ten methods and, let's say, 2 assertions, only to fail. If you validate as soon as possible, you'd have tested just two methods before failing on the second assertion. I don't know about you, but I'd rather fail in half a second than in 5 seconds. All that time spend eventually adds up.


Finally, writing one test per method versus writing one test for everything: they both have practical uses. The former is typically called a "unit test", while the latter is called an "end-to-end test." In the real world, it's helpful to have both. Use "unit tests" (those that test a single method) to verify that a method works. Use "end-to-end tests" to make sure that the entire process works, and to make sure that the entire chain of code being called doesn't exceed governor limits. Both types of tests are useful, so it's unlikely that, in the real world, you'd want to stick to only using one or the other.

  • Thank for clearing that up. For end-to-end testing, can I, for example, test that going to second page loads different records, then within same test, call previous button method and test that it returns to original page? If this is the case, is this enough to test the next and previous methods, not requiring me to unit test them? – rna7 Jun 22 '18 at 14:28
  • 1
    @rna7 Depends what I can do within the governor limits, really. If I could do everything all at once, I'd probably make it an end-to-end test, but if not, I'd probably lump all the navigation methods into a single unit test, making sure everything loads correctly, then one unit test per page for all of the methods one would invoke. Or maybe break it up even further. Unit Testing is more of an art form than a science; the most important thing is that you cover as many gaps as you can to try and avoid logic errors. – sfdcfox Jun 22 '18 at 14:50
  • This may need another post, but would writing tests for standardsetcontrollers its own test methods? – rna7 Jun 22 '18 at 15:54
  • @rna7 I'm afraid I don't understand the question. You don't write unit tests for built in library code. But if you were testing something that used standardsetcontroller, then it'd probably be with whatever you are testing. You wouldn't generally write a separate test for this. – sfdcfox Jun 22 '18 at 16:36

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.