java - Providing context for @Value fields from a unit test -
i have class this:
@service("someclient") public class someclient { @value{some.value} private string somevalue; public void somemethod() { return somevalue; } }
and test this:
@contextconfiguration(locations = "classpath:/some/where/testapplicationcontext.xml") @runwith(springjunit4classrunner.class) public class someclienttest extends testcase { @value{some.value} private string somevaluetest; @test public void shouldwork() { ... someclient.somemethod() ... } }
when wider application running, field somevalue inside someclient class populated properties file referenced testapplicationcontext.xml. when run test in debug mode can see somevaluetest populated in test, when test calls class under test, value not populated.
i use advice! can change visibility of field in class, or provide setter, avoid if possible. if isn't, please advise.
in order populate fields @value annotation in test need configure propertysourcesplaceholderconfigurer
.
add following test:
@configuration public static class config { @bean public static propertysourcesplaceholderconfigurer propertyconfigurer() { return new propertysourcesplaceholderconfigurer(); } }
to read values test property file can add @testpropertysource(locations="classpath:test.properties")
test class declaration
Comments
Post a Comment