AuthorizationResponse { @Override public String toString() { try { final StringBuilder sb = new StringBuilder(); sb.append(RESPONSE_CODE).append('=').append(URLEncoder.encode(code, UTF_8)); if (state != null) { sb.append('&'); sb.append(RESPONSE_STATE).append('=').append(URLEncoder.encode(state, UTF_8)); } return sb.toString(); } catch (final UnsupportedEncodingException e) { throw new Error(e); } } AuthorizationResponse(final String code, final String state); String getCode(); String getState(); @Override String toString(); static AuthorizationResponse fromString(final String s); static AuthorizationResponse fromString(final String s, final String potentialDescription);  }
@Test public void toString_withState() throws Exception { final AuthorizationResponse cut = new AuthorizationResponse("red", "green"); final String actual = cut.toString(); Assert.assertEquals("code=red&state=green", actual); }
@Test public void toString_withoutState() throws Exception { final AuthorizationResponse cut = new AuthorizationResponse("red", null); final String actual = cut.toString(); Assert.assertEquals("code=red", actual); }