Tales from the jar side: Mockito Answers, New books, and my Medical adventure
Dark joke this week: I tried to donate blood. Never again! Too many questions. Whose blood is it? Where did you get it? Why is it in a bucket?
Welcome, fellow jarheads, to Tales from the jar side, the Kousen IT newsletter, for the week of March 6 - March 13, 2022. This week I had to cancel all my classes, because I spent the week as a patient at Hartford Hospital.
More about that in a moment, but here’s the TL;DR: I’m fine. It was a DKA event (diabetic ketoacidosis) probably brought on by a weird combination of a medication and a trigger we don’t know. Looks like a one-time thing, so now it’s just a question of getting my strength back. For those who might be interested, I put more info at the bottom of this week’s newsletter.
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:
(I was tempted to replace that caption with, “I’m not doing this for my health, you know,” but that’s actually not true. Writing this newsletter helps me every week, at least mentally. But don’t worry, it’s free and always will be.)
Mockito Answers
I want to mention something technical, because I’ve been working hard on Mockito lately and another piece of the puzzle fell into place. I didn’t learn it this week, of course — this was supposed to go in last week’s newsletter — but it’s worth mentioning.
The key idea of Mockito is that it can replace dependencies of the class you are testing with autogenerated mocks and stubs. You then tell the stub what you want it to do (that’s called setting the expectations), inject the stub into the class you are testing, and run your test. Assuming the tests pass, you can verify the interactions between the class you’re testing and the stub, at which point we call it a mock instead.
I have an example system that involves a PersonService and a PersonRepository, which is representative of a typical Java layered architecture. The service is there for transaction boundaries and business logic, and the repository’s job is to transform Person objects into table rows and back again.
Say I want to test a service method called savePeople, whose signature is:
public List<Integer> savePeople(Person… people)
This method calls a corresponding method called save in the PersonRepository:
Person save(Person person);
I want Mockito to generate a stub for the repository, and when the method I’m calling invokes the save method, return the argument. Then I can verify that when I call savePeople with a collection of Person instances, the save method is called for each one.
Here’s one test that does the job, but it’s a bit awkward:
The part I don’t like is the willReturn block, where I list the arguments I plan to pass into the save method one by one. What if the order changes? What if I get it wrong?
It turns out, this is why Mockito has the Answer<T> interface. It has a single, abstract method:
T answer(InvocationOnMock invocation) throws Throwable
That doesn’t look terribly helpful, but the InvocationOnMock argument is the key. It gives you access to the method being called, its arguments, and more. In fact, all I have to do is replace the willReturn call from lines 4 - 9 with the willAnswer line here on line 5:
Now the stub returns whatever the first argument was to the save method automatically. Much cleaner, though again you have to remember what that “invocation” thing is. It’s not the clearest syntax in the world (welcome to the wonderful world of Mockito), but once you know what to look for, it work well.
As an aside, this is yet another item I learned at one point and forgot, so I had to learn all over again. Still, it’s going to be in my upcoming book.
Other Stuff
My friend and fellow NFJS speaker Craig Walls informed me that the 6th edition of his classic book, Spring in Action, is now available at Manning:
I learned a ton from earlier editions of this book. Craig can both write and code, and as you no doubt know, that’s not a common combination.
Craig has also been spending on writing voice apps for Alexa. You might want to take a look here as well:
You might want to check it out.
Seems Timely
March 15 is Tuesday, btw.
My Medical Adventure
I’ve been sending this newsletter every week for over three years now. I recently joked to my wife that if I ever missed a week, my readers would think I was in the hospital.
Funny, that.
Last Saturday I woke in the morning completely exhausted, and went back to bed. I got up a few hours later and repeated the process. Next time I tried to eat something, managed only a few mouthfuls, and went back to bed. This pattern repeated itself all weekend long.
I had no other symptoms — no shortness of breath, no fever, no coughing, nothing. Just complete exhaustion. The only clue something else was wrong came when I used our pulse oximeter, which I bought near the start of the pandemic. My pulse was sitting at a steady 117. No wonder I was tired.
By Monday morning, nothing had changed, and my wife had had enough. She called the doctor, made an appointment, and dragged me there. Before going, we took one of those home COVID tests, which came up negative, so at least I had that going for me.
The doctor thought it was probably the flu, but he suggested we go back to the emergency center in Marlborough (my home town), to get some blood work done and probably a saline IV. While there, the doctors kept coming back for more. Finally they did one additional blood test and informed us they were calling ahead to admit me to Hartford Hospital. Apparently my blood showed high levels of proteins associated with a mild heart attack. This came as quite a surprise, because I hadn’t had any of those symptoms, either.
At the hospital, in the ICU, they used the term DKA (diabetic ketoacidosis) for the first time. Apparently something had gotten out of whack with my blood sugars and my heart was working so hard to keep up it was damaging itself. The weird part, again, was my blood sugar was only about 210 when I arrived, but we’re blaming one of the medications as artificially suppressing the actual results, which came from a urine test and were sky high. (I don’t have the actual numbers, but I expect they’re in my records somewhere.)
The next few days passed in a blur. I remember a few snippets.
Doctor: We’re adding another IV, or maybe we can combine these two…
Me: What’s the record?
Or,
Doctor: You say your stomach hurts?
Me: Yeah, but look, as long as I’m here already, how hard would it be to do some liposuction and get rid of 20 to 30lbs right now?
Or,
Doctor: We’re going to give you a stress test. Have you had one before?
Me (debating potential answers: 1. You mean my entire life? 2. Waves vaguely around at the world, 3. Ha! Stress is my middle name!): …
Doctor: …
Me: No.
Or even,
(Nurse hands me a urinal)
Me: You realize the opening probably isn’t big enough, right?
Nurse: …
Me: I’ll do my best.
I’ll spare you the rest. I’ll just say that when the plumbing has “issues” it tends to dominate your thinking, but there’s no need to share the details.
I came home Friday night, and am mostly recovered, though still moving slowly. That’s the thing about reading this — you have no idea how much work it was just to write it, or how much longer it took than normal to do so. Still, I have my obligations, so onward we go.
The good news, as mentioned at the beginning, is that this isn’t the beginning of some major battle or anything like that. I imagine I’ll have to be more careful about my diet and start getting more exercise, but so big surprise there, either. I was a one-off, and it’s basically over.
I’m glad to be home, and you should expect to see more issues of this silly newsletter every Sunday for the foreseeable future.
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:
Sadly, had to cancel my classes. Make-up dates are currently being scheduled.
This week:
Private class, Tuesday through Friday.
Glad to hear you are ok! I just read that Joshua Bloch was diagnosed with colon cancer, hopefully he will be ok too. Keep up the good work, it’s appreciated!