Ensure difference between sets An endpoint that my application interacts with allows you to specify which fields are returned in the JSON result. My application will then map the JSON to a POJO using Jackson. Quite often I add more fields in one place and forget to add it in the other which results in problems. I wrote a unit test to help make the process easier but I feel like there could be room for improvements.

Here's what I have so far:

private static final String[] POTENTIAL_FIELDS = new String[] { "field1", "field2" };

@Test
public void checkFields() {
    // get all constant fields
    Set&lt;String&gt; constantFields = Arrays.stream(POTENTIAL_FIELDS)
            .map(String::toLowerCase)
            .collect(Collectors.toSet());

    // get all private fields
    Set&lt;String&gt; reflectFields = Arrays.stream(FieldsModel.class.getDeclaredFields())
            .filter((field) -&gt; !Modifier.isStatic(field.getModifiers()))
            .map(field -&gt; field.getName().toLowerCase())
            .collect(Collectors.toSet());

    Set&lt;String&gt; symmetricDifference = Sets.symmetricDifference(constantFields, reflectFields);

    // check if there's a difference
    if (!symmetricDifference.isEmpty()) {
        Assert.fail(String.format("Difference: %s\n", String.join(",", symmetricDifference)));
    }
}

private static class FieldsModel {
    private int field1;
    private String field2;
}


The only non-standard imports are FieldsModel which is what the name suggests, and Sets which is from Guava (com.google.coom.collect.Sets). Here is a direct link to the method I used.
On the whole, I think this is a pretty neat solution to your problem.  A couple of observations...

case == CASE

You lower case everything.  Maybe this is exactly what you're after, but to me, fieldOne and fieldone aren't the same, so I'd want to know about that difference.

Developer time vs test time

symmetricDifference gives back a set of all of the differences, it doesn't discriminate between the left and the right side.  So, if you've added something to FieldsModel and something else to POTENTIAL_FIELDS they would both come back.  The developer then has to go and look at both and reconcile where the problem is.  In reality this might not be a problem because the developer has probably worked on one of the files recently, so would realise which one they'd missed, however it would be better if the test showed what was missing from where.  You could do something like this:

String inConstantButNotModel = constantFields.stream()
        .filter(i -&gt; !reflectFields.contains(i))
        .collect(Collectors.joining(","));
String inModelButNotConstant = reflectFields.stream()
        .filter(i -&gt; !constantFields.contains(i))
        .collect(Collectors.joining(","));

assertThat(String.format("ConstantsMissing: %s\nModelMissing: %s\n",
        inModelButNotConstant,
        inConstantButNotModel))
        .isEqualTo("ConstantsMissing: \nModelMissing: \n");


To give output along the lines of...

ConstantsMissing: field5,field4
ModelMissing: field3