BigDecimalConverter implements PropertyConverter<BigDecimal> { @Override public BigDecimal convert(String value, ConversionContext ctx) { ctx.addSupportedFormats(getClass(), "<bigDecimal> -> new BigDecimal(String)"); if(value==null){ return null; } String trimmed = value.trim(); try{ return new BigDecimal(trimmed); } catch(Exception e){ LOG.finest("Parsing BigDecimal failed, trying BigInteger for: " + value); BigInteger bigInt = integerConverter.convert(value, ctx); if(bigInt!=null){ return new BigDecimal(bigInt); } LOG.finest("Failed to parse BigDecimal from: " + value); return null; } }  @Override BigDecimal convert(String value, ConversionContext ctx); @Override boolean equals(Object o); @Override int hashCode();  }
@Test public void converterHandlesNullValueCorrectly() throws Exception { ConversionContext context = mock(ConversionContext.class); BigDecimalConverter converter = new BigDecimalConverter(); BigDecimal value = converter.convert("", context); assertThat(value).isNull(); }
@Test public void callToConvertAddsMoreSupportedFormatsToTheContext() throws Exception { ConversionContext context = mock(ConversionContext.class); BigDecimalConverter converter = new BigDecimalConverter(); BigDecimal value = converter.convert("", context); assertThat(value).isNull(); verify(context).addSupportedFormats(BigDecimalConverter.class, "<bigDecimal> -> new BigDecimal(String)"); }