java如何使用@WithMockUser并从属性文件中传递用户名和密码?
我正在编写一个集成测试,其中应用程序使用SpringSecurity应用了基本身份验证。我正在使用@WithMockUser告诉mockMVC如何使用我的端点进行身份验证
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
@WithMockUser(username="username", password="password")
这是可行的,但我想知道是否可以用application.properties
文件中的值的引用替换这些字符串,类似于您可以执行的操作:
@Value("${spring.security.user.name}")
private String userName;
上述情况是否可能
# 1 楼答案
我最终删除了
@WithMockUser
注释,并使用了这个对我的用例更有效的解决方案。此方法使用配置的基本身份验证进行身份验证,因此,如果要进行更改,则不会出现问题# 2 楼答案
您只能将常量表达式传递给注释,但另一种方法是创建自己的安全上下文,如@WithSecurity Context一章中所述: https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/test-method.html
我还将创建自己的测试属性配置文件,用于从测试中读取属性