### Question:
RuleToXmlConverter implements RuleConverter { @Override public String marshal(Rule rule) throws RuleConversionException { try { RuleSer ruleSer = toXmlConverter.map(rule); StringWriter sw = new StringWriter(); createMarshaller().marshal(objectFactory.createRule(ruleSer), sw); return sw.toString(); } catch (Exception e) { Attribute ruleNameAttribute = rule.getAttribute("ruleName"); String ruleName = ruleNameAttribute != null ? ruleNameAttribute.getValue().toString() : null; throw new RuleConversionException(String.format("Rule cannot be converted to XML:\n%s", ruleName), e); } } private  RuleToXmlConverter(Class<?>... extraClasses); static RuleToXmlConverter getInstance(Class<?>... extraClasses); @Override String marshal(Rule rule); @Override Rule unmarshal(String value);  }### Answer:
@Test void shouldMarshallValidRuleWithBuildInObjectTypes() { Rule ruleObject = toRuleConverter.map(TestRuleFactory.constructValidRuleWithBuildInObjectTypes()); String ruleAsString = converter.marshal(ruleObject); assertThat(ruleAsString).isXmlEqualTo(validXmlRuleWithBuildInObjectTypes); }
@Test void shouldMarshallValidRuleWithCustomObjectTypes() { Rule ruleObject = toRuleConverter.map(TestRuleFactory.constructValidRuleWithCustomObjectTypes()); String ruleAsString = converter.marshal(ruleObject); assertThat(ruleAsString).isXmlEqualTo(validXmlRuleWithCustomObjectTypes); }
@Test void shouldMarshallValidRuleWithNullUndefinedValue() { Rule ruleObject = toRuleConverter.map(TestRuleFactory.constructValidRuleWithNullUndefinedValue()); String ruleAsString = converter.marshal(ruleObject); assertThat(ruleAsString).isXmlEqualTo(validXmlRuleWithNullUndefinedValue); }
@Test void shouldMarshallValidRuleWithNullStringValue() { Rule ruleObject = toRuleConverter.map(TestRuleFactory.constructValidRuleWithNullStringValue()); String ruleAsString = converter.marshal(ruleObject); assertThat(ruleAsString).isXmlEqualTo(validXmlRuleWithNullStringValue); }
@Test void shouldMarshallRuleConsistentWithSchema() { Rule ruleObject = toRuleConverter.map(TestRuleFactory.constructValidRuleWithCustomObjectTypes()); String ruleAsString = converter.marshal(ruleObject); assertThat(ruleAsString).isXmlEqualTo(validXmlRuleWithCustomObjectTypes); }
@Test void shouldNotMarshallRuleInconsistentWithSchema() { Rule ruleObject = TestRuleFactory.constructInvalidRule(); assertThatThrownBy(() -> converter.marshal(ruleObject)) .isInstanceOf(RuleConversionException.class).hasCauseExactlyInstanceOf(MarshalException.class); }