MessageJsonMapper { public Message transformMessage(String messageJsonResponse) { return jsonSerializer.fromJson(messageJsonResponse, Message.class); } @Inject  MessageJsonMapper(JsonSerializer jsonSerializer); Message transformMessage(String messageJsonResponse); String transformMessage(Message message);  }
@Test public void shouldProperlyDeserializeJsonString() throws Exception { String someJson = "any value"; Message mockMessage = mock(Message.class); when(jsonSerializer.fromJson(someJson, Message.class)).thenReturn(mockMessage); Message message = messageJsonMapper.transformMessage(someJson); assertThat(message).isNotNull(); assertThat(message).isInstanceOf(Message.class); verify(jsonSerializer, times(1)).fromJson(someJson, Message.class); }
@Test public void shouldProperlySerializeMessage() throws Exception { ErrorMessage errorMessage = new ErrorMessage.Builder().with(404).and("Not found!").build(); String fakeSerializedJson = "{status:fail, message:not found!}"; when(jsonSerializer.toJson(errorMessage)).thenReturn(fakeSerializedJson); String message = messageJsonMapper.transformMessage(errorMessage); assertThat(message).isNotNull(); assertThat(message.length()).isEqualTo(fakeSerializedJson.length()); }