Spring4 MVC单元测试不编译 [英] Spring4 MVC Unit test does not compile
问题描述
在Spring 3.2.5→4.0.0版本更新后尝试编译源代码时,我遇到了一些奇怪的行为。
I have strange behaviour when trying to compile sources after Spring 3.2.5 → 4.0.0 version update.
来自的错误代码片段ApplicationControllerTest .java
(它等同于来自文档):
Faulty code snippet from ApplicationControllerTest.java
(it is equivalent to code from documentation):
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:不兼容的类型;推断类型参数java.lang.Object不符合类型变量的边界B
found:< B> org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder< B>
必需:java.lang.Object
如果查看 MockMvcBuilders
源,可以看到区别:
If one looks into MockMvcBuilders
sources, one can see the difference:
Spring 4.0.0:
Spring 4.0.0:
public static <B extends DefaultMockMvcBuilder<B>> DefaultMockMvcBuilder<B> webAppContextSetup(WebApplicationContext context) {
return new DefaultMockMvcBuilder<B>(context);
}
Spring 3.2.5:
Spring 3.2.5:
public static DefaultMockMvcBuilder<DefaultMockMvcBuilder<?>> webAppContextSetup(WebApplicationContext context) {
return new DefaultMockMvcBuilder<DefaultMockMvcBuilder<?>>(context);
}
我试图让它可编译没有成功。
My attempts to make it compilable did not succeeded.
其实 Spring文档说,框架应该是Java 1.6.0_10兼容。我使用Java 1.6.0_45。
Actually Spring documentation says that framework should be Java 1.6.0_10 compatible. I use Java 1.6.0_45.
推荐答案
我认为 webAppContextSetup
方法现在应该使用< B extends DefaultMockMvcBuilder< B>< / code>的类来显式地参数化。明显的候选者是
StandaloneMockMvcBuilder
或简单地 DefaultMockMvcBuilder
(虽然后者将生成关于未选中或不安全操作的警告)。所以试试这个:
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();
这篇关于Spring4 MVC单元测试不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!