Source:
<code>public void shouldReplaceUsernameInPollRequest() throws NullPointerException, IOException { String test = "/shouldReplaceUsernameInPollRequest/test"; String encodedPattern = URLEncoder.encode( PostBasedAuthenticationMethod.MSG_USER_PATTERN, StandardCharsets.UTF_8.name()); String pollUrl = "/shouldReplaceUsernameInPollRequest/pollUrl"; String pollData = "user=" + PostBasedAuthenticationMethod.MSG_USER_PATTERN; String username = "user"; final List<String> orderedReqUrls = new ArrayList<>(); final List<String> orderedReqData = new ArrayList<>(); this.nano.addHandler( new NanoServerHandler(pollUrl.replace(encodedPattern, username)) { @Override protected Response serve(IHTTPSession session) { orderedReqUrls.add( session.getUri() + "?" + session.getQueryParameterString()); HashMap<String, String> map = new HashMap<String, String>(); try { session.parseBody(map); orderedReqData.add(map.get("postData")); } catch (Exception e) { } return newFixedLengthResponse(LOGGED_IN_BODY); } }); HttpMessage testMsg = this.getHttpMessage(test); HttpMessage pollMsg = this.getHttpMessage(pollUrl + "?" + encodedPattern); method.setPollUrl(pollMsg.getRequestHeader().getURI().toString()); method.setPollData(pollData); <START> User user = spy(new User(0, "user")); user.setAuthenticationCredentials( new UsernamePasswordAuthenticationCredentials(username, "")); doReturn(mockedContext).when(user).getContext(); <END> assertThat(method.isAuthenticated(testMsg, user), is(true)); assertThat(orderedReqUrls.size(), is(1)); assertThat(orderedReqUrls.get(0), is(pollUrl + "?" + username)); assertThat(orderedReqData.size(), is(1)); assertThat( orderedReqData.get(0), is(pollData.replace(PostBasedAuthenticationMethod.MSG_USER_PATTERN, username))); }</code><technical_language>mock (instead of instantiate spy), e.g.: Java User user = mock(User.class); given(user.getAuthenticationCredentials()).willReturn(new UsernamePasswordAuthenticationCredentials(...)); given(user.getContext()).willReturn(mockedContext);</technical_language>

Target:
public void shouldReplaceUsernameInPollRequest() throws NullPointerException, IOException { String test = "/shouldReplaceUsernameInPollRequest/test"; String encodedPattern = URLEncoder.encode( PostBasedAuthenticationMethod.MSG_USER_PATTERN, StandardCharsets.UTF_8.name()); String pollUrl = "/shouldReplaceUsernameInPollRequest/pollUrl"; String pollData = "user=" + PostBasedAuthenticationMethod.MSG_USER_PATTERN; String username = "user"; final List<String> orderedReqUrls = new ArrayList<>(); final List<String> orderedReqData = new ArrayList<>(); this.nano.addHandler( new NanoServerHandler(pollUrl.replace(encodedPattern, username)) { @Override protected Response serve(IHTTPSession session) { orderedReqUrls.add( session.getUri() + "?" + session.getQueryParameterString()); HashMap<String, String> map = new HashMap<String, String>(); try { session.parseBody(map); orderedReqData.add(map.get("postData")); } catch (Exception e) { } return newFixedLengthResponse(LOGGED_IN_BODY); } }); HttpMessage testMsg = this.getHttpMessage(test); HttpMessage pollMsg = this.getHttpMessage(pollUrl + "?" + encodedPattern); method.setPollUrl(pollMsg.getRequestHeader().getURI().toString()); method.setPollData(pollData); User user = mock(User.class); given(user.getAuthenticationCredentials()) .willReturn(new UsernamePasswordAuthenticationCredentials(username, "")); given(user.getContext()).willReturn(mockedContext); assertThat(method.isAuthenticated(testMsg, user), is(true)); assertThat(orderedReqUrls.size(), is(1)); assertThat(orderedReqUrls.get(0), is(pollUrl + "?" + username)); assertThat(orderedReqData.size(), is(1)); assertThat( orderedReqData.get(0), is(pollData.replace(PostBasedAuthenticationMethod.MSG_USER_PATTERN, username))); }