【发布时间】:2014-09-19 21:21:40
【问题描述】:
我正在尝试在 spring-boot 中使用 spring security 和一个使用 thymeleaf 进行模板处理的简单主(根)控制器运行单元测试。我正在尝试编写一些单元测试来验证我的安全权限是否正常工作,并且正确的数据是否从我的模板中隐藏或显示(它使用 thymeleaf spring 安全集成)。当我运行它时,应用程序本身可以正常工作。我只是想验证它是否正在使用一组集成测试。 您可以在这里找到所有代码,但我也会在下面包含相关的 sn-ps:
https://github.com/azeckoski/lti_starter
控制器非常简单,除了渲染模板(在根 - 即“/”)之外什么都不做。
@Controller
public class HomeController extends BaseController {
@RequestMapping(method = RequestMethod.GET)
public String index(HttpServletRequest req, Principal principal, Model model) {
log.info("HOME: " + req);
model.addAttribute("name", "HOME");
return "home"; // name of the template
}
}
模板有很多内容,但测试的相关部分是:
<p>Hello Spring Boot User <span th:text="${username}"/>! (<span th:text="${name}"/>)</p>
<div sec:authorize="hasRole('ROLE_USER')">
This content is only shown to users (ROLE_USER).
</div>
<div sec:authorize="isAnonymous()"><!-- only show this when user is NOT logged in -->
<h2>Form Login endpoint</h2>
...
</div>
最后是测试:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
@WebAppConfiguration
public class AppControllersTest extends BaseApplicationTest {
@Autowired
WebApplicationContext wac;
@Autowired
private FilterChainProxy springSecurityFilter;
private MockMvc mockMvc;
@Before
public void setup() {
// Process mock annotations
MockitoAnnotations.initMocks(this);
// Setup Spring test in webapp-mode (same config as spring-boot)
this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
.addFilter(springSecurityFilter, "/*")
.build();
}
@Test
public void testLoadRoot() throws Exception {
// Test basic home controller request
MvcResult result = this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andReturn();
String content = result.getResponse().getContentAsString();
assertNotNull(content);
assertTrue(content.contains("Hello Spring Boot"));
assertTrue(content.contains("Form Login endpoint"));
}
@Test
public void testLoadRootWithAuth() throws Exception {
Collection<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
Authentication authToken = new UsernamePasswordAuthenticationToken("azeckoski", "password", authorities);
SecurityContextHolder.getContext().setAuthentication(authToken);
// Test basic home controller request
MvcResult result = this.mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML))
.andReturn();
String content = result.getResponse().getContentAsString();
assertNotNull(content);
assertTrue(content.contains("Hello Spring Boot"));
assertTrue(content.contains("only shown to users (ROLE_USER)"));
}
}
我在上述两个测试中得到的错误是:
testLoadRoot(ltistarter.controllers.AppControllersTest) 已用时间: 0.648 秒
但是,只有在启用两个测试并且包含 springSecurityFilter 时才会发生这种情况。如果我禁用其中一项测试并删除 springSecurityFilter 代码 (.addFilter(springSecurityFilter, "/*")),那么我将不再收到该错误。我怀疑某些东西可能会弄乱 WebApplicationContext 或使安全性内容处于某种故障状态,但我不确定我需要重置或更改什么。
因此,如果我取出第二个测试并删除 springSecurityFilter,那么我的第一个测试仍然会失败(尤其是这个assertTrue(content.contains("Form Login endpoint"))),但我不再收到任何错误。当我查看生成的 HTML 时,我没有看到任何使用 sec:authorize 属性的标签内容。
所以我四处搜索,发现我需要在springSecurityFilter 中添加一个建议(我已经在上面的代码示例中完成了),但是,一旦我这样做了,我就会立即失败(它没有甚至到了没有它就失败的地步)。有关导致该异常的原因以及如何修复它的任何建议?
【问题讨论】:
-
消息 处理器执行期间出错 并没有真正的帮助。你能包括完整的堆栈跟踪吗?
-
给我们你所拥有的一切:-)
-
第一条有用的行说明了
WebApplicationContextUtils:84中的一个问题,即throw new IllegalStateException("No WebApplicationContext found: no ContextLoaderListener registered?");。我不确定为什么在那个阶段上下文会为空。 -
我也是。它似乎不是空的(我什至添加了一个检查以确保它不是)。也许我需要做一些事情来使它可以访问它?
标签: spring-mvc spring-security spring-boot thymeleaf spring-test-mvc