In my Spring Security UserDetailsService, I inject Environment to read credentials from env variables.
In an integration test, I would like to mock the Environment interface in order to change the env variables for the tests.
Here is my Test :
@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = EportfolioApplication.class)
@AutoConfigureMockMvc
public class IntegrationAuth {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Test
void loginCorrectCredentials_returnsToken() throws Exception {
User user = new User();
user.setUsername("John Shepard");
user.setPassword("Tali");
MvcResult mvcResult = mockMvc.perform(post("/login")
.contentType("application/json")
.content(objectMapper.writeValueAsString(user)))
.andExpect(status().isOk())
.andReturn();
assertNotNull(
"JWT Token should be present",
mvcResult.getResponse().getHeader("Authorization")
);
}
}
What would be the best approach for this ?