Credentials implements Serializable { @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE). append("userName", userName != null ? "*****" : "<null>"). append("password", password != null ? "*****" : "<null>"). toString(); } Credentials(final String userName, final String password); String getUserName(); String getPassword(); boolean isEmpty(); @Override String toString(); static final Credentials EMPTY; }
@Test public void testToString() throws Exception { String testString = "Credentials[" + "userName=*****," + "password=*****]"; Credentials credentials = new Credentials("uid", "pwd"); assertEquals(testString, credentials.toString()); testString = "Credentials[" + "userName=<null>," + "password=*****]"; credentials = new Credentials(null, "pwd"); assertEquals(testString, credentials.toString()); testString = "Credentials[" + "userName=<null>," + "password=<null>]"; credentials = new Credentials(null, null); assertEquals(testString, credentials.toString()); }