Java Testing with Spock: A good (and sometimes Groovy) guide to using this powerful testing framework – #programming #java

Java Testing with Spock

Konstantinos Kapelonis

Manning, paperback

 

Spock, the author states, is “a comprehensive testing framework for Java (and Groovy) code that can help you automate the boring, repetitive, and manual process of testing a software application. Spock is comprehensive because it’s a union of existing testing libraries”—specifically JUnit, Mockito and JBehave. It also is influenced by several others.

What is Spock’s main advantage in test scenarios? “When things go wrong,” Konstantinos Kapelonis notes, “Spock gives as much detail as possible on the inner workings of the code at the time of the failure.”

Spock is written in Groovy, and just mentioning that language, as well as the Gradle build tool, may give a little heartburn to hardcore Java developers who don’t want to learn them. But others find Groovy refreshingly efficient and Gradle easy to use. In any case, using Groovy (and Gradle) with this book is “optional,” the author emphasizes. As noted in Appendix A, “It’s perfectly possible to use Spock in your Java project without installing Groovy itself.”

To emphasize that point, Kapelonis shows how to use Spock with the Maven build tool first, before he delves into how to use Spock with the Gradle build tool.

The book is divided into three major parts: (1) Foundations and brief tour of Spock; (2) Structuring Spock tests; and (3) Spock in the Enterprise.

Two appendices deal with installing and using Spock, plus getting your IDE set up, and using the book’s example files.

Java Testing with Spock is a comprehensive guide to learning how to do Java (and Groovy) testing with Spock, and it is generally well written and adequately illustrated.

I chose to try the Groovy-Gradle approach, with Eclipse as my IDE. And I did run into some awkward moments trying to get Eclipse Mars.2 to play correctly. The Groovy-Gradle plug-in from the Eclipse Marketplace was for earlier versions of Eclipse, and so was the Spock plug-in. After some tinkering and reconfiguring, I was able to get things working together and do some Java and Groovy tests. To be fair, I was doing this on a kludged-together Windows 10 machine that definitely is no development powerhouse. And I did not have time to try out the Maven approach, but I have used Maven in the past, and the author’s instructions and examples for Maven look solid.

Java Testing with Spock is a good, helpful how-to book for anyone who wants to know more about putting the Spock testing framework to good use at all levels of Java development.

Si Dunn

Advertisements

‘Spring Boot in Action’ can help you push aside the old drudgeries of configuring Spring applications – #programming #bookreview

Spring in Action

Craig Walls

Manningpaperback

If you have worked with the decade-old Spring framework, you know well that it has a long history of providing configuration headaches for developers. The new Spring Boot framework, on the other hand, literally brings much-needed simplification and automation to the process of using Spring. And it can put some refreshing fun back into application development.

“Spring Boot,” Craig Walls states in his new book, “is an exciting new way to develop Spring applications with minimal friction from the framework itself. Auto-configuration eliminates much of the boilerplate configuration that infests traditional Spring applications. Spring Boot starters enable you to specify build dependencies by what they offer rather than use explicit library names and version. The Spring Boot CLI takes Spring Boot’s frictionless development model to a whole new level by enabling quick and easy development with Groovy from the command line. And the [Spring Boot] Actuator lets you look inside your running application to see what and how Spring Boot has done.”

You do not need a lot of Spring experience to benefit from this book. You do need some Java background, and it is helpful to have used Groovy, Gradle and Maven a few times. But the book’s text is written smoothly, and it is well illustrated, with numerous code examples and a few screen shoots. So Java developers who are fairly new likely can use it and pick up new skills.

While going through the book, you develop a reading-list application using Spring Initializr, Spring Boot, Spring Tool Suite, and other tools. In the project, you “use Spring MVC to handle web requests, Thymeleaf to define web views, and Spring Data JPA to persist the reading selections to a database,” Craig Walls explains. Initially, at least, “an embedded H2 database” is employed during development.

Walls’s book is divided into eight chapters:

