Skip to content

Instantly share code, notes, and snippets.

@fmbenhassine
Created March 1, 2020 21:28
Show Gist options
  • Save fmbenhassine/5b52d8784b9ee054cf08bbcaabf23591 to your computer and use it in GitHub Desktop.
Save fmbenhassine/5b52d8784b9ee054cf08bbcaabf23591 to your computer and use it in GitHub Desktop.
Custom Apache beanutils converter with EasyProps
import java.util.Date;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.jeasy.props.annotations.SystemProperty;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.jeasy.props.PropertiesInjectorBuilder.aNewPropertiesInjector;
public class TestCustomApacheConverter {
@Before
public void setUp() {
System.setProperty("date", "01/03/2020");
}
@Test
public void testCustomApacheConverter() { // passes with v2.0.1
// given
MyBean myBean = new MyBean();
DateConverter converter = new DateConverter();
converter.setPattern("dd/mm/yyyy");
ConvertUtils.register(converter, Date.class);
//when
aNewPropertiesInjector().injectProperties(myBean);
// then
assertThat(myBean.getDate()).isNotNull();
}
public static class MyBean {
@SystemProperty("date")
private Date date;
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment