• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unit testing

 
baskar krishnan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unit testing has been a real challenge ever especially when someone needs to work a lot around legacy code, mocking, not-to-hit-database etc. Many times I had to reinvent the wheel to write test programs while working in those areas. Is there any new idea introduced in Java 8 ?

Thanks.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
410
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read Working Effectively with Legacy Code? The book is a decade old, but still up to date. That's something nice about legacy code - it isn't cutting edge.
 
Tim Cooke
Sheriff
Posts: 3294
153
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
+1 for Jeanne's book recommendation.

Since reading it I actually quite enjoy working with Legacy code now. The prospect of finding a tangled mess of code used to fill me with dread, with the first though in my head being "I'm not touching THAT!". But now I get to have a good laugh at the ridiculousness of it and relish in the challenge of taking it on. Then bask in the glory once it's all been nice and safely refactored.

Turns out this is a large part of what it means to be a half decent Software Engineer.
 
Raoul Gabriel
Author
Greenhorn
Posts: 20
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Baskar,

Lambdas can enable you to do testing in a more declarative BDD approach. See this link for an example: http://www.blog.project13.pl/index.php/coding/1830/proof-of-concept-lambdaspec-how-testing-will-look-with-java8/

 
Rafał Borowiec
Greenhorn
Posts: 2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some cases for unit testing with Java 8:

1. One can use lambda expressions in test code to implement lightweight stubs. if the collaborator to be stubbed is a functional interface. I found a neat description of this approach in "Java 8 Lambdas" book (http://www.amazon.com/Java-Lambdas-Pragmatic-Functional-Programming/dp/1449370772/ref=sr_1_1?s=books&ie=UTF8&qid=1404769897)

2. Lambda Expressions can help in testing exceptions in JUnit and therefore can easily replace libraries like catch-exception or even plain JUnit @Rule.

Having simple functional interface like this:



We can achieve a syntax like below:



Please have a look at blog post that describes this approach in more detail: http://blog.codeleak.pl/2014/07/junit-testing-exception-with-java-8-and-lambda-expressions.html
 
Campbell Ritchie
Marshal
Posts: 52574
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Is that your own blog?
 
Rafał Borowiec
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. But the book I did not write.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!