1. Bootstarting Spring
2. Developing your first Spring Boot Application
3. Customizing configuration
4. Testing with Spring Boot
5. Getting Groovy with the Spring Boot CLI
6. Applying Grails in Spring Boot
7. Taking a peek inside the Actuator
8. Deploying Spring Boot applications

Four appendices also are presented: Spring Boot developer tools, Spring Boot starters, Configuration properties, and Spring Boot dependencies.

Bottom line: with Spring Boot providing much of the heavy lifting, you likely will gain better feelings about the venerable Spring framework. You may even wind up with a healthy new respect for it. And Spring Boot certainly should add more years to Spring’s usefulness and viability in the marketplace.

Si Dunn

Attack of the Killer Parentheses: ‘Clojure in Action, 2nd Edition’ – #bookreview

Clojure in Action, 2nd Edition

Amit Rathore and Francis Avila

Manning – paperback

Clojure seems to be afflicted with a measles-like outbreak of parentheses, and it generally just looks strange to many software developers. And there’s a good reason for that, as this book’s two author point out in their recently released second edition.

“Clojure’s syntax is derived from its Lisp roots: lots of parentheses. It’s alien to most developers with experience in languages with Algol-inspired syntax like C, C++, Java, Python, Ruby, Perl, and so on.”

But Clojure also is an intriguing and powerful choice for many software development projects, Amit Rathore and Francis Avila insist. Clojure is “a functional Lisp on the JVM” (the Java Virtual Machine), and: “It is impossible to separate the Lisp, functional programming, and JVM features of Clojure. At every step they play on each other and tell a compelling software development story….”

I have been tinkering with Clojure on the side, at random spare moments, for more than two years, using a disorganized approach of looking at web postings, building and modifying simple projects that others have posted, and sometimes looking at Clojure how-to books as time permits.

From my perspective, Clojure in Action, 2nd Edition fills a beginner’s need for a friendly, well-organized approach to learning the language and putting it to work effectively. Developers already working with Clojure can benefit from having this book, too, as a handy reference. It covers a lot of ground, using reasonably short paragraphs and offers many short code examples to illustrate its key points.

Clojure in Action, 2nd Edition “assumes no prior experience with Lisp or with any functional programming language,” the authors emphasize. “It starts out with the absolute basics and slowly layers on the different features of the language in  a way to make it all fit together in an intuitive way. It takes a first-principles approach to all the topics, first explaining why something needs to be done a certain way, and only then talking about the Clojure way.”

Clojure is not a language for absolute beginners. The authors assume “you’re familiar with an OO [object-oriented] language like Java, C++, Ruby, or Python, but no background in Java, Lisp, or Clojure is required.” They also assume you have downloaded Clojure and gotten it working on your PC. You can read more about Clojure and download it here.

This expanded 2nd edition states that it covers the “new” features of Clojure 1.6. Of course, Clojure already is up to 1.8, but I have tried many of the code examples at various points in the book and have not encountered problems while running 1.8.

Si Dunn

Groovy in Action, Second Edition – A hefty how-to guide newly updated for Groovy 2.4 – #programming #bookreview

Groovy in Action, Second Edition

Dierik König and Paul King, with Guillaume Laforge, Hamlet D’Arcy,
Cédric Champeau, Eric Pragt and Jon Skeet

Manning – paperback

Groovy in Action, Second Edition, is not light reading. Indeed, the printed book weighs nearly three and a half pounds and has 880 pages. But it is great reading for anyone who wants to learn, or get better at, the increasingly popular Groovy scripting language that works very smoothly with Java. Indeed, Java’s creator, James Gosling, has hailed Groovy’s “smooth and efficient” integration with Java and called Groovy “an effective implementation language in its own right.” He also has praised the Groovy in Action book as “a clear and detailed exposition of what is groovy about Groovy.”

The Second Edition‘s two main authors and five assisting authors are members of the Groovy core team. And their book spent a lot of time being reviewed and tested by readers in the Manning Early Access Program (MEAP) before it was formally released. So it likely has a better preparation record than many programming books currently on the market.

