In my Spring Boot project with IntelliJ IDEA Community, I'm creating integration tests with JUnit and Mockito, I want to set env variables globally because tests are many execution points, and in IntelliJ Community just I can set they for each run configuration. I'm using GNU/Linux, I set the env variables in my .bashrc and source the file, I define they in /test/resources/application-test.yml. When I run the tests throws an exception because it doesn't get the DB credentials, in this case, from the env variables. How it can read the values defined in .bashrc?
.bashrc
# Obviously I setted the values, are empty just to show you
export DB_URL_TEST=""
export DB_USERNAME_TEST=""
export DB_PASSWORD_TEST=""
/test/resources/application-test.yml
spring:
datasource:
url: "${DB_URL_TEST}"
username: "${DB_USERNAME_TEST}"
password: "${DB_PASSWORD_TEST}"
driver-class-name: org.mariadb.jdbc.Driver
The test in question:
// It's in development, it's I have
package com.latteIceCream.latte;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class FlavorCRUDTest {
@Autowired
MockMvc mockMvc;
@Test
public void flavorRequest() throws Exception
{
mockMvc.perform
(
MockMvcRequestBuilders.post("/flavor/{name}")
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
.andExpect
(MockMvcResultMatchers.content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)
);
}
}
System.getenv()methods provide access to environment variables in the JVM's environment. Core Java has no special shortcuts for referring to these. I don't know about Spring, but I would be surprised if it did.