package org.inferred.freebuilder;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class DefaultsOptimizationTest {

  @Rule
  public final ExpectedException thrown = ExpectedException.none();

  @Test
  public void testNoDefaultsNoOptimisation() throws NoSuchFieldException, SecurityException {
    RequiredPropertiesType.Builder.class.getSuperclass().getDeclaredField("_unsetProperties");
  }

  @Test
  public void testDefaultsOptimisation() throws NoSuchFieldException, SecurityException {
    thrown.expect(NoSuchFieldException.class);
    DefaultedPropertiesType.Builder.class.getSuperclass().getDeclaredField("_unsetProperties");
  }
}

@@ -27,32 +27,39 @@
 uilder;%0A%0Aimport 
+static 
 org.junit.Rule;%0A
@@ -56,13 +56,77 @@
 nit.
-Rule;
+Assert.assertEquals;%0A%0Aimport com.google.common.collect.ImmutableSet;%0A
 %0Aimp
@@ -149,275 +149,325 @@
 st;%0A
+%0A
 import 
-org.junit.rules.ExpectedException;%0A%0Apublic class DefaultsOptimizationTest %7B%0A%0A  @Rule%0A  public final ExpectedException thrown = ExpectedException.none();%0A%0A  @Test%0A  public void testNoDefaultsNoOptimisation() throws NoSuchFieldException, SecurityException %7B%0A    
+java.lang.reflect.Field;%0Aimport java.lang.reflect.Modifier;%0Aimport java.util.HashSet;%0Aimport java.util.Set;%0A%0Apublic class DefaultsOptimizationTest %7B%0A%0A  @Test%0A  public void testNoDefaultsNoOptimisation() %7B%0A    assertEquals(%0A        ImmutableSet.of(%22firstName%22, %22surname%22, %22_unsetProperties%22),%0A        fieldsOn(
 Requ
@@ -518,44 +518,9 @@
 ss()
-.getDeclaredField(%22_unsetProperties%22
+)
 );%0A 
@@ -576,198 +576,457 @@
 n() 
-throws NoSuchFieldException, SecurityException %7B%0A    thrown.expect(NoSuchFieldException.class);%0A    DefaultedPropertiesType.Builder.class.getSuperclass().getDeclaredField(%22_unsetProperties%22)
+%7B%0A    assertEquals(%0A        ImmutableSet.of(%22firstName%22, %22surname%22),%0A        fieldsOn(DefaultedPropertiesType.Builder.class.getSuperclass()));%0A  %7D%0A%0A  private static Set%3CString%3E fieldsOn(Class%3C?%3E cls) %7B%0A    Set%3CString%3E generatedFields = new HashSet%3CString%3E();%0A    for (Field field : cls.getDeclaredFields()) %7B%0A      if (!Modifier.isStatic(field.getModifiers())) %7B%0A        generatedFields.add(field.getName());%0A      %7D%0A    %7D%0A    return generatedFields
 ;%0A  

Make DefaultsOptimizationTest more robust