4

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.

4
  • 1
    If you have a answer that would be appreciated more than a snarky comment. Anyways, not sure if you are referring to the 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.
    – Chai
    Commented Apr 25, 2022 at 14:25
  • Well you are using Spring features but not using any of those in running your test, so that simply won't work. You are only using Mockito not Spring, also expecting to have Spring features on a Mockito managed instance isn't going to work either. So you need the SpringExtension and some configuration to test this. I actually do wonder what you want to test with this test in the first place.
    – M. Deinum
    Commented Apr 25, 2022 at 15:00
  • Right, well obviously this is not the actual test, but a minimal example of what is going wrong.
    – Chai
    Commented Apr 25, 2022 at 16:00
  • That is not so obvious imho, nonetheless the solution depends on what you want to test. With your current question it looks like you want to test the @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.
    – M. Deinum
    Commented Apr 26, 2022 at 5:28

2 Answers 2

5

You can use @SpringBootTest without running the whole application by passing it the class that contains the @Value but you have to use Spring's extension @ExtendWith({SpringExtension.class}) which is included inside @SpringBootTest and by that using Spring's MockBean instead of @Mock and @Autowired for autowiring the bean like this:

@SpringBootTest(classes = FeatureFlagService.class)
class FeatureFlagServiceTests {

  @MockBean
  RestTemplate restTemplate;

  @Autowired
  FeatureFlagService featureFlasgService;

  @Test
  void propertyTest(){
    Assertions.assertEquals(featureFlasgService.URL_FEATURE_FLAGS, "http://endpoint");
  }
1
  • Thx. Works great and I like that my test now has similar annotations to the class, makes it easy to see what is going on!
    – Chai
    Commented Apr 25, 2022 at 16:01
2

I recommend you to try this approach. Just need a slightly refactoring and add a package-private constructor to your FeatureFlagService.

FeatureFlagService.java

@Component
@Slf4j
public class FeatureFlagService {

    private final RestTemplate restTemplate;
    private final String URL_FEATURE_FLAGS;

    // package-private constructor. test-only
    FeatureFlagService(RestTemplate restTemplate, @Value("${url.feature_flags}") String flag) {
        this.restTemplate = restTemplate;
        this.URL_FEATURE_FLAGS = flag;
    }

    // do stuff
}

Then prepare your mocks and url, and inject them by the constructor-injection.

FeatureFlagServiceTests.java

public class FeatureFlagServiceTests {

    private FeatureFlagService featureFlagService;

    @Before
    public void setup() {
        RestTemplate restTemplate = mock(RestTemplate.class);
        // when(restTemplate)...then...
        String URL_FEATURE_FLAGS = "http://endpoint";
        featureFlagService = new FeatureFlagService(restTemplate, URL_FEATURE_FLAGS);
    }

    @Test
    public void propertyTest(){
        Assertions.assertEquals(featureFlasgService.getUrlFeatureFlags(), 
        "http://endpoint");
    }
}

The significant advantage is, your FeatureFlagServiceTests becomes very simple to read and trivial to test. You don't need magic annotation of Mockito anymore.

1
  • 1
    Thanks for the answer. I will keep this pattern in mind. I choose the other answer to go with for now, but it does seem like a good solution as well!
    – Chai
    Commented Apr 25, 2022 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.