Groovy in Action‘s front flap indicates that the book covers Groovy 2.4. Groovy recently was up to version 2.4.3, but the programming language has maintained a good track record for supporting backward compatibility. Indeed, I tested random selections of the book’s code samples using version 2.2.0 and its Groovy Console, and programs compiled and ran without problem.

However, if you own the first edition of Groovy in Action, you likely will want to upgrade to the new book. It is, the authors state, “a full rewrite,” with several new chapters, plus  “a few hundred additional pages of genuinely new content.” (And yes, I am upgrading my Groovy installation from 2.2.0 to 2.4.3.)

Despite its heft, the book is nicely structured and easily approached. And its many code examples are mercifully compact, for the most part, and available online, if you prefer. (I actually enjoy keying reasonably short code examples into the Groovy Console.)

The 20 chapters are organized into three major parts:

  • The Groovy Language
  • Around the Groovy Library
  • Applied Groovy

“The Groovy Language” introduces the reader to the language’s basics: its “syntax, grammar, and typical idioms,” plus how to use dynamically typed Groovy as a static language, if desired. The “Around the Groovy Library” reference section focuses on such topics as working with builders and the Groovy Development Kit (GDK), as well as Groovy’s support for database programming and the handling of JSON and XML. And “Applied Groovy” looks at “typical uses cases for Groovy,” including “a thorough exposition of how to use Groovy for test automation,” how to put Groovy to work on multi-core machines in concurrent programming situations, and “using Groovy for domain specific languages.”

In short, there is no shortage of useful content in Groovy in Action, Second Edition.

Si Dunn

——————–
Get Groovy in Action, Second Edition here, at no extra cost.
——————–

Java 8 in Action – Ready for lambdas, streams and functional-style programming? #bookreview

 

Java 8 in Action

Lambdas, streams, and functional-style programming

Raoul-Gabriel Urma, Mario Fusco, Alan Mycroft

(Manning – paperback)

 Java 8, we were sometimes assured, would just be Java 7 with a few slick new (or past-due) features added.

Actually, now that it’s here, Java 8 represents “the biggest change to Java in the 18 years since Java 1.0 was released,” the three authors of this fine new book point out.

Of course, news of “big changes” seldom sits well with developers who have spent countless hours learning and getting comfortable with one particular version of a programming language.

And many coders and companies will continue sticking with Java 7 for a while longer, because it still works. But the adoption pace for Java 8 keeps picking up. So, to misquote an old sci-fi slogan, resistance soon will become somewhat futile.

Lambdas, streams, and functional-style programming capabilities are Java 8’s headline additions. And there are some other major and minor additions, as well, including default methods and a new Date and Time API.

Java 8 in Action does an excellent job of introducing these new capabilities, and the book offers many short code examples and other illustrations to show how to put the new Java 8 capabilities to work.

Indeed, short (and shorter!) code is one of the hallmarks of Java 8. “In Java 8 you can write more concise code that reads a lot closer to the problem statement,” the writers emphasize. To illustrate that point, they offer a five-line example of verbose Java 7 code and follow it with a one-line Java 8 code example that accomplishes the same thing. Other examples also drive home the coding efficiencies that Java 8 can offer.

Lambdas, also known as anonymous functions, enable you to skip writing method definitions that will only be used once. The authors note that “passing code is currently tedious and verbose in Java [meaning 7 and earlier]. Well, good news! Lambdas fix this problem: they let you pass code in a concise way. Lambdas technically don’t let you do anything you couldn’t do prior to Java 8. But you no longer have to write clumsy code using anonymous classes to benefit from behavior parameterization!”

The new Streams API makes it much easier to work with collections in Java and provides “a much different way to process data in comparison to the Collections API.” Using the Streams API, “you don’t need to think in terms of loops at all. The data processing happens internally inside the library.”

Meanwhile, if you are a diehard object-oriented programmer, you may be leery of the term “functional programming” and the notion of using functions as values. (“In practice, you can’t completely program in pure functional style in Java,” the authors note. Instead, you will learn how to write “functional-style programs” in which you hide the side effects.)

