Tales from the jar side: Spring 6 Http interfaces, Mockito 5, Joking with ChatGPT, Upcoming talks, and a Feel Good story about Unicorns
How much does Santa pay for parking? Nothing, it's on the house <rimshot>
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of December 4 - 11, 2022. This week I taught the first week of my Spring and Spring Boot in 3 Weeks course on the O’Reilly Learning Platform, and two NFJS Virtual Workshops: one on Java Testing with JUnit 5 and AssertJ, and the other on Mocks, Stubs, and Spies with Mockito 4.
Regular readers of (and listeners to) this newsletter are affectionately known as jarheads, and are far more intelligent, sophisticated, and attractive than the average newsletter reader (or listener). If you wish to become a jarhead, please subscribe using this button:
I know I make that claim every week (about how smart, sophisticated, and attractive jarheads are), but I’ve met at least some of you and it’s been true in every single case. Just sayin’.
As a reminder, when this message is truncated in email, click on the title to open it in a browser tab formatted properly for both the web and mobile.
Springing Into Action
Next Friday, I’m giving an NFJS webinar online entitled, The Big Jump: Spring 6 and Spring Boot 3. It’s only an hour, given over lunch time (more or less), from 1pm - 2pm EST. Please forgive the click-baity title, because while the major version change is a big deal, Spring and Spring Boot themselves didn’t really change much.
The big shift is in the underlying Java system and libraries. I know I mentioned this a couple weeks ago, but the big changes are that Spring now requires Java 17 and uses the Jakarta EE libraries instead of Java EE. I’ll get into that in my talk. I’m planning to show an interesting app that is mostly about the newer features in Java 17, like text blocks, records, sealed classes, pattern matching, and so on.
Still, it’s always nice when other people do the work for me. In this case, my friend Dan Vega decided to publish a YouTube video on the changes, called What’s New In Spring Framework 6 and Spring Boot 3:
I watched the whole thing (you’re welcome, Dan) and he does his normal excellent job. He did a very quick demo of the new GraalVM native image capability, which I also plan to do, and he spent a fair amount of time on the new “observability” implementation with Micrometer, which I don’t use and don’t really know at all. I don’t plan to talk about that in my webinar, other than mentioning it happened and pointing to the documentation.
(The changes in Spring Boot 3 are summarized in this blog post.)
Dan, however, demonstrated one new capability of Spring 6 that I haven’t used, and since Craig Walls did the same in one of his recent videos, I feel obligated to at least try it out. It’s called Http Interfaces, which lets you declare an interface with @HttpExchange
annotations on the methods, like @GetExchange
or @PostExchange
. The motivation in the Spring docs says:
The Spring Framework lets you define an HTTP service as a Java interface with annotated methods for HTTP exchanges. You can then generate a proxy that implements this interface and performs the exchanges. This helps to simplify HTTP remote access which often involves a facade that wraps the details of using the underlying HTTP client.
Honestly, I’m not convinced this is any easier than just using the RestTemplate
or the WebClient
classes directly, but I’ll try it out in preparation for the webinar and report back next week.
Mockito 5 coming Real Soon Now
In my upcoming book, Mockito Made Clear (available in beta, but now in copy editing and soon to be released), I’ve been facing a bit of a dilemma. In any book based on a library, you want to make sure you’re using the latest version of the library. The current version of Mockito is 4.9.0, updated just last month. I know they’re planning a release of Mockito 5, however, but I can’t find any commitment to actually when that will be done.
In browsing the GitHub repository for Mockito, I noticed they’ve got a highlighted issue called Release Mockito 5, which involves two major changes:
The first one sounds confusing, but all it really means (if I understand it correctly), is that you won’t need to change your dependency in your build file (Maven or Gradle) from mockito-core
to mockito-inline
in order to mock final classes, because that’s going to be the default in Mockito 5. That works for me, and I mentioned something about that in the text of my book.
That actually came up this week in my Mockito course. I wanted to show how you can “spy” on an existing dependent object in order to verify that the methods on it were called the right number of times, with the right arguments, in the right order. Here’s my test:
Normally I stick to Java 8 in my courses, because a significant fraction of the Java community still uses that, but in this case I used Java 17, and that meant I could use the static List.of(…)
method to create my wrapped list. When I ran the test, however, I got:
Cannot mock/spy class java.util.ImmutableCollections$ListN
Mockito cannot mock/spy because :
- final class
I knew that, though I hadn’t really thought about it. The “of
” method was added to List
in Java 9, and produces an unmodifiable list, meaning if you call any method that changes the number of entries, like add
, clear
, or removeAll
, Java throws an UnsupportedOperationException
. What I hadn’t realized was the the implementation class is actually final
.
No problem — I just changed my Gradle dependency from mockito-core
to mockito-inline
, and it’s all good. In Mockito 5, presumably even that change will not be necessary.
The other issue scheduled for Mockito 5 is the move from a minimum JDK level of 8 to 11, so that List.of
method will already be available (among others). That’s an interesting choice, too, but isn’t as dramatic as moving all the way to 17 like Spring is doing.
The Release Mockito 5 issue was created about two weeks ago, but I can’t tell from the comments how long it is expected to take to complete. My prediction is that they’re waiting for my book to be released so it will look out of date as soon as it’s published, but that might be just a tad paranoid on my part.
When the book comes back from the copyeditor, I’ll squeeze in a few words about everything working under Mockito 5 (beyond what I’ve already included in the chapter on mocking final classes and methods), and hope for the best.
One of my books is on sale
On a marginally related note, in the sense that both books are from the same publisher, my Help Your Boss Help You book is currently on sale. According to this brand new Medium post (which came out as I was writing the previous section):
On December 12, from 6 p.m. to 11 p.m. EST, you can save 40 percent on Effective Remote Work by James Stanier and Help Your Boss Help You by Kenneth Kousen . Both are available as ebooks and as audio books.
This is part of the #12DaysOfPragProg promotion they’re running, so I don’t believe any promo code is required.
Grab them while they’re hot.
(If you miss the sale, which is running for a very limited time, jarheads can use the promo code kkmanage35 for a 35% discount. That’s almost a whole extra dollar, though, so grab the bigger sale when it happens if you can.)
Chatting with AI for the NYJavaSIG
If you’ve been even tangentially connected to IT the past few weeks, you’ve noticed the skyrocketing attention paid to ChatGPT, the AI chat client currently available for free (though you know they’re going to start charging for it eventually). I spent a bit of time playing with it this week, treating it like the toy it is. I have to give you some context first, however.
This week I’m headed down to New York to give a talk Wednesday evening at their local Java Users Group, the NYJavaSIG. My talk is entitled, Functional Java, Groovy, and Kotlin — OR — I Gave My Talk To Venkat And He Messed It Up.
Here’s the abstract I sent in:
Back in March I was scheduled to give a talk at the NYJavaSIG about the functional features of the Java, Groovy, and Kotlin programming languages. I intended to discuss basic streams, lambdas, and method references in all three languages, and then look at more advanced functional features like trampolining, closure composition, and memoization.
Unfortunately, I was unable to attend, and I asked the inimitable Venkat Subramaniam to fill in for me, which, I am assured, he capably did. I gave him my slides and my code and expected him to do the speaker equivalent of presentation karaoke, but no, he threw everything away and live-coded it all instead, as he does.
I'm back now to clean up the mess he left behind. Please come and see as I go to great lengths to avoid becoming the Java speaker equivalent of Wally Pipp. (You're New Yorkers, so presumably you get that reference, but if not, I'll explain it during the talk.)
The talk will be live, but I know they’re planning to try to stream it online as well. If you’re available, register for it at this link.
I plan to give the talk in the same somewhat silly spirit. I have a lot of real examples (you can find the GitHub repository here) as well as slides, but I’m going to try to be at least somewhat entertaining. To that end, I chatted with the ChatGPT client:
The gags range from “Huh?” to “Okay, that’s kind of funny” to “I promise never to tell that joke under any circumstances,” but nice try, I guess.
I tried this, too:
That’s actually not bad.
The thing about ChatGPT is that it always sounds confident, but gets its facts wrong a lot of the time, leading a lot of people to describe it as Mansplaining as a Service. There’s a good article at The Atlantic called ChatGPT Is Dumber Than You Think which gets into some of the details. Just keep in mind it’s all pattern matching, not actually thinking or understanding anything you say, so it’s really good at producing plausible-looking boilerplate text.
Keep an eye out for similar articles. Most of the current ones talk about the hopelessness of teaching students how to write essays, because they’ll just use this and submit the results. My impression is that’s it’s useful for routine stuff, like generating cover letters.
Argh, matey, I be sure to get the job now, or it’s the plank for ye!
I’ll let you know next week how the talk goes.
Other Posts
What If What The What?
What if, indeed.
Direct Objects FTW
An old gag, but I still chuckled.
Who Let The Cats Out?
I Feel Seen
I Heard There Was A Secret Chord
Honestly, it’s posts like that that make it hard for me to leave Twitter, though the upcoming changes scheduled to be announced tomorrow might be the last straw. We’ll see.
Speaking of Twitter
Yup. Of course, people can be funny on Mastodon, too.
What could possibly go wrong?
Feel Good Story of the Week/Month/Year
I haven’t verified this, but if it’s not true, I don’t want to know. It’s too awesome.
The story goes that the following letter was written by a little girl named Madison, to the LA County Animal Control Office:
Someone in that office wrote back the best possible reply:
Isn’t that fantastic? Whatever they’re paying Marcia Mayeda (the listed Director, who presumably wrote the letter), it’s not enough. She even went beyond that, and included an official certificate:
And they even included a unicorn, with its own official license:
I dearly hope all that is true. If it’s not, as I say, I don’t want to know because I don’t care. The only downside I can even imagine is that every kid in the area who hears about this is going to send their own letter and want one, too, but it’s still worth it.
Have a great week, 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:
Spring and Spring Boot in 3 Weeks (week 1), on the O’Reilly Learning Platform
Java Testing, Part I: JUnit 5 and AssertJ, an NFJS Virtual Workshop
Java Testing, Part II: Mocks, Stubs, and Spies with Mockito, ditto
This week:
Spring and Spring Boot in 3 Weeks (week 2), on the O’Reilly Learning Platform
Managing Your Manager, ditto
Presentation at the NYJavaSIG, Wednesday evening
Gradle Concepts and Best Practices, an NFJS Virtual Workshop
The Big Jump: Spring 6 and Spring Boot 3, an NFJS webinar, Friday 1pm EST