Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can lambdas be used to test your code?  RSS feed

 
Teresa Sparkman
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a java-based test framework and use testNG. How can lambdas be used to simplify code testing?
 
Campbell Ritchie
Marshal
Posts: 55707
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know myself, but welcome to the Ranch
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is one chapter about testing lambda expressions. If I remember it right, it's chapter 7.
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Warburton
Author
Greenhorn
Posts: 22
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!