gpt4 book ai didi

java - 如何使用 spring 3.2 新 mvc 测试登录用户

转载 作者:IT老高 更新时间:2023-10-28 13:50:38 24 4
gpt4 key购买 nike

这工作正常,直到我必须测试需要登录用户的服务,我如何将用户添加到上下文:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-test.xml")
@WebAppConfiguration
public class FooTest {
@Autowired
private WebApplicationContext webApplicationContext;

private MockMvc mockMvc;

@Resource(name = "aService")
private AService aService; //uses logged in user

@Before
public void setup() {
this.mockMvc = webAppContextSetup(this.webApplicationContext).build();
}

最佳答案

如果你想使用 MockMVC 和最新的 spring 安全测试包,试试这个代码:

Principal principal = new Principal() {
@Override
public String getName() {
return "TEST_PRINCIPAL";
}
};
getMockMvc().perform(get("http://your-url.com").principal(principal))
.andExpect(status().isOk()));

请记住,您必须使用基于主体的身份验证才能使其正常工作。

关于java - 如何使用 spring 3.2 新 mvc 测试登录用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14308341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com