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

Agile Metrics in Action: A good how-to guide to getting better performance measurements – #programming #bookreview

Agile Metrics in Action

Christopher W. H. Davis

Manningpaperback

In the rapidly changing world of software development, metrics “represent the data you can get from your application lifecycle as it applies to the performance of software development teams,” Christopher W. H. Davis writes in his well-written, well-structured new book, Agile Metrics in Action.

“A metric can come from a single data source or it can be a combination of data from multiple data sources. Any data point that you track eventually becomes a metric that you can use to measure your team’s performance.”

The goals of agile metrics include collecting and analyzing data from almost every useful and accessible point in the software development life cycle, so team and individual performances can be measured and improved, and processes can be streamlined.

A key aspect of the data collection and analysis process is distributing the resulting information “across the organization in such a way that everyone can get the data they care about at a glance,” Davis says. He explains how and highlights some “traps” that teams can “fall into when they start publishing metrics,” such as “[s]ending all the data to all stakeholders,” many of whom won’t know what to do with most of it.

Metrics remain a controversial topic for many software developers, Davis emphasizes. So any business leader planning to rush his or her company into adopting agile metrics will need to proceed cautiously, instead. It is vital to get buy-in first from developers and their managers, he says.

“There will likely be people in your group who want nothing to do with measuring their work,” he explains. “Usually this stems from the fear of the unknown, fear of Big Brother, or a lack of control. The whole point here is that teams should measure themselves, not have some external person or system tell them what’s good and bad. And who doesn’t want to get better? No one is perfect—we all have a lot to learn and we can always improve.”

The concept of continuous development is a key topic in this book. “In today’s digital world consumers expect the software they interact with every day to continuously improve,” Davis states. “Mobile devices and web interfaces are ubiquitous and are evolving so rapidly that the average consumer of data expects interfaces to continually be updated and improved. To be able to provide your consumers the most competitive products, the development world has adapted by designing deployment systems that continuously integrate, test, and deploy changes. When used to their full potential, continuous practices allow development teams to hone their consumer’s experience multiple times per day.”

Of course, continuous development produces continuous data to measure and manage, as well, using agile metrics techniques.

Many different topics are addressed effectively in this book. And the practices the author presents are organized to work with any development process or tool stack. However, the software tools Davis favors for this book’s code-based examples include Grails, Groovy and MongoDB.

Agile Metrics in Action is structured and written to serve as a how-to book for virtually anyone associated with a software development team that relies on agile metrics. You may not understand all of the text. But if you take your time with this well-illustrated book, you can at least gain a better comprehension of what agile metrics means, how the process works, and why it is important to your employer, your group and your paycheck.

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.
——————–

Grails in Action, 2nd Edition – A (mostly) winning how-to guide to use with a winning web app framework – #programming #bookreview

 

Grails in Action, Second Edition

Glen Smith and Peter Ledbrook

(Manning – paperback)

Grails finishes at or very near the top in almost any smackdown of full-stack web application frameworks that run on the Java Virtual Machine (JVM). And this recently updated Grails in Action how-to book is mostly a clear winner, too.

According to the Grails.org website, open-source Grails “takes advantage of the Groovy programming language and convention over configuration to provide a productive and streamlined development experience.”

Grails likewise is a world that “moves very quickly,” the authors of Grails in Action, Second Edition emphasize. “There have been substantial changes in Grails in the time it took us to develop Grails in Action, Second Edition. Even moving from Grails 2.2 to 2.3 caused us to make significant changes! Although the book targets Grails 2.3, a new version of Grails (2.4) is already available. Fortunately, everything in here is still valid for the new version.”

In the first chapter, the authors try to move very quickly through the process of getting a Grails application up, running, tested and deployed. But in taking this “Grails in a hurry” approach, they race a bit too quickly and unclearly through the installation instructions, in my opinion. (My Linux and Windows installations did not work correctly at first, and I had to seek out  information on how to sort them out.)

And, in the portion of the chapter where you are told how to get the random-quote database set up and working, it is not always clear which file you are supposed to modify and in which subdirectory. I already had a little bit of experience with Groovy, so that portion went smoothly. But the Grails database steps could have been explained and illustrated more clearly. It took me several tries to finally get the “Quote of the Day” database working and posting random quotes.

