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:
foo: bar: baz: normal
...and foo: bar: baz: test
@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.
And check (current version):
...considered in the following order:
Default properties (specified by setting
SpringApplication.setDefaultProperties
).
@PropertySource
annotations on your@Configuration
classes. Please note...Config data (such as
application.properties
files).A
RandomValuePropertySource
that has properties only inrandom.*
.OS environment variables.
Java System properties (
System.getProperties()
).JNDI attributes from
java:comp/env
.
ServletContext
init parameters.
ServletConfig
init parameters.Properties from
SPRING_APPLICATION_JSON
(inline JSON embedded in an environment variable or system property).Command line arguments.
properties
attribute on your tests. Available on@SpringBootTest
and the test annotations for testing a particular slice of your application.
@TestPropertySource
annotations on your tests.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:
Application properties packaged inside your jar (
application.properties
and YAML variants).Profile-specific application properties packaged inside your jar (
application-{profile}.properties
and YAML variants).Application properties outside of your packaged jar (
application.properties
and YAML variants).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.