Tales from the jar side: My new Mockito book, an Optional option, and funny tweets
Oldie but goodie: What would Wonder Woman and Spider-Man name their joint business? Amazon Web Services
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of May 29 - June 4, 2022. This week I taught my full-day Deep Dive Into Spring course and my Gradle Key Concepts course as NFJS Virtual Workshops, and my Managing Your Manager course on the O’Reilly Learning Platform.
Regular readers of this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader. If you wish to become a jarhead, please subscribe using this button:
As a reminder, this message is truncated in email, click on the title at the top to open it in a browser tab formatted properly for both the web and mobile. (You’ll probably need that this week, so you might want to click on the title now.)
Mocks and Stubs and Spies
This week, my latest book, Mockito Made Clear, was released in beta form. Here’s the announcement, via Twitter:
In case you can’t see the cover, and you don’t feel like going through Twitter, here it is:
Could the implication that a Mojito may be involved be any more obvious?
Perhaps that will be the way to celebrate the release of the book when it’s finished. I guess it depends on how well it sells, but maybe not. If it sells well, the drink is a celebration, and if not, it’s compensation. It’s like the silly debate about whether the Millenium ended on Jan 1, 1999 or Jan 1, 2000. My argument was that you should have a party on both days. Duh.
As the subtitle says, the Mockito testing library is about creating mocks, stubs, and spies. What are those, you ask?
This is obviously a mock:
This is a stub:
And here are spies:
None of these appear in my book, at least not in those forms, for copyright reasons if nothing else. No, mocks and stubs fill in for class dependencies, and return known outputs for given inputs (I’ll get to spies in a moment).
Here’s an analogy that isn’t in the book because it just occurred to me. Consider a rehearsal for a scene in a play, where one of the actors is not available. In order to practice the scene, someone still has to read those lines, so the other actors can work on their delivery, blocking, songs, and so on.
I should note that I was going to include a rehearsal scene from the wonderful Topsy-Turvy movie about Gilbert and Sullivan’s creation of their operetta The Mikado, where the stage manager is required to step in for two missing actors. As Jim Broadbent, playing Gilbert, says in the movie (around the 1:45 mark):
Since Nanki-Poo and Yum-Yum have decided not to grace us with their presence, it would transpire, Mr Seymour, that your moment of glory has finally arrived.
In lieu of that, here is a playlist of clips from the movie. My wife and I loved the movie, especially because we used to perform in local productions of several G&S plays.
At any rate, the stand-in provides the right lines at the right time, so that the real actors can rehearse the scene.
To define that in terms of software:
The class under test has one or more dependencies, which it uses to do its job.
Rather than use the actual dependencies, you set up a stub, which provides known outputs for its methods for given inputs.
If you also want to check afterwards that the class you are testing interacted with the stub the right way — called the methods on the stub the right number of times, in the right order — the stub is then called a mock.
Mockito is a library that lets you generate the mocks or stubs automatically, tell them what to do (which is called setting expectations), and verify afterwards that the class you’re testing worked with the mocks correctly.
(Incidentally, it’s easiest to watch The Mikado these days if you keep in mind that the target of the humor was the British society of the times, and that the Japan of the play bears no resemblance to any actual country, real or imaginary. Just replace all references to Japan with Mars or Narnia and you get much the same effect. Besides, the music — especially the whole first act finale — is awesome. That said, I must admit the casual racism and sexism of mid-1880s England is clearly on display.)
To complete the subtitle of my book, a spy sits in between the class you’re testing and a real dependency. The spy can be used to measure the interactions with the dependency, or even replace some of its behavior. Spies are supposed to be transparent, so rather than James Bond, think Anna Strong, who “arranged clothes on her clothesline as a means to signal a fellow spy regarding the location of hidden documents to be transported.” I don’t have her in the book either, though maybe I should. Hmm.
The final point I’ll make about the book is that it is part of the Pragmatic Answers series, which means it will only be available as an inexpensive ($10 US), short (around 50 pages, though of course I went long) ebook (pdf, mobi, and epub versions all available, with no DRM restrictions).
You’ll find Mockito Made Clear on the PragProg web site. If you do get it while it’s in beta, any feedback you send me will impact the final product, which of course you will receive when it’s done.
Optional Or Not
During my Spring course, I used the RestTemplate
to access a web service, get back a response, and extract a string from it. Since the getForObject(String,Class)
method can return a null, I normally use an if/then statement to decide what to return:
It occurred to me recently that I can replace the if
statement with this chain based on the Optional
class:
That avoids the if
statement, but is a bit more obscure. I’m not sure which one I prefer. If you have an opinion, please let me know.
Other Stuff
This may be the grumpiest cat on the Internet, and that’s saying something:
Wow. Jenny is not a happy kitty.
This tweet is making the rounds, in honour (British spelling, of course) of the Queen’s Platinum Jubilee:
That is one scary snake:
Finally, this was my experience as well:
Take care, everybody.
As a reminder, you can see all my upcoming training courses on the O’Reilly Learning Platform here and all the upcoming NFJS Virtual Workshops here.
Last week:
Deep Dive Into Spring, an NFJS Virtual Workshop
Managing Your Manager, on the O’Reilly Learning Platform
This week:
No classes, which is nice. I haven’t had a week without at least one class all year, and even then I was on a consulting project and working on a book as well. So yay, a bit of a break, other than the user group talk in the next item (and a private presentation for another client). Got to finish that book, too.
Help Your Boss Help You, presentation at the Gateway JUG: the St. Louis Java Users Group
I'm a fan of the use of Optional vs. using an If/Else statement, as it reads as more expressive that you are expecting a null to be passed and are handling it.
I know that an If/Else statement does the same thing, but at a glance you won't know immediately that the If/Else statement is looking for a `null` or another condition; However with an Optional, you're sounding the alarms that the result could be null.
Thus I feel that the use of Optional is a great showcase of self-describing code. Using Optional means it could be null, end of assumption. While when you are using an If/Else statement, you only know that you could have a condition that you're trying to assert, and said condition could be one of many different types!