Forums Register Login

How can lambdas be used to test your code?

+Pie Number of slices to send: Send
We have a java-based test framework and use testNG. How can lambdas be used to simplify code testing?
1
+Pie Number of slices to send: Send
Don't know myself, but welcome to the Ranch
1
+Pie Number of slices to send: Send
There is one chapter about testing lambda expressions. If I remember it right, it's chapter 7.
+Pie Number of slices to send: Send
Teresa,
Welcome to CodeRanch!

I think you are asking about how lambdas can simplify your test code. (rather than how to test lambas).

I think most tests will be too simple to benefit. But tests with more complex assertions can be simplified. For example, I often write a helper assertion method that looks like:



Yes, I could use a comparator, but then I'd be writing it just for this method. With lambas, I could express this whole method in one line.
+Pie Number of slices to send: Send
 

Teresa Sparkman wrote:We have a java-based test framework and use testNG. How can lambdas be used to simplify code testing?



Hi Teresa,

I think the biggest benefit isn't their direct usage in test code, although there is a good example elsewhere in this thread of that happening, its actually in making it easier to abstract over different behaviours. Being able to pass in some behaviour as a parameter makes it a lot easier to write code which is testable. In the same way that depending on any abstraction makes it easier to write code that's testable.

regards,

Richard
Squanch that. And squanch this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 822 times.
Similar Threads
Lambda
Java SE 8 for The Really Impatient: Lambda Expressions
Java SE 8 for the Really Impatient: Lambdas and more ?
Unit testing
Java 8 in Action: Debugging Java 8 code
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 18:55:46.