Coming Up for Air

Writing CLIs with Spring Boot and JCommander

I was recently asked to convert a Spring Boot-based "CLI" to a real CLI utility. It was actually just a normal Spring Boot application with REST endpoints that we’d hit with curl. Pretty ugly. After a few frustrating hours, I finally settled on a solution that seems to work pretty well for us. It uses Spring Boot, of course, as that’s our library of choice, plus JCommander for the argument handling. This is a pared-down example of how the application is structured. And because I care about of each you deeply, I’ll present it in Java AND Kotlin. :)

For those of you in a hurry, you can get the complete code in my GitHub repo. Everyone else, feel free to read along.

Hands-free Flyway and Jooq

Recently, I started working on a new project and I wanted to give Jooq a go. I also wanted to integrate Flyway: I wanted Jooq to generate its various classes based off the database schema, and I want to Flyway to create that schema. That’s all easy enough, but I’m resisting, right now, committing the generated classes to source control (to avoid the churn and additional maintenance), so how do I make that happen with as little work as possible? How do I make it work in a CI environment? Thanks to Maven, the answer is lots and lots of XML. :) Let’s take a look…​

Building Maps in Kotlin

Over the years, I’ve found myself processing a set of data and storing it in a Map, say, something like Map<Long, List<String>> (think something like a list of Room objects, keyed by a building id). I have found myself writing it something like this (in non-idiomatic Kotlin):

val foo = map.get(key)
if (foo == null) {
    foo = MutableList<String>()
    map.put(key, foo)
}
foo.add(bar)

Fortunately, the Kotlin standard library has a better way:

map.getOrPut(key) { mutableListOf() }
    .add(bar)

If key is not found, the lambda is run, adding the result to Map and return to use the value, new or otherwise, to which we add bar. Much more concise. :) Generally speaking, any time you can let the language/compiler do the work for you, you’re going to be better off.

Custom Methods in Spring Repositories

One of the great things about Spring Data Repositories is that they provide a number of query methods out of the box, with the ability to add additional queries simply by adding carefully named methods to the interface, and Spring generates the actual implementation for you. Sometimes, though, you do need to color outside the lines a bit. Thankfully, Spring allows us to do this. You just have to ask it nicely. Here’s now.

Restoring a Deleted Git Branch

Thanks to haste and some sloppy copy-and-paste, today I deleted the wrong remote Git branch. There’s nothing like learning in a panic, but that’s what happened. Here’s what I learned on how to fix that.

Best Advice I've Been Given: Check Your Ego

The Red Hat twitter account just asked this question: "Communities grow by uplifting others. What is one piece of advice you’d offer to a new developer? Reply below and then check out some advice from #RedHatter @somalley108." Here’s my input.

As a general rule, if you’re writing software, you’re probably pretty intelligent. That intelligence, while important, of course, can also become a hindrance. For example, I’ve been told by coworkers, "You may write buggy code, but I don’t" and "We don’t need techincal oversight". I think both of those statements are patently false, and they — and others like them — always take me back to that advice from my first manager, Chris Anderson, gave me way back in 1997:

Check your ego at the door.

It’s really easy to walk in to a coding review, a design session, or one of a myriad of other meetings with your ideas and think they’re the best. It’s also really easy to overlook the fact that other people in that room are also pretty intelligent — maybe even more so (it happens), and that we all have blindspots and weaknesses. In my experience, and I’ve certainly been on both sides more than I care to admit, many conflicts in software development are ego-driven. There’s a difference between being passionate about something and being an arrogant jerk. The latter type does nobody any good, so check your ego. Go in humbly, and see what there is to learn from your coworkers, even if you’re more senior than they. With that mindset, everyone will come out a winner.

Dear MockK, Repeat After Me

The project I’m working on is using Mockk in the unit tests. It’s a great library that has made true unit testing so much easier. I ran into a problem, though, where I needed a method I was mocking to return the value it was receiving. To be more specific, we were passing an object to a Spring repository method that had been built inside the method to test, and, to test thoroughly, I needed to get to that object. Turns out, that’s pretty easy to do with Mockk. Let’s take a look…​

Merry Christmas, 2019

Merry Christmas!

For God so loved the world, that He gave His only begotten Son, that whoever believes in Him shall not perish, but have eternal life. -- John 3:16

Testing Spring Repositories with Flyway

With my recent job change, I’ve gotten a chance to use Spring Boot in anger a bit. It’s been fun, and I’ve learned a fair bit about the current state of Spring (I still love you, Jakarta EE!). One of my tasks involved adding a query method to a repository, and I wanted to make sure the query worked before I pushed it upstream. To do that confidently, of course, required a unit test. In this post, I’ll show how remarkably simple it is to test Spring Repositories using Flyway to set up schemas and test data.

Quotes

Sample quote

Quote source