With Java 8, “two core ideas from functional programming…are now part of Java: using methods and lambdas as first-class values, and the idea that calls to functions or methods can be efficiently and safely executed in parallel in the absence of mutable shared state. Both of these ideas are exploited by the new Streams API,” the writers state.  Also, in Java 8, they add, “there’s an Optional class that, if used consistently can help you avoid NullPointer exceptions.”

This review barely dents the surface of this excellent how-to book’s contents. Whether you are learning Java now or you are a Java developer who wants to keep your coding skills up-to-date and sharp, Java 8 in Action should be a book you will read soon.

Si Dunn

Scala for Java Developers – A practical guide to building apps and integrating code – #programming #bookreview

Scala for Java Developers

Build reactive, scalable applications and integrate Java code with the power of Scala

Thomas Alexandre

(Packtpaperback, Kindle)

 

The increasingly popular Scala programming language runs on the Java Virtual Machine (JVM). And “Java and Scala stacks can be freely mixed for totally seamless integration,” Scala’s website proudly trumpets.

The Scala site goes on to note: “Scala is a modern multi-paradigm programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages.”

Indeed, in Scala, “every value is an object” and “every function is a value.”

Scala’s continuing  rise poses something of a dilemma for many Java developers. Their questions include:

  • Should I learn Scala and blend it with Java?
  • Should I take up Scala and use it instead of  Java?
  • Should I just keep my head down, ignore Scala, and focus on getting better at developing with Java?

Thomas Alexandre’s well-written new book, Scala for Java Developers, clearly is aimed at those who would much rather add Scala to their skills and resumes than attempt to hide from the changes Scala can offer to Java development.

He notes in his preface: “When I tell people around me that I now program in Scala rather than Java, I often get the question, ‘So, in simple words, what is the main advantage of using Scala compared to Java?’  I tend to respond with this: ‘With Scala, you reason and program closer to the domain, closer to plain English.’ “

However, Alexandre’s new how-to guide does not try to browbeat developers into abandoning Java and pledging allegiance to Scala alone. Instead, Alexandre shows how the two programming languages can be used together. And he demonstrates how Scala solutions often can be shorter and less complex than their Java equivalents.

He focuses on “exploring progressively some of the new concepts brought by Scala, in particular, how to unify the best of Object-Oriented and functional programming without giving away any of the established and mature technologies built around Java for the past fifteen years.”

Decisions regarding how far to go with Scala remain with the reader. Yet Alexandre and his numerous code examples make a compelling case for becoming at least reasonably familiar with the language and experiencing how easily it can integrate with Java.

And, yes, he does evangelize a bit; this is, after all, a book about Scala. “Scala is the only language that has it all,” Alexandre touts. “It is statically typed, runs on the JVM and is totally Java compatible, is both object-oriented and functional, and is
not verbose, thereby leading to better productivity, less maintenance, and therefore more fun.”

His book is divided into 10 chapters:

  • Chapter 1: Programming Interactively within Your Project – Includes advantages of using Scala, learning Scala via the REPL, and performing operations on collections.
  • Chapter 2: Code Integration – Focuses on creating a REST API from an existing database, adding a test in Scala, setting up Scala within a Java Maven project, and showing how Scala and Java work together despite some differences in code style.
  • Chapter 3: Understanding the Scala Ecosystem – Includes inheriting Java Integrated Development Environments (IDEs), building with Simple Build Tool (SBT), using Scala worksheets, working with HTTP, and using Typesafe Activator.
  • Chapter 4: Testing Tools – Writing tests with ScalaTest and testing with ScalaCheck.
  • Chapter 5: Getting Started with the Play Framework – Getting started with the classic Play distribution, getting started with the Typesafe Activator, the architecture of a Play application, authentication, and practical tips when using Play.
  • Chapter 6: Database Access and the Future of ORM – In this case, ORM is Object Relational Modeling. Chapter topics include integrating an existing ORM – Hibernate and JPA, dealing with persistence in the Play framework, replacing ORM, learning about Slick, and scaffolding a Play application.
  • Chapter 7: Working with Integration and Web Services – Includes binding XML data in Scala, working with XML and JSON, and handling Play requests with XML and JSON.
  • Chapter 8: Essential Properties of Modern Applications – Asynchrony and Concurrency – Covers the pillars of concurrency, the async library – SIP-22-Async, and getting started with Akka.
  • Chapter 9: Building Reactive Web Applications – Includes describing reactive applications, handling streams reactively, experimenting with WebSockets and Iteratees in Play, learning from activator templates, and playing with Actor Room.
  • Chapter 10: Scala Goodies – Covers exploring MongoDb and scratching the surface of Big Data. Also introduces DSLs in Scala and introduces Scala.js, which compiles Scala to JavaScript.

