multiple ways of using soft asserts in junit 5

Do you think this test passes?

    @Test
    void mystery() {
        var softly = new SoftAssertions();
        softly.assertThat("robot").isEqualTo("izzy");
        softly.assertThat(126).isLessThanOrEqualTo(125);
    }

It does! Since softly is never told that it is done, the test does not fail. I’ve been using the approach of calling assertAll() to get my tests to fail:

    @Test
    void callingAssertAll() {
        var softly = new SoftAssertions();
        softly.assertThat("robot").isEqualTo("izzy");
        softly.assertThat(126).isLessThanOrEqualTo(125);
        softly.assertAll();
    }

I learned there are a few other approaches today. One is using an autocloseable version. This one is great if you are using a local variable:

    @Test
    void autoclosable() {
        try (var softly = new AutoCloseableSoftAssertions()) {
            softly.assertThat("robot").isEqualTo("izzy");
            softly.assertThat(126).isLessThanOrEqualTo(125);
        }
    }

Alternatively, you can use the lambda version. I like the autoclosable one better as it doesn’t encourage cramming stuff in a lambda. I could call another method inside assertSoftly with the actual asserts but that doesn’t seem better than using try with resources.

    @Test
    void lambda() {
        SoftAssertions.assertSoftly(s -> {
            s.assertThat("robot").isEqualTo("izzy");
            s.assertThat(126).isLessThanOrEqualTo(125);
        });
    }

There’s another way with an extension (that comes with asserj-core). I like this approach as it uses an instance variable and doesn’t require figuring out a place to call assertAll

@ExtendWith(SoftAssertionsExtension.class)
class SoftAssertionsExtensionTest {

    @InjectSoftAssertions
    SoftAssertions softly;

    @Test
    void field() {
        softly.assertThat("robot").isEqualTo("izzy");
        softly.assertThat(126).isLessThanOrEqualTo(125);
    }
}

The same can be done with a method. It’s nice to have choices!

@ExtendWith(SoftAssertionsExtension.class)
class SoftAssertionsExtensionTest {

    @Test
    void parameter(SoftAssertions softly) {
        softly.assertThat("robot").isEqualTo("izzy");
        softly.assertThat(126).isLessThanOrEqualTo(125);
    }

}

gson supports records

I like using records in Java and getting rid of the boilerplate on my immutable objects. I also like using gson (google’s serialization/deserialization library) for my lightweight JSON needs. All I need is an annotation to use a different field type. (For complex parsing I use Jackson)

Suppose I want parse this

var json = """
   {"first":"Mickey",
	"last":"Mouse",
	"birth_year":1928}
	""";

For example before records, I would write:

class Name {
	 private String first;
	 private String last;
     @SerializedName("birth_year") int birthYear;

     // boilerplate methods
}

I needed to use my IDE to generate a constructor, getters, and a toString(). And additionally, equals/hashCode if needed for that class.

In Java 17, I switched to records. I didn’t realize I could use them with gson annotations until recently. Turns out all I need to write is:

record Name(String first, 
   String last, 
   @SerializedName("birth_year") int birthYear) { }

The parsing code remains the same

var gson = new Gson();
var name = gson.fromJson(json, Name.class);
System.out.println(name);

FTC Judge vs Judge Advisor

I’ve been judging FTC (FIRST Tech Challenge) competitions for many years. It’s a lot of fun. The kids tell you about their robot and outreach and learnings and more. You also negotiate with other judges to determine the award winners. And the day ends with some super excited kids finding out the result. It’s a great day.

There’s also a judge advisor role. The description includes

The Judge Advisor coordinates the judging process, which includes facilitating group deliberation sessions, ensuring the award decisions are made, and the awards script is written. Sometimes the Judge Advisor trains judges and can help in scheduling the judging interviews. The Judges, Judge Advisor Assistant, and Judge Match Observers look to the Judge Advisor for training materials, schedules, and other general questions throughout the
event.

I was asked a few years ago about being Judge Advisor and passed. I like judging a lot. The judge advisor doesn’t get to interview the teams or make any decisions/negotiate. They are a manager and facilitator.

I got to the event on Saturday and was greeted with the event coordinators telling me their judge advisor couldn’t come unexpectedly and could I do it. I said yes. While it isn’t my first choice of role, I am qualified to. I was also the most experienced judge in the room (by a good amount.) I also appreciate that they asked/told me in the form of a question. If they had asked me two weeks ago, I’d have said no because there was still time to find someone more excited about the role. Day of, choices are limited!

What’s interesting about the combination of managing and facilitating is that I very much like facilitating and very much dislike managing. I was able to train folks, form groups, enforce standards and keep things on track.

I definitely relied on crowd sourcing. I wrote constraints on the board and explained what I was trying to accomplish for groups. Having a dozen people “check your work” real time is great quality control!

Overall, it was fine. We accomplished what we needed to and the judges/event staff were happy with how things went. But I still greatly prefer judging and will continue signing up as a judge for future events!