UserForm { @Override public String toString() { return "UserForm{" + "id=" + id + ", email='" + email + '\'' + ", displayName='" + displayName + '\'' + ", password='XXXXX'" + ", password2='XXXXX'" + ", userRole=" + userRole + '}'; }  Long getId(); void setId(final Long id); String getEmail(); void setEmail(final String email); String getDisplayName(); void setDisplayName(final String displayName); String getPassword(); void setPassword(final String password); String getPassword2(); void setPassword2(final String password2); UserRole getUserRole(); void setUserRole(final UserRole userRole); boolean exists(); @Override String toString();  }
@Test public void testToString() throws IllegalAccessException, NoSuchFieldException { final String expectedSecret1 = "MySuperSecretKey"; final String expectedSecret2 = "AnotherSecret"; final UserForm userForm = new UserForm(); final Field field1 = userForm.getClass().getDeclaredField("password"); field1.setAccessible(true); field1.set(userForm, expectedSecret1); final Field field2 = userForm.getClass().getDeclaredField("password2"); field2.setAccessible(true); field2.set(userForm, expectedSecret1); final String result = userForm.toString(); assertFalse("Should not contain our sensitive field", result.contains(expectedSecret1)); assertFalse("Should not contain our sensitive field", result.contains(expectedSecret2)); }