Alexandre makes a strong pitch for using the Play framework in Scala web development. And he again speaks out for Scala at the conclusion of his well-structured book:

“The concise and expressive syntax of the Scala language should make your code not only more readable but also more maintainable for yourself and other developers,” he writes. “You don’t have to give up any of the libraries of the very large and mature Java ecosystem as all the APIs can be reused directly within Scala. Moreover, you benefit from many additional great Scala-specific libraries. Our recommendation is to take a piece of Java code from a domain you understand well, maybe because you wrote it in the first place one or several times before. Then, try to convert it to Scala code and refactor it to get rid of the boilerplate and to make it in a more functional style.”

Si Dunn

 

 

 

 

 

 

 

 

Mule in Action, 2nd Edition – Want to be an integration developer? Here’s a good start – #bookreview

 

Mule in Action, Second Edition

David Dossot, John D’Emic, Victor Romero

(Manning – paperback)

 

An enterprise service bus (ESB) can help you link together many different types of platforms and applications–old and new–and keep them communicating and passing data between each other.

“Mule,” this book’s authors note, “is a lightweight, event-driven enterprise service bus and an integration platform and broker.  As such, it resembles more a rich and diverse toolbox than a shrink-wrapped application.”

Mule in Action, Second Edition, is a comprehensive and generally well-written overview of Mule 3 and how to put its open-source building blocks together to create integration solutions and develop them with Mule. The book provides very good focus on sending, receiving, routing, and transforming data, key aspects of an ESB.

More attention, however, could have been paid to clarity and detail in Chapter 1, the all-important chapter that helps Mule newcomers get started and enthused.

This second edition is a recent update of the 2009 first edition. Unfortunately, the Mule screens have changed a bit since the book’s screen shots were created for the new edition. Therefore, some of the how-to instructions and screen images do not match what the user now sees. This gets particularly confusing while trying to learn how to configure a JMS outbound endpoint for the first time, using Mule Studio’s graphical editor. The instructions seem insufficient, and the mismatch of screens can leave a beginner unsure how to proceed.

The same goes for configuring the message setting in the Logger element. The text instructs: “You’ll set the message attribute to print a String followed by the payload of the message, using the Mule Expression Language.” But no example is given. Fortunately, a reviewer on Amazon has posted a correct procedure. In his view, the message attribute should be: We received a message: #[message.payload]  –without any quote marks around it. (It works.)

Of course, this book is not really aimed at beginners–it’s for developers, architects, and managers (even though there will be Mule “beginners” in those ranks). Fortunately, it soon moves away from relying solely on Mule Studio’s graphical editor. The book’s examples, as the authors note, “mostly focus on the XML configurations of flows.” Thus, there are many XML code examples to work with, plus occasional screen shots of the flows as they appear in Mule Studio. And you can use other IDEs to work with the XML, if you prefer.

Indeed, the authors note, “no functionality in the CE version of Mule is dependent on Mule Studio.”

Overall, this is a very good book, and it definitely covers a lot of ground, from “discovering” Mule to becoming a Mule developer of integration applications, and using certain tools (such as business process management systems) to augment the applications you develop. I just wish a little more how-to clarity had been delivered in Chapter 1.

Si Dunn