Spring4 MVC Unit test does not compile

爱⌒轻易说出口 提交于 2019-12-05 01:32:00

I think the call of webAppContextSetup method now should be explicitly parameterized with the class of <B extends DefaultMockMvcBuilder<B>>. The obvious candidates are StandaloneMockMvcBuilder or simply DefaultMockMvcBuilder (though the later will generate a warning about unchecked or unsafe operations). So try this:

mockMvc = MockMvcBuilders.<StandaloneMockMvcBuilder>webAppContextSetup(wac).build();

I had to tie the webAppContextSetup to the DefaultMockMvcBuilder in order to sort this problem out in my build (Java 1.6.0_65), and suppress the warnings that resulted from that.

@SuppressWarnings({"unchecked", "rawtypes"})
@Before
public void setUp()
{
   mockMvc = MockMvcBuilders.<DefaultMockMvcBuilder> webAppContextSetup(webApplicationContext).addFilter(springSecurityFilterChain).build();
}

As stated in another answer StandaloneMockMvcBuilder would work without warnings, if you are testing a stand alone controller rather than integration testing the full application context.

(Would have simply commented if I could have.)

This has been fixed for Spring Framework 4.0.1.

See the following JIRA issue for details: https://jira.springsource.org/browse/SPR-11238

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!