The authors take a four-part approach to explaining Grails and its underlying Groovy programming language:

  • Part 1: Introducing Grails – You are shown how to get a nicely formatted Quote of the Day (QOTD) application up and running, while also learning how to work with Groovy.
  • Part 2: Core Grails – You get a “more thorough exploration of the three core parts of the Grails ecosystem: models,
    controllers, and views.” Includes such topics as: domain modeling; query mechanisms; how to query a database in Grails without using SQL; Grails’ web-oriented features; Grails Service objects; Grails’ tags for user interface construction; and Grails support for Ajax.
  • Part 3: Everyday Grails – The focus here is on “building all the necessary pieces of a real-world application.” The chapters cover tests, plug-ins, security in Grails and working with RESTful APIs. The chapters also cover (1) Grails single-page web apps using the Angular.js framework, and (2) Spring integration in Grails.
  • Part 4: Advanced Grails – These chapters zero in on “performance tuning, legacy integration, database transactions, custom build processes, and even how to develop and publish your own plugins.”

Aside from a few small omissions of how-to information, I am happy to have the wide-ranging contents of this book. And I am certainly pleased with what I can now do with Grails and Groovy, after reading Grails in Action, Second Edition.

 —Si Dunn

 

 

Gradle in Action – Had enough of Maven and Ant? Try this powerful Java build tool – #programming #bookreview

Gradle in Action

Benjamin Muschko

(Manning, paperback)

Apache Maven and Apache Ant are perhaps the two best-known and most widely used Java build tools. But Gradle is gainmuschkoing users and aficionados ( if, indeed, there can be “fans” of software build tools). And this well-prepared new book likely will help Gradle achieve even wider acceptance and employment in the workplace.

“Gradle,” writes Benjamin Muschko, “is the next evolutionary step in JVM-based build tools. It draws on lessons learned from established tools like Ant and Maven and takes their best ideas to the next level. Following a build-by-convention approach, Gradle allows for declaratively modeling your problem domain using a powerful and expressive domain-specific language (DSL) implemented in Groovy instead of XML. Because Gradle is a JVM native, it allows you to write custom logic in the language you’re most comfortable with, be it Java or Groovy.”

Muschko has a strong bias toward Gradle, of course. He is a member of the Gradleware engineering team and has written several popular Gradle plugins.

Nonetheless, his well-written, 15-chapter, 456-page book makes a compelling case for Java developers to add Gradle to their build-tool cabinet and list of skills.

“Over the course of years,” Muschko contends, Maven and Ant “[have] significantly improved and extended their feature set. But even though both are highly popular and have become industry standards, they have one weak point: build logic has to be described in XML. XML is great for describing hierarchical data, but falls short on expressing program flow and conditional logic. As a build script grows in complexity, maintaining the build code becomes a nightmare.”

To use Gradle, you do have to learn parts of yet another programming language, Groovy. But, if you already know Java, you are much of the way there.  “The language [Groovy] integrates with existing Java classes and libraries, which makes it easy for Java developers to learn it.” Muschko stresses. “Not only does Groovy build upon the strengths of Java, it also provides powerful programming features inspired by those of Ruby, Python, and others. Groovy can be used as a scripting language without having to compile the code. Alternatively, Groovy code can be compiled to Java bytecode.” (Both approaches are used in the book.)

“Gradle’s core functionality is built with Java,” Muschko points out. “On top of this functionality sits a domain-specific language (DSL) written in the dynamic programming language Groovy. When writing a Gradle build script, you automatically use the language constructs exposed by this DSL to express the desired build instructions. Gradle build scripts are executable Groovy scripts, but they can’t be run by the Groovy runtime. When the need to implement custom logic arises, you can use Groovy’s language features to build out the desired functionality directly in the Gradle build script.”

Gradle in Action uses some Groovy in most of its code examples. But you are not expected to have experience with that language. Instead, Muschko gradually introduces Groovy and shows how it is used in the build processes, while keeping the book’s focus on Gradle and Gradle’s advantages over Maven and Ant (with Ivy). (You can run many of the book’s code examples from the Gradle command line and also, as I did, try out some of the Groovy code snippets using Groovy’s console. And Chapter 10 describes Gradle’s IDE plug-ins for Eclipse and NetBeans.)

“This book is primarily for developers and build automation engineers who want to implement a repeatable build that’s easy to read and extend,” Muschko says.

Muschko’s book is organized into three parts:

  • Part 1, Introducing Gradle
  • Part 2, Mastering the Fundamentals
  • Part 3, From Build to Deployment.

Part 1 includes an introduction to project automation and illustrates the differences in how builds are put together in Maven, Ant, and Gradle. It also shows how to write and execute a simple Gradle script, run Gradle on the command line, and build a Gradle project by example.

Part 2 delves into more advanced topics such as dependency management, testing an application with Gradle, extending a build with plugins, and other subjects, such as multiproject builds. It also digs deeper into testing, Gradle’s extension mechanism, and “how to translate existing build logic from one tool to another, identify integration points, and depict migration strategies.”

