Keyboard Shortcuts
Likes
Search
[TDD] Unit tests and parameter validation
If this is internal code only and the code calling it is also testing then I not only would I not test it but I'd remove all defensive code guards as well. ?For example, a typical pattern would be: public void foo(SomeObject bar) { ? ?if (bar == null) { ? ?..... ? ?} } Instead of catching the null and handling it, ensure all code calling it will never pass in null. ?Cuts down on both test and production code.
On Tue, Oct 15, 2013 at 5:03 PM, Eb <amaeze@...> wrote:
--
Maybe she awoke to see the roommate's boyfriend swinging from the chandelier wearing a boar's head. Something which you, I, and everyone else would call "Tuesday", of course. |
Thanks for your responses. Colin - what if another developer would be using it (and not the originally creator). ?How does he/she know that passing in null (for example) is wrong?
On Tue, Oct 15, 2013 at 12:52 PM, Colin Vipurs <zodiaczx6@...> wrote:
blog:
twitter: @eikonne
|
Donaldson, John
¿ªÔÆÌåÓýEb, ? I agree with Colin in principle, but wider than that I¡¯d say, following Uncle Bob¡¯s rule of ¡°no code without a failing test¡± pretty well ensures you will have tests which supply incorrect inputs, if you want to handle those cases. ? John D. ? From: testdrivendevelopment@... [mailto:testdrivendevelopment@...]
On Behalf Of Colin Vipurs
Sent: 15 October 2013 18:52 To: testdrivendevelopment@... Subject: Re: [TDD] Unit tests and parameter validation ?
If this is internal code only and the code calling it is also testing then I not only would I not test it but I'd remove all defensive code guards as well. ?For example, a typical pattern would be: ? public void foo(SomeObject bar) { ? ?if (bar == null) { ? ?..... ? ?} } ? Instead of catching the null and handling it, ensure all code calling it will never pass in null. ?Cuts down on both test and production code. ? On Tue, Oct 15, 2013 at 5:03 PM, Eb <amaeze@...> wrote: ? Hello everyone - ? I'm curious to know thoughts on writing tests that specify the behavior of a method when its inputs are incorrect. ?Is this a common practice. ?How about parameter validation in this case? ?Having some healthy debate on it and wanted to get other insight. ? The context for this in a class/methods that are used within an internal application - this is not an API that will be publicly exposed to external consumers. ? Thanks. ? -- blog: twitter: @eikonne
? --
|
Donald - If a test passes in the wrong input and fails initially, I see 3 general ways of getting the test to pass:
Or is the assumption that my code should never call the method with a bad parameter, hence I shouldn't have a test that tests with bad parameter? Thanks. On Tue, Oct 15, 2013 at 1:25 PM, Donaldson, John <john.m.donaldson@...> wrote:
--
blog:
twitter: @eikonne
|
John Goodsen
Think in terms of the contract between the caller and the code. ?The code guarantees valid results, ONLY IF the caller guarantees they'll pass in valid parameters. ?So if passing a null is not part of that contract, you should not have to write tests and code to check if a null was passed. ?Can you imagine if you started down the path of littering all your code with "if (foo == null) throw new Exception("Foo cannot be null") at the beginning of each method? ?Don't do that.
In the case of validating that the value pass passes some business validation rules, that might be an object waiting to be born, where you could check in a constructor that somebody is creating a bad object (e.g. the class "invariant") .. and I'd fail right there and then - you shouldn't have code later on that checks validity of values - don't let them be created in the first place.
On Tue, Oct 15, 2013 at 10:41 AM, Eb <amaeze@...> wrote:
|
Thanks John. Well I don't have to imagine it, I've seen which is what led to the question. :) Interesting perspective on contracts and the requirement to pass "correct" inputs (avoiding the use of the word valid here) to get correct results. ?How does a consumer know what is correct however?
More to ponder.... On Tue, Oct 15, 2013 at 2:25 PM, John Goodsen <jgoodsen@...> wrote:
blog:
twitter: @eikonne
|
John Goodsen
I tend to program in a more functional style these days, so you can write much more concise, if-less code ... if you want your data in a collection to be "valid", for example, you flatMap over it and morph the data into a temporary shape that you need (e.g. add some fields, reject values meet some criteria, etc... ... which means I pretty much never have code that validates data anymore and I rarely have if statements in the code at all, because once the data looks like the code wants it to look like, you just let the code do it's thing- sometimes I'll throw in a check temporarily to debug some tests or when we're getting an API right, but then I'll remove those data checks once we're confident they aren't needed anymore... |
On 10/15/13 11:42 AM, Eb wrote:
Interesting perspective on contracts and the requirement to passA consumer must be told the specific semantics to expect. One way is to read the code. In the absence of code you must rely on documentation. Executable documentation in the form of code examples that also serve as tests for validation is just one way to communicate the semantics. -- Edwin G. Castro |
Hi Edwin -
So what's your opinion on what to actually do? ?What would the tests look like? Thanks. Ebenezer On Tue, Oct 15, 2013 at 3:09 PM, Edwin Castro <egcastr@...> wrote:
--
blog:
twitter: @eikonne
|
Eb, Hope this make sense On Oct 15, 2013 8:41 PM, "Eb" <amaeze@...> wrote:
|
On 10/15/13 12:42 PM, Eb wrote:
On Tue, Oct 15, 2013 at 3:09 PM, Edwin Castro wrote:What is appropriate for your team depends on your culture and the requirements around how semantics are communicated between developers in your team, developers in other teams consuming your work, QA, etc. There are no fast rules, only principles and guidelines. Executable documentation in the form of tests is preferable to traditional API references or developer guides because tests have a tendency to break when the documentation is out of date. An acceptance test that validates that an exception is thrown and that no side effects have occurred provides a way to explicitly document the semantics of the method. The specific exception validated by the test is perhaps not as important from the test's perspective but we do want to provide valuable information to the developers consuming our work so they can fix their error. An acceptance test that validates no side effects occur will alert us when we accidentally break our documented semantics in a way that could negatively impact consumers. As others have mentioned, the client code should be responsible for not passing you a null value. Your code is likely also client code for other code so you'll need to make sure you are not accidentally passing nulls to other code. This is an important quality to validate in unit tests. -- Edwin G. Castro |
Lior - Sure. Eb On Tue, Oct 15, 2013 at 4:16 PM, Lior Friedman <lfriedmal@...> wrote:
blog:
twitter: @eikonne
|