Created
March 1, 2020 21:28
-
-
Save fmbenhassine/5b52d8784b9ee054cf08bbcaabf23591 to your computer and use it in GitHub Desktop.
Custom Apache beanutils converter with EasyProps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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