I have a spring mvc application. There is a class A as shown below:
public class A extends X {
private final RetryRegistry retryReg;
@value("${retry.attempts:5}")
private int retryAttempts;
@value("${retry.delay:5000}")
private int retryDelay;
public A( B b, C c, D d){
super(b,c,d);
this.retryReg = RetryRegistry.of(RetryConfig.custom()
.maxAttempts(this.retryAttempts)
.waitDuration(Duration.ofMillis(this.retryDelay))
.build());
}
.......
}
The test class for class A is as shown below:
@PrepareForTest(A.class)
@runWith(PowerMockRunner.class)
@SupressWarnings("unchecked")
public class ATest {
@InjectMocks
private A a;
........
}
Now when test class injects mock and calls A's constructor, the value for retryAttempts is 0 inside the constructor, hence an exception is thrown.
It seems that the configurable properties (from the properties file whose path has been mentioned in servlet-context are no being read) when inject mock tries to construct A.
I tried many things like adding @TestPropertySource to my test file, but it doesn't work.
@TestPropertySource(locations="classpath:SettingsTest.properties")
@PrepareForTest(A.class)
@runWith(PowerMockRunner.class)
@SupressWarnings("unchecked")
public class ATest {
@InjectMocks
private A a;
........
}
Am I doing something wrong, or is @TestPropertySource is only supported for Springboot apps. Any suggestions on how I can set the configurable properties before @InjectMocks tries to access class A'constructor.