【发布时间】:2021-04-09 04:20:51
【问题描述】:
在我的 Spring Security UserDetailsService 中,我注入 Environment 以从 env 变量中读取凭据。
在集成测试中,我想模拟 Environment 接口以更改测试的环境变量。
这是我的测试:
@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")
);
}
}
最好的方法是什么?
【问题讨论】:
标签: java spring-boot junit environment-variables integration-testing