Chapter 1: New Language Features

Inline Short Files in Tests

class SupplyTests {

  @Test
  void updateBarcode() throws JSONException {
    var supply = new Supply("1234", 5);

    var actualJson = supply.toJson();

    var expectedJson = read("/supply.json");
    assertEquals(expectedJson, actualJson, STRICT);
  }

}
  • uses JSONAssert
  • is a JUnit 5 unit test
  • read file from classpath (part of src/test/resources)
  • expect output not directly visible, one click away
class SupplyTests {

    @Test
    void updateBarcode() throws JSONException {
        var supply = new Supply("1234", 5);

        var actualJson = supply.toJson();

        var expectedJson = """
                {
                  "id": "1234", 
                  count: 5
                }""";
        assertEquals(expectedJson, actualJson, STRICT);
    }
}
  • expected output directly visible
  • makes use of text block
  • templating easy with formatted ???