Part 3  emphasizes how to use Gradle in deployment. “In times of increased pressure to deliver software quickly and frequently,” Muschko writes, “automating the deployment and release process is extremely important. In part 3, you’ll learn how to use Gradle to its fullest in the context of continuous delivery.”

Meanwhile, Appendix A provides a closer look at how to use Gradle’s Command Line Interface, while Appendix B, titled “Groovy for Gradle users,” provides an introduction to what the author terms “Groovy’s most important language features,” with recommendations to help you learn more Groovy on your own. 

“For years, builds had the simple requirements of compiling and packaging software,” Muschko says. “But the landscape of modern software development has changed, and so have the needs for build automation. Today, projects involve large and diverse software stacks, incorporate multiple programming languages, and apply a broad spectrum of testing strategies. With the
rise of agile practices, builds have to support early integration of code as well as frequent and easy delivery to test and production environments. Established build tools continuously fall short in meeting these goals in a simple but customizable fashion.”

So, will it be Gradle to the rescue? In some settings, perhaps yes. In other environments, you may need to know how to use Maven, Ant and Gradle, plus some other build tools. And in still other work settings, the powers that be may insist on Maven or Ant or something else.

In any case, if you work with Java software builds, you may want to consider learning Gradle (and, by default, some Groovy, too). If so, give serious consideration to Benjamin Muschko’s excellent new how-to book.  In its foreword, Hans Dockter, the founder of Gradle and Gradleware, terms Gradle in Action “the authoritative guide.”

Si Dunn

Making Java Groovy – An excellent how-to guide for Java developers – #programming #bookreview

Making Java Groovy

Kenneth A. Kousen
(Manning, paperback)

I have looked at a lot of programming how-to texts in recent years, and this is one of the best I have encountered. Kenneth A. Kousen does not try to get readers to abandon aging and bloated Java in favor of a language that’s sleeker, shinier and newer. Instead, he seeks to show us how to use Groovy’s features and capabilities to help simplify Java, to make working with it easier, more efficient and–yes, even a bit more enjoyable. Indeed, Kousen proposes that we should gradually blend more and more Groovy into our Java code. Hence the title, Making Java Groovy.

(By the way, I lived through the hippie Sixties and remember how weary many of us grew of endlessly hearing the happy word “groovy”–as in “It’s groovy, man, groovy! Everything’s groovy!” I can say honestly that I resisted looking at Groovy, the programming language, for a long time, primarily because of that long-ago, wince-inspiring memory.)

Groovy is an object-oriented programming language that works on the Java platform, and it is intended to complement Java, not replace it. As Guillaume Laforge, the Groovy program manager, explains in his foreword to Kousen’s book: “The idea [behind Groovy] was to empower users to be more productive by removing the boilerplate of Java, and to simplify their programming lives by giving them compelling and straightforward APIs [application programming interfaces] to work with.”

Ken Kousen emphasizes that, among other things, Groovy “adds new capabilities to existing Java classes.” It uses Java libraries. And it makes it easier to work with XML and JSON. Kousen adds that “the Groovy version of a Java class is almost always simpler and cleaner. Groovy is far less verbose and generally easier to read.”

His book has ten chapters and two appendices and is structured into three parts. The first part, titled “Up to Speed with Groovy,” focuses on some long-time Java problems that Groovy addresses and simplifies, and it presents some ways you can use Groovy’s features in Java. The obligatory “Hello, World!” example is presented, but Kousen’s code accesses Google Chart Tools and generates a cool (okay, a groovy) “Hello, World!” 3D pie chart, to which you can add new slices and labels. (But, yes, you do first get the bland and obligatory Groovy shell command-line groovy:000>println ‘Hello, World!’ to verify that your Groovy installation works. Then you get to create the pie chart.)

Kousen is an independent consultant and technical trainer who specializes “in all areas related to Java.” He presents several Java code examples and shows how much shorter they can be made by integrating some Groovy into the mix. “Java historically hasn’t played well with others [other programming languages],” he cautions. But he demonstrates how Java and Groovy can be integrated smoothly to solve some nagging Java irritations and shortcomings.

The book’s second section highlights “Groovy Tools” and emphasizes build processes and testing, “two of the major ways Groovy is often introduced into an organization….”

The third section, “Groovy in the Real World,” describes “the sorts of challenges Java developers face on a regular basis” and how Groovy can help overcome them. The author starts with the Spring framework, “probably the most commonly used open source project in the Java world” and illustrates how “Spring and Groovy are old friends and work together beautifully.”  He also examines: (1) how Groovy interacts with persistent storage; (2) REST-ful web services, “with an emphasis on the JAX-RS 2.0 specification”; and (3) using Groovy in web application development.

