I am trying to get some junit tests with Mockito to work in a SprinBoot application.
Now my Service has some variable that gets filled from the application.properties
by means of @Value
annotation:
@Component
@Slf4j
public class FeatureFlagService {
@Autowired
RestTemplate restTemplate;
@Value("${url.feature_flags}")
String URL_FEATURE_FLAGS;
// do stuff
}
I am trying to test this by using TestPropertySource
like so:
@ExtendWith(MockitoExtension.class)
@TestPropertySource(properties = { "${url.feature_flags} = http://endpoint" })
class FeatureFlagServiceTests {
@Mock
RestTemplate restTemplate;
@InjectMocks
FeatureFlagService featureFlasgService;
@Test
void propertyTest(){
Assertions.assertEquals(featureFlasgService.URL_FEATURE_FLAGS, "http://endpoint");
}
However the property does not get filled and remains null
.
There are a bunch of tpoics on this, but I have not been able to piece together a solution. I saw solutions suggesting @SpringBootTest
, but then it seems to want to do an integration test, spinning up the service, which fails because it can not connect to DB. So that is not what I am looking for.
I also saw solutions suggesting I make a PropertySourcesPlaceholderConfigurer
bean.
I tried that by putting :
@Bean
public static PropertySourcesPlaceholderConfigurer propertiesResolver() {
return new PropertySourcesPlaceholderConfigurer();
}
In my Application.java
. But that is not working / not enough. I am not sure if I was supposed to do that differently, or if there is more there that I do not know.
Please advice.
ExtendsWith
annotation. In case you do I also tried:@ExtendWith(SpringExtension.class)
. That gives me a truck load of debug lines and the test fails just the same. So yeah, any suggestions are welcome.SpringExtension
and some configuration to test this. I actually do wonder what you want to test with this test in the first place.@Value
resolution from Spring and not some functional part of your application. So the way you ask a question and the code you provide is heavily influencing the answer you will get.