0

I've a problem in my JUNIT test with Spring boot : the @Value is not resolved. Here the code :

Spring boot config class :

@Configuration
@PropertySource(value="classpath:/config/parametrage-environnement.properties",name="env")
public class ExternalRessourceConfiguration {

    //To resolve ${} in @Value
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            return new PropertySourcesPlaceholderConfigurer();
        }
}

Class test :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
public class ConnexionEbicsResourceIntTest {

@Test
@Transactional
public void createConnexionEbics() throws Exception {

            restConnexionEbicsMockMvc.perform(post("/api/connexionEbicss")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(connexionEbicsDTO)))
            .andExpect(status().isCreated());

Java ressource :

@RestController
@RequestMapping("/api")
public class ConnexionEbicsResource {

    @Value("${env['connexion.proxy.host']}")
    //@Value("${connexion.proxy.host}") 
    public String protocol;

 @RequestMapping(value = "/connexionEbicss",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<ConnexionEbicsDTO> createConnexionEbics( @RequestBody ConnexionEbicsDTO connexionEbicsDTO) throws URISyntaxException {
        log.debug("REST request to save ConnexionEbics : {}", connexionEbicsDTO);
        String a = protocol;

}

In java ressource, when I run the test, "a" is null. The @Value was not resolved, why ? My spring boot configuration was ALL bootstraped.

The parametrage-environnement.properties file is located in both paths : src/main/resources/config and src/test/resources/config (copy/paste)

1 Answer 1

0

1) Since my controller is mocked, I can't use spring injection directly present in the controller (mocking annihilate Spring !)

2) Syntax

@Value("${env['connexion.proxy.host']}")

is wrong because, env is supposed to be a spring bean (@Bean). See here

So, With @PropertySource, we have to use @Value("${connexion.proxy.host}") syntax and don't forget the Resolver:

  //To resolve ${} in @Value
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
            return new PropertySourcesPlaceholderConfigurer();
        }

3) To set the protocol in the controller class, I need to inject it from the Test class :

 @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        ConnexionEbicsResource connexionEbicsResource = new ConnexionEbicsResource();
        ReflectionTestUtils.setField(connexionEbicsResource, "connexionEbicsService", connexionEbicsService);
        **ReflectionTestUtils.setField(connexionEbicsResource, "protocol", protocol);**
        ReflectionTestUtils.setField(connexionEbicsResource, "connexionEbicsMapper", connexionEbicsMapper);
        this.restConnexionEbicsMockMvc = MockMvcBuilders.standaloneSetup(connexionEbicsResource)
            .setCustomArgumentResolvers(pageableArgumentResolver)
            .setMessageConverters(jacksonMessageConverter).build();
    }

Having the loaded resource bundle in the test class :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
@IntegrationTest
@PropertySource(value="classpath:/config/parametrage-environnement.properties",name="env")
//@PropertySource("classpath:/config/parametrage-environnement.properties")
public class ConnexionEbicsResourceIntTest {
....
  @Value("${connexion.proxy.host}")
    public String protocol;

Thanks anyway

Sign up to request clarification or add additional context in comments.

1 Comment

You could avoid the use of ReflectionTestUtils if you used constructor injection

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.