我正在使用spring-test-mvc(独立)因为我的应用程序运行在spring 3.1中。我基本上只是想测试我的控制器方法和响应HTTP代码。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class})
public class AdminSpringControllerTests {
private AdminSpringController cut;
private MockMvc mockMvc;
public AdminSpringControllerTests() {
cut = new AdminSpringController();
this.mockMvc = MockMvcBuilders.standaloneSetup(cut).build();
}
@Test
public void testGetUsers_Sc200_1() throws Exception{
List<User> users = new ArrayList<User>();
for (int i = 1; i<3; i++) {
User user = new User();
user.setId(i);
user.setUserId("test"+i);
user.setName("Max Testermann");
users.add(user);
}
EasyMock.expect(cut.userService.getUsers()).andReturn(users);
EasyMock.replay();
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/user");
this.mockMvc.perform(request).andExpect(MockMvcResultMatchers.status().isOk());
EasyMock.verify(cut);
}
}
但我得到NoSuchMethodError
java.lang.NoSuchMethodError: org.springframework.web.servlet.FlashMapManager.saveOutputFlashMap(Lorg/springframework/web/servlet/FlashMap;Ljavax/servlet/http/HttpServletRequest;Ljavax/servlet/http/HttpServletResponse;)V
at org.springframework.test.web.server.request.MockHttpServletRequestBuilder.buildRequest(MockHttpServletRequestBuilder.java:605)
at org.springframework.test.web.server.MockMvc.perform(MockMvc.java:125)
at com.prime_sign.admin.web.controller.AdminSpringControllerTests.testGetUsers_Sc200_1(AdminSpringControllerTests.java:98)
是否有必要@Autowire我的控制器?我的构造函数错了吗?
在src / test / resources /“package-path”/AdminSpringControllerTests-context.xml中有一个上下文文件,但它只包含<context:annotation-config />
和<tx:annotation-driven />
,我是否需要在那里注入我的控制器bean(问题) 1,是否必要)。
任何帮助的谢谢:)
//编辑
我更新了我的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class AdminSpringControllerTests {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext wac;
public AdminSpringControllerTests() {
this.mockMvc = MockMvcBuilders.webApplicationContextSetup(this.wac).build();
}
@Test
public void testGetUsers_Sc200_1() throws Exception{
List<User> users = new ArrayList<User>();
for (int i = 1; i<3; i++) {
User user = new User();
user.setId(i);
user.setUserId("test"+i);
user.setName("Max Testermann");
users.add(user);
}
EasyMock.expect(cut.userService.getUsers()).andReturn(users);
EasyMock.replay();
MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/user").accept(MediaType.APPLICATION_JSON);
this.mockMvc.perform(request).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk());
EasyMock.verify(cut);
}
}
但是知道我遇到的问题(我认为),我没有得到正确的WebApplicationContext:
java.lang.IllegalArgumentException: WebApplicationContext is required
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.test.web.server.setup.InitializedContextMockMvcBuilder.<init>(InitializedContextMockMvcBuilder.java:39)
答案 0 :(得分:0)
您正在使用standaloneSetup并使用新的AdminSpringController()创建您的控制器;所以春天对它没有任何控制权。使用standaloneSetup更方便一次测试一个控制器,没有任何连接到过滤器/拦截器等...这可能不是你想要的。请尝试使用webAppContextSetup:
....
@Autowired
private WebApplicationContext wac;
@BeforeClass
public void setUp(){
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
这样它应该使用你的弹簧配置。