Image by Brian Kenney via Shutterstock, with addition of Mockito Made Clear cover

Unboxing Day, Part I

Mockito Made Clear

Kenneth Kousen
4 min readJan 25, 2023

--

https://pragprog.com/newsletter/

How do you make an unboxing video for an ebook?

I have a new book coming out, called Mockito Made Clear, from the Pragmatic Bookshelf. The book is part of their Pragmatic Answers series, which has a couple of interesting features:

  • The books are supposed to be short, on the order of 50 pages. (My book is 75 pages, because apparently I have trouble writing that short. I could make the Blaise Pascal reference, “I would have written a shorter letter, but I did not have the time,” but I already spent way more time on this book than I intended.)
  • The books in the Answers series are inexpensive, on the order of $10 U.S., which is pretty cool.
  • Most importantly for this series of posts, the books are ebook only. There are no print options.

The ebook-only aspect presents a (slight) problem: how am I supposed to do an unboxing video for an ebook? For my previous book, Help Your Boss Help You, also with PragProg, you can find my unboxing video for the five paperback copies I received on the book’s page at Amazon.

📗 Help Your Boss Help You ranked #1 on Amazon in New Releases in Business Ethics (?) when it came out. It is currently ranked #309 in that category, and #390 in Business Communications. The Kindle version is #261 in Communication in Management and #930 in Business Ethics. But I digress.

My plan in this series of posts is to show a few silly solutions to the unboxing problem for my ebook, in a series of increasingly overcomplicated and unlikely implementations. There will, inevitably, be a companion video or two.

Step 0: The Quick (But Incredibly Simple and Boring) Way

You can purchase Mockito Made Clear from the main book page at pragprog.com, at this link.

That means the simplest way to unbox it is:

  1. Buy the book at PragProg.com.
  2. Have them add it to your Dropbox account or save it locally.
  3. Click on the pdf and watch it open in your associated pdf reader.
  4. Read and enjoy. :)

I mean, you could do that, but there would be a distinct lack of confetti. In addition to learning how and why to use Mockito, you would have the deep appreciation of a grateful author, but that’s about it.

Surely we can do better, right?

(Yes we can, and don’t call me Shirley.)

Step 1: Open the Book Programmatically in Java

It turns out that it is remarkably easy to ask Java to open a local application associated with a file extension. You want the java.awt.Desktop class, which is a real age check for those of you who don’t normally do desktop Java. The good old (emphasis on the word old) java.awt package is the Abstract Window Toolkit (AWT) — the original classes for client-side user interfaces in Java. Some of those classes are still relevant, and one of them is Desktop.

Javadocs for the java.awt.Desktop class

As the docs say:

The methods look for the associated application registered on the current platform, and launch it to handle a URI or file. If there is no associated application or the associated application fails to be launched, an exception is thrown.

With that in mind, the method you need is open, which takes a java.io.File as an argument. You call it this way:

public void openPdf(String fileName) {
if (Desktop.isDesktopSupported()) {
try {
Desktop.getDesktop().open(new File(fileName));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}

Holy checked exceptions, Batman — but what can you do? Rethrow them as unchecked, I guess.

My test for this method is:

@Test
void openPdf() {
// Project Gutenberg returns epubs instead of pdfs, but they work too
unboxing.openPdf("src/main/resources/pg1661-images-3.epub");
}

The epub file I’m using is The Adventures of Sherlock Holmes, from Project Gutenberg. Project Gutenberg returns epub documents instead of pdfs, but the same procedure works. It just might open a different associated application. The listed file has been downloaded and stored in the src/main/resources folder of the project repository.

I know that’s not much of a test, since I’m not asserting anything, but the open method returns void, and void methods are hard to test. I can vouch for the fact, however, that the method does in fact open the pdf. Now I can make an actual video.

But why stop there? I had to do the actual download of the book and store it locally myself. Can’t I do that part programmatically too?

Stay tuned for the next entry in this series, same Bat-time, same Bat-channel.

Spoiler alert: All the code is available in this GitHub repository.

Cover of Mockito Made Clear by Ken Kousen featuring limes and mint as a play on “mojito”
Cover of Mockito Made Clear by Ken Kousen

--

--

Kenneth Kousen
The Pragmatic Programmers

Author of the books Mockito Made Clear, Help Your Boss Help You, Kotlin Cookbook, Modern Java Recipes, Gradle Recipes for Android, and Making Java Groovy