The book’s instructions for downloading and installing Groovy are positioned near the back in Appendix A. and seem a bit sparse. But this is okay, since Making Java Groovy is not intended for programming beginners. And I can confirm that writers really don’t like to have to pause a well-written introduction long enough to explain how to download and install the right software for several operating systems.

Following Kousen’s instructions, I installed Groovy fairly easily on a Windows 7 PC, using a Windows EXE installer. He also mentions (too briefly) the Eclipse Marketplace. Since I have Eclipse (with Java and Scala) installed on a Linux machine, I used the Marketplace to get the Groovy and Grails plug-in, too. (My wife did not want Groovy anywhere on her precious Mac, so I did not test that installation.)

“Groovy generates Java bytecodes that are interpreted by a Java virtual machine,” Kousen states. “This means you have to have Java installed to install Groovy.”  Note also: “You need a full Java Development Kit (JDK) rather than a Java Runtime Environment (JRE).” And the Standard Edition (SE) is fine.

Appendix B, “Groovy by Feature,” expands upon Chapter 2’s “Groovy by Example,” and this one seems to be oddly placed. “While some people learn best through short, simple code examples illustrating each concept,” Kousen explains, “others prefer to see basic concepts combined to solve actual problems.” Appendix B “walks through most of the major features of Groovy and provides short snippets of code illustrating them.”

I would be happier if Appendix B had been positioned as Chapter 3, instead. Many people can learn both by feature and by example. And there should be nothing wrong with using one approach to reinforce the other.

But this is minor nitpicking. Making Java Groovy is an excellent how-to guide that I predict will go a long way to help popularize Groovy programming–even among us old guys who still shudder at the verbal excesses of our youth, when we proclaimed “everything” was “groovy.”

Si Dunn

Programming Groovy 2 – For the experienced Java developer seeking dynamic productivity – #programming #bookreview

Programming Groovy 2

Dynamic Productivity for the Java Developer

Venkat Subramaniam
(Pragmatic Bookshelf, paperback)


The programming language
Groovy has a bit of a checkered past. Before it reached Release 1 in early 2007, it was almost abandoned because of a series of development problems. But some dedicated developers reworked it, gave it new life, and helped it gain acceptance in a widening array of commercial projects. Release 2.0 became available last year, and you can download 2.1, with 2.2 in beta. Groovy is doing groovy now, thank you very much (and thank the Grails web application framework, too).

In his new book, which keys off of his 2008 Groovy 1 edition, Dr. Venkat Subramaniam  describes Groovy 2 as “lightweight, low-ceremony, dynamic, object-oriented, and runs on the JVM [the Java Virtual Machine].”

He notes: “Groovy is open sourced under the Apache License, version 2.0. It derives strength from various languages such as Smalltalk, Python, and Ruby, while retaining a syntax familiar to Java programmers. Groovy compiles into Java bytecode and extends the Java API and libraries. It runs on Java 1.5 and newer. For deployment, all we need is a Groovy Java archive (JAR) in addition to the regular Java stuff, and we’re all set.”

Groovy is not for coding beginners, nor is it a means to avoid learning Java. This book–well written and nicely illustrated with short code examples and screenshots–”is aimed at Java programmers who already know the JDK [Java Development Kit] well but are interested in learning the Groovy language and its dynamic capabilities,” Dr. Subramaniam says.

He has organized his 19-chapter, 333-page book into three major parts:

Part I: Beginning Groovy – Focuses on the fundamentals of the language but deliberately skips the basics of programming. The book, after all, is aimed at “experienced Java programmers.”

Part II: Using Groovy – Shows “how to use Groovy for everyday coding–working with XML, accessing databases, and working with multiple Java/Groovy classes and scripts….” Also delves into Groovy extensions and additions to the JDK.

Part III: “MOPping Groovy” – The odd title may conjure up a brief image of mopping up spilled gravy. But this part deals with “Groovy’s metaprogramming capabilities….” The coverage includes: (1) metaobject protocol (MOP); (2) “how to do AOP-like operations in Groovy” [AOP = “aspect-oriented programming”]; and (3) “dynamic method/property discovery and dispatching,” as well as Groovy’s “compile-time metaprogramming capability….”

If you are a Java developer seeking new tools. new challenges, and new horizons, this could be the right time and right way to get your groove on: with Groovy 2 and Dr. Venkat Subramaniam’s fine how-to guide.

“Groovy is an attractive language for a number of reasons,” the author says, naming four key ones:

“It has a flat learning curve.”

“It follows Java semantics.”

“It bestows dynamic love.”

“It extends the JDK.”

“Groovy,” he adds, “feels like the Java language we already know, with a few augmentations.”

Si Dunn