Tales from the jar side: Espresso and the Hamcrest Matchers, Black Hole sounds, and Viral theme songs
My fat parrot died, which is sad, but a huge weight off my shoulders. (Rimshot)
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of August 21 - 28, 2022. This week I taught a class on Getting Started with Spring and Spring Boot on the O’Reilly Learning Platform, an NFJS Virtual Workshop called Deep Dive Into Spring, and another day of a private Android course.
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, when 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.
Espresso and the Hamcrest Matchers
Frequent readers of this newsletter know I try to add tests to my code. For example, I’m a big fan of the improvements added to version 5 of JUnit, which is now named JUnit Jupiter. I give a regular presentation on both the NFJS tour about testing with JUnit Jupiter, and I teach a course on the O’Reilly Learning Platform about it.
In case you’re wondering, there are two reasons why JUnit 5 is called Jupiter, both rather silly:
Jupiter starts with the letters “JU”, just like JUnit.
Jupiter is the fifth planet from the Sun, and this is JUnit 5.
When I mention that in class, it provides me an excuse to include a photo from the Juno satellite in orbit around Jupiter. Here, for example, is an image from the Juno image gallery at NASA:
I have no idea what the JUnit team is going to do when they move to JUnit 6. Rename it to Saturn, maybe?
Another testing library I use on a regular basis is Mockito, which lets you generate mocks and stubs to represent dependencies of the classes you’re trying to test. In fact, I’m working on a new book, called Mockito Made Clear, currently available in beta from the Pragmatic Bookshelf publishing company.
As part of my Mockito course on the O’Reilly Learning Platform, I also taught a course called Mockito and the Hamcrest Matchers, which, I have to admit, sounds like a cartoon band from the 70s, like Josie and the Pussycats.
(Incidentally, I’ve heard the 2001 Josie and the Pussycats movie has become a cult classic and is supposed to be surprisingly good, but I still haven’t seen it. I’ll try to fix that soon.)
The Hamcrest library is what we call “syntactic sugar,” which means it doesn’t really change the tests, but it makes it easier to express and read them. For example, instead of saying:
assertEquals(defaultProduct, myProduct)
you write instead
assertThat(myProduct, is(equalTo(defaultProduct))
Same assertion, but somewhat more readable for a native English speaker.
While the Hamcrest library is useful, it hasn’t changed much for several years, and I’ve been transitioning away from it. The Spring team is fond of the AssertJ library, which is substantially more extensive. They include it in every Spring app, and a lot of their examples use it.
Testing in Android is a lot more primitive, for a variety of reasons. The big testing library in Android is called Espresso, which used to be a separate library but is now included in Android itself (though you still have to add dependencies to the Gradle build file). It’s a library that allows you to programmatically automate the user interface, as in:
@Test
fun greeterSaysHello() {
onView(withId(R.id.name_field)).perform(typeText("Steve"))
onView(withId(R.id.greet_button)).perform(click())
onView(withText("Hello Steve!"))
.check(matches(isDisplayed()))
}
That means, find the name
text field, type in the name Steve, click the greet
button, and check that there is text with the words ‘Hello Steve!’ displayed somewhere on the user interface.
It’s an interesting library, but the documentation is, shall we say, a bit thin. I was working with the students trying to add a test to an app we got from the training materials, and realized I didn’t know the official way to express an “or” condition. After class I dug into the problem, and the key turned out to be that matches
method shown in that code snippet. If you look in the Javadocs for the ViewAssertions class
, the signature of the matches
method is:
static ViewAssertion matches(Matcher<? super View> viewMatcher)
(Note the docs show the Java syntax, given that Hamcrest is a Java library, even though you can call it from Kotlin.)
That Matcher
argument turns out is my old friend, org.hamcrest.Matcher
. I had no idea that Espresso was built on top of the Hamcrest library, which I already know something about.
(The Robin Williams caveat from Good Will Hunting still applies: “I teach this stuff. I didn’t say I could do it.” But at least I know what to try.)
That means I can write expressions like:
onView(withId(R.id.text_view))
.check(
matches(
anyOf(
withText("Fred"),
withText("Barney")
)
))
The anyOf
method comes from Hamcrest. If you look at the GitHub repository of Espresso samples, they even include one that creates a custom Hamcrest matcher to use in their tests, which is another topic I cover during my Hamcrest course.
In other words, I’m good. A whole world of Espresso testing opened up, based on a library I already knew but was in the process of abandoning because it’s so dated. That, too, is a typical Android thing — while Android itself changes all the time, their dependent libraries (especially those from the Java world), are often horribly out of date. For once, that worked in my favor.
My next challenge is to find a way to get the Josie and the Pussycats theme song out of my head. Here’s the version from the movie:
What would the kids today say? It slaps? It most certainly does. And yeah, I’m old, but you already knew that.
(Now that I think about it, the phrase Espresso and the Hamcrest Matchers reminds me as much of Katrina and the Waves, but that’s enough nostalgia for one newsletter.)
Tweets and Other Stuff
In Space, No One Can Hear You Scream…
… unless, apparently, you’re a black hole. Keeping with the space references this week, NASA released this fascinating little video:
Spooky. I knew it wouldn’t take long, and fortunately it didn’t, for Dr. Becky to put up a quick explainer video:
In case you don’t follow her, Dr. Becky is great. Her explanations are clear and understandable, and she simply radiates charm. You can find her channel here.
Composition Puns FTW
My friend Frank Greco responded to another tweet and brought up the whole issue of building up applications by assembling related parts (composition) vs extending families of classes (inheritance):
That gave me an opportunity to throw in my favorite related joke:
Snicker. It’s not true, by the way. If you look into it, you’ll find Beethoven’s father was much like Mozart’s father, pushing his little prodigy really hard and trying to make him go viral. Still, it’s a good joke. Just replace Beethoven with the composer of your choice.
Mo’ Money
As you might have heard, student loans are in the news. For the record, it took me ten years to pay off my students loans, and I don’t want anyone else to have to go through that. I’m quite happy to have everybody get the education they want and need without worrying about cost.
(To be specific, my parents paid for four years of college, though I did take out a small loan, at least by today’s standards. I was a double major, so I joked I paid half for my two BS degrees of what everybody else paid. Since I was in a STEM field (engineering), for graduate school I was able to pay for it with a combination of fellowships, research assistantships, and teaching assistantships. That meant I actually got paid to go to graduate school, and though it was a very small amount, at least it was coming in the right direction. I got my MA and Ph.D that way. Then, later, I went to school at night, paid for by my employer, to get my MS in Computer Science. They were willing to pay for two courses a semester, so that’s what I did.)
The push-back conveniently ignored the money so many wealthy people received in the form of other loans from a wide range of sources. ProPublica prepared a site you can use to find out about those Paycheck Protection Program (PPP) loans sent out to businesses during the pandemic:
While the potentially unscrupulous borrowers made the news (especially those by members of congress, but I repeat myself), I prefer to believe that virtually all of the PPP loans did what they were designed to do — keep businesses afloat while the whole economy shut down during the pandemic. Everybody wants to pretend the pandemic is over, or, even more, never happened, but as one of only about 5 people in the US who still hasn’t gotten COVID, I can’t think that way.
The search site is free and easy to use, if you want to play with it.
Speaking of Hypocrisy
Here’s a nice bit of sarcasm:
As the saying goes, “the essence of conservatism is that there must be in-groups that the law protects but does not bind, and out-groups the law binds but does not protect.” We see it over and over and over again these days.
The Aliens Have Landed
Finally, I know it’s never aliens, but just in case it is:
Sup, indeed.
Have a nice week everybody.
(Josie and the Pussycats!
Long tails and ears for hats
Guitars and Marshall stacks
Hot, sweet, super cool, don't y'all know these kitties rule?
Hurry, hurry!)
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:
Getting Started with Spring and Spring Boot, on the O’Reilly Learning Platform (APAC time zone)
Deep Dive: Spring and Spring Boot, an NFJS Virtual Workshop
Android development, private class
This week:
Android development, two days, private class
Functional Programming in Java, on the O’Reilly Learning Platform