You need to sign in to do that
Don't have an account?
Zoom-Zoom
Test-Drive Development (TDD) with apex?
I was wondering if anybody has any experience with doing Test-Drive Development (TDD) with apex? I've started experimenting on my own but was curious to know if people have developed some TDD practices specific to Salesforce.
For instance, TDD's point is to write the tests before writing any code, but Salesforce statically typed language doesn't allow to write some code that calls a class or method that doesn't exist yet. Should one write a class stub just to be able to save the test code? Something else?
Apex Code is no exception to this model. All code has to resolve to valid constructs before you can save a single line of code anywhere. This forces a lock-step process that developers must adhere to in order to successfully run tests. You can, however, delete methods later, but your test won't run at all, making this venture pointless.
Given the nature of Apex Code, I would imagine that a TDD-oriented approach to Apex Code would involve writing stubs, writing tests, then completing the stubs until the tests no longer fail. I typically write the test methods and code in tandem, when possible, as a sort of double-check. It's usually more difficult to write the same logic wrong twice, since writing test methods forces the developer to think about the implementation.
Thank you for your feedback.
Personally, I've found that the best way to motivate myself to write tests is to tie them to the user requirements - and to let users see what the tests do.
This is why I'm trying to see if I can come with an apex version of Cucumber (an automated testing tool for Ruby) that can be somehow usable.
I've thought about this, too. Unfortunately, I don't think I have the time to actually do this. I'd be glad to provide some feedback if you have any ideas on how you might do it. We need better testing, and I think it'd be a great project.
I've actually started writing my own Cucumber implementation called Pickle. It's still a draft of course, but can already handle a scenario like:
I'm OK about handling the development part. However, there are a couple of ways you can help:
Also, do you happen to know if there is a way to dynamically read the fields of an Object? I know how to do that with an SObject, but not an Object. A System.debug() on an Object however displays its fields, so the information is somewhere.
A) I'd probably prefer to use this either in a browser, an IDE, or even a CLI project. As a pure Apex Code function, it'd be far too limited, given the lack of proper reflection in Apex Code. The concept is definitely sound though.
Q) Do you know of anybody else who could be interested? If so, could you ask them to check the tool out?
A) Not directly, but I'll probably post something about it on my various social networks and direct them here.
Q) One of the things I'm working on is a class which serves as repository of scenarios, so they can be shown to end users through a VF page. The challenge is however to verify the scenarios are actually part of some tests and not added just for show. My current approach involves parsing the test classes code (ApexClass SObject), but I am open to suggestions for a better approach.
A) Parsing the Apex Code would be the best way to accomplish this, I think. For performance reasons, you'll probably still need to move this logic to something client-side (e.g. JavaScript), or you'll run into script limits for large classes.
Q) Also, do you happen to know if there is a way to dynamically read the fields of an Object? I know how to do that with an SObject, but not an Object. A System.debug() on an Object however displays its fields, so the information is somewhere.
A) Inside Apex Code, the only reliable method would be to JSON.serialize, then JSON.deserializeuntyped into a map; you can then read the keys/values directly. Outside of Apex Code, you'd have a much easier time of it.
Thank you for your feedback. Using a scenario in an IDE is an interesting idea. But that would mean running it "live", outside of Salesforce tests, wouldn't it?
I thought about coding it as a pure Apex solution so that 1) it can be used in Salesforce test methods (and benefit from the fact the transaction doesn't get committed) and 2) one needs anyway to provide some apex code to bind to one's methods and VF actions.
Ah, interesting. I hadn't thought of generating test code. You lose the dynamic aspect of a script (if you change the script you need regenerate everything and manually add some of your custom code) but you wouldn't have to jump through hoops to determine what functions to call in the code.