### Question:
DirectMessageTemplate extends AbstractTwitterOperations implements DirectMessageOperations { public DirectMessage sendDirectMessage(String toScreenName, String text) { requireUserAuthorization(); MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>(); data.add("screen_name", String.valueOf(toScreenName)); data.add("text", text); return restTemplate.postForObject(buildUri("direct_messages/new.json"), data, DirectMessage.class); } DirectMessageTemplate(RestTemplate restTemplate, boolean isAuthorizedForUser, boolean isAuthorizedForApp); List<DirectMessage> getDirectMessagesReceived(); List<DirectMessage> getDirectMessagesReceived(int page, int pageSize); List<DirectMessage> getDirectMessagesReceived(int page, int pageSize, long sinceId, long maxId); List<DirectMessage> getDirectMessagesSent(); List<DirectMessage> getDirectMessagesSent(int page, int pageSize); List<DirectMessage> getDirectMessagesSent(int page, int pageSize, long sinceId, long maxId); DirectMessage getDirectMessage(long id); DirectMessage sendDirectMessage(String toScreenName, String text); DirectMessage sendDirectMessage(long toUserId, String text); void deleteDirectMessage(long messageId);  }### Answer:
@Test public void sendDirectMessage_toScreenName() { mockServer.expect(requestTo("https: .andExpect(content().string("screen_name=habuma&text=Hello+there%21")) .andRespond(withSuccess(jsonResource("directMessage"), APPLICATION_JSON)); DirectMessage message = twitter.directMessageOperations().sendDirectMessage("habuma", "Hello there!"); assertSingleDirectMessage(message); mockServer.verify(); }
@Test(expected = MessageTooLongException.class) public void sendDirectMessage_toScreenName_tooLong() { mockServer.expect(requestTo("https: .andExpect(content().string("screen_name=habuma&text=Really+long+message")) .andRespond(withStatus(FORBIDDEN).body("{\"error\":\"There was an error sending your message: The text of your direct message is over 140 characters.\"}").contentType(APPLICATION_JSON)); twitter.directMessageOperations().sendDirectMessage("habuma", "Really long message"); mockServer.verify(); }
@Test public void sendDirectMessage_toUserId() { mockServer.expect(requestTo("https: .andExpect(content().string("user_id=11223&text=Hello+there%21")) .andRespond(withSuccess(jsonResource("directMessage"), APPLICATION_JSON)); DirectMessage message = twitter.directMessageOperations().sendDirectMessage(11223, "Hello there!"); assertSingleDirectMessage(message); mockServer.verify(); }