title: How can I use env variables with Spring Boot Test?
body: <p>In my Spring Boot Security application, I have stored my secret at file application.properties and it works just fine.I can get my secret key like this:</p>
  <pre class="lang-java prettyprint-override"><code>@Value(&quot;${secret}&quot;)
  private String secret;
  </code></pre>
  <p>But in order to testing it and <em>don't hard code the secret password on my test</em>, I need to generate my token and put it on my Authorization header (using mockMvc), but I can't access my secret env variable from test class, even if I insert my env variable at application.properties file of test paths.</p>
  <p><strong>How I'm generating token for my tests:</strong></p>
  <pre class="lang-java prettyprint-override"><code>public class TokenForTest {
      public static String generateTokenForTest() {
          Date today = new Date();
          Date expireDate = new Date(today.getTime() + Long.parseLong(&quot;86400000&quot;));
          return &quot;Bearer &quot; + Jwts.builder()
                  .setIssuer(&quot;BeatSound API&quot;)
                  .setSubject(&quot;1&quot;)
                  .setIssuedAt(today)
                  .setExpiration(expireDate)
                  .signWith(SignatureAlgorithm.HS256, &quot;secretKey&quot;)
                  .compact();
      }
  }
  </code></pre>
  <p><strong>How I'm doing my tests:</strong></p>
  <pre class="lang-java prettyprint-override"><code> @Test
          void shouldReturnAllUsers() throws Exception {
                  String token = TokenForTest.generateTokenForTest();
                  this.mockMvc.perform(get(&quot;/api/v1/user&quot;)
                          .header(&quot;Authorization&quot;, token))
  .andExpect(content().contentType(MediaType.APPLICATION_JSON))
                          .andExpect(status().isOk()).andDo(print())
                          .andExpect(jsonPath(&quot;$.[*].id&quot;).exists())
                          .andExpect(jsonPath(&quot;$.[*].name&quot;).exists())
                          .andExpect(jsonPath(&quot;$.[*].email&quot;).exists())
                          .andExpect(jsonPath(&quot;$.[*].password&quot;).exists())
          }
  </code></pre>