12

在 Spring 3.2.5 → 4.0.0 版本更新后尝试编译源时,我有奇怪的行为。

错误的代码片段ApplicationControllerTest.java(它相当于文档中的代码):

import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
...
@Autowired
private WebApplicationContext wac;

private MockMvc               mockMvc;

@Before
public void setUp() {
    mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}

错误:

COMPILATION ERROR : /C:/Development/.../war/src/test/java/org/.../web/controller/ApplicationControllerTest.java:[59,61] C:\Development\...\war\src\test\java\org\...\web\controller\ApplicationControllerTest.java:59: incompatible types; inferred type argument(s) java.lang.Object do not conform to bounds of type variable(s) B found : <B>org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder<B> required: java.lang.Object

如果查看MockMvcBuilders来源,可以看到差异:

春季 4.0.0:

public static <B extends DefaultMockMvcBuilder<B>> DefaultMockMvcBuilder<B> webAppContextSetup(WebApplicationContext context) {
    return new DefaultMockMvcBuilder<B>(context);
}

春季 3.2.5:

public static DefaultMockMvcBuilder<DefaultMockMvcBuilder<?>> webAppContextSetup(WebApplicationContext context) {
    return new DefaultMockMvcBuilder<DefaultMockMvcBuilder<?>>(context);
}

我试图使其可编译的尝试没有成功。

实际上Spring 文档说框架应该与 Java 1.6.0_10 兼容。我使用 Java 1.6.0_45。

4

3 回答 3

8

我认为webAppContextSetup现在方法的调用应该用<B extends DefaultMockMvcBuilder<B>>. 显而易见的候选者是StandaloneMockMvcBuilder或只是DefaultMockMvcBuilder(尽管后者会生成有关未经检查或不安全操作的警告)。所以试试这个:

mockMvc = MockMvcBuilders.<StandaloneMockMvcBuilder>webAppContextSetup(wac).build();
于 2013-12-17T09:48:36.943 回答
3

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.)

于 2013-12-17T16:35:02.723 回答
3

这已在 Spring Framework 4.0.1 中得到修复。

有关详细信息,请参阅以下 JIRA 问题:https ://jira.springsource.org/browse/SPR-11238

于 2014-01-04T17:39:04.587 回答