ResponseBeanWriter { public byte[] toJSON(Object bean) { if (bean instanceof Optional) { Optional<?> optional = (Optional<?>) bean; if (optional.isEmpty()) return Strings.bytes("null"); Object value = optional.get(); Context<Object> context = ContextHelper.context(this.context, value.getClass()); context.validator.validate(value, false); return context.writer.toJSON(value); } else { Context<Object> context = ContextHelper.context(this.context, bean.getClass()); context.validator.validate(bean, false); return context.writer.toJSON(bean); } } ResponseBeanWriter(); void register(Type responseType, BeanClassValidator validator); boolean contains(Class<?> beanClass); byte[] toJSON(Object bean);  }
@Test void validateList() { List<TestBean> list = Lists.newArrayList(); assertThatThrownBy(() -> writer.toJSON(list)) .isInstanceOf(Error.class) .hasMessageContaining("bean class must not be java built-in class"); }
@Test void toJSONWithEmptyOptional() { Optional<TestBean> optional = Optional.empty(); byte[] bytes = writer.toJSON(optional); assertThat(new String(bytes, StandardCharsets.UTF_8)).isEqualTo("null"); }
@Test void toJSONWithOptional() { var bean = new TestBean(); bean.intField = 5; Optional<TestBean> optional = Optional.of(bean); byte[] bytes = writer.toJSON(optional); assertThat(bytes).isNotEmpty(); }
@Test void toJSON() { var bean = new TestBean(); bean.intField = 5; byte[] bytes = writer.toJSON(bean); assertThat(bytes).isNotEmpty(); }
@Test void toJSONWithValidationError() { assertThatThrownBy(() -> writer.toJSON(new TestBean())) .isInstanceOf(ValidationException.class); }