简体   繁体   中英

How to use @Value in Junit tests

I'm trying to get a property value from my application-test.yml file like that:

@SpringBootTest()
@ActiveProfiles("test")
@ExtendWith(MockitoExtension.class)
public class GoogleServiceUtilsTest {
    @Value("${google.service.account.user}")
    private String serviceAccountUser;

    @Value("${google.service.account.path}")
    private String pathFile;

    Set<String> scopesSet = DirectoryScopes.all();
    List<String> scopesList = new ArrayList<String>(scopesSet);

    @Test
    void getCredentialTest() throws GoogleCredentialException {
        // Given
        GoogleCredentials credentials;

        // When
        credentials = GoogleServiceUtils.getCredential(serviceAccountUser, scopesList, pathFile);

        // Then
        assertThat(credentials != null);
        assertThat(new HttpCredentialsAdapter(credentials));
    }
}

but when I use it in my test method, serviceAccountUser and pathFile variables are always null.

My application-test.yml file is located in 'src/test/resources', and tests are in 'src/test/java/' and content:

google:
  service:
    account:
      user: ...
      path: ...

# FED credentials
fed:
  url: ...
  token: ...
  grantType: ...

# Logging
logging:
  level:
    root: ...
    org.springframework: ...

When I use the @Value tag in my app code, all is working. Variables are getting good values from 'src/main/resources/application.yml' file.

After reading comments, I add that the profiles are not useful in my case, but as when I don't use them, it doesn't work, I thought that maybe it came from there and that it is necessary to use them.

I also tried to write the value of the variables hard in the file, and then there is no problem, tests are passing well.

Is someone understanding this problem? Many people seem to have had the same, but I can't find an answer working for me.

Thank you for your answer!

Sorry, cannot reproduce!

Having:

  1. Simplest starter
  2. src/main/resources/application.yml
     foo: bar: baz: normal
    ...and
  3. src/test/resources/application-test.yml
     foo: bar: baz: test
  4. This test passes:
     @SpringBootTest @ActiveProfiles("test") //. class SomeTest { @Value("${foo.bar;baz}") String foo. @Test void testProp() { Assertions,assertEquals("test"; foo); //!# } }

Either @ExtendWith(MockitoExtension.class) has no effect/does no harm.


Whenever you wonder, where your properties come from

Consult: https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

And check (current version):

...considered in the following order:

  1. Default properties (specified by setting SpringApplication.setDefaultProperties ).

  2. @PropertySource annotations on your @Configuration classes. Please note...

  3. Config data (such as application.properties files).

  4. A RandomValuePropertySource that has properties only in random.* .

  5. OS environment variables.

  6. Java System properties ( System.getProperties() ).

  7. JNDI attributes from java:comp/env .

  8. ServletContext init parameters.

  9. ServletConfig init parameters.

  10. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).

  11. Command line arguments.

  12. properties attribute on your tests. Available on @SpringBootTest and the test annotations for testing a particular slice of your application.

  13. @TestPropertySource annotations on your tests.

  14. Devtools global settings properties in the $HOME/.config/spring-boot directory when devtools is active.


Config data files (3.) are considered in the following order:

  1. Application properties packaged inside your jar ( application.properties and YAML variants).

  2. Profile-specific application properties packaged inside your jar ( application-{profile}.properties and YAML variants).

  3. Application properties outside of your packaged jar ( application.properties and YAML variants).

  4. Profile-specific application properties outside of your packaged jar ( application-{profile}.properties and YAML variants).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM