1
我一直在为我们的Spring MVC应用程序编写集成测试。我们使用oAuth2进行身份验证。Java Spring MVC集成测试创建OAuth2校长
在这种情况下,Spring会给我们一个Principal
实例,我们用它来确定我们必须发送回客户端的实体。在我们的控制,我们有一个端点:
@RequestMapping("/bookings")
public @ResponseBody ResponseEntity<List<ThirdPartyBooking>> getBookings(Principal principal) {
OAuth2Authentication auth = (OAuth2Authentication) principal;
OAuth2AuthenticationDetails authDetails = (OAuthAuthenticationDetails) auth.getDetails();
// Extract stuff from the details...
}
现在,在我们的测试中,我想确保我们只发送预订已认证用户。下面的测试代码,可以发现:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {ThirdPartyBookingServiceConfiguration.class})
@WebAppConfiguration
@Component
public abstract class RepositoryTestBase {
@Resource
private WebApplicationContext context;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
}
@Test
public void shouldOnlyReturnUserBookings() throws Exception {
MockHttpServletResponse result = mockMvc.perform(MockMvcRequestBuilders.get("/bookings").principal(???)).andReturn().getResponse();
// Validate the response
}
}
我怎么会插入一个OAuth2Authentication
在???
?
这不正是我一直在寻找。它的确有很大的帮助。谢谢! – irundaia