jparams / to-string-verifier

To String Verifier provides an easy and convenient way to test the toString method on your class.
MIT License
32 stars 6 forks source link

Test does not fail when excluded field has a different label in toString #23

Open danielFesenmeyer opened 5 years ago

danielFesenmeyer commented 5 years ago

Let's assume I have a field named "password", but it is labeled as "pwd" in the toString() method. I would expect that the following test should fail, but it passes. Maybe this could be fixed by failing if the test contains any unexpected fields? (I did not find an option for this.)

import org.junit.Test;

import com.jparams.verifier.tostring.NameStyle;
import com.jparams.verifier.tostring.ToStringVerifier;

public class PasswordWithWrongFieldLabelShouldThrowTest
{

   private static class ClassWithPassword
   {
      private String password;

      @Override
      public String toString()
      {
         return "ClassWithPassword{" + "pwd=" + password + '}';
      }
   }

   @Test
   public void testToString() {
      ToStringVerifier.forClass(ClassWithPassword.class)
         .withClassName(NameStyle.SIMPLE_NAME)
         .withIgnoredFields("password")
         .withFailOnExcludedFields(true)
         .verify();
   }

}