0
根據該文件有兩種方法可以做到這一點:集成和單元測試MockMvc
第一:
@RunWith(SpringRunner.class)
@WebAppConfiguration
@ContextConfiguration("my-servlet-context.xml")
public class MyWebTests {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
// ...
}
這種形式使用應用的實際環境。
而第二種方式:
public class MyWebTests {
private MockMvc mockMvc;
@Mock
private MyService myService;
@InjectMocks
private MyController myController;
@Before
public void setup() {
// Process mock annotations
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(myController)
.setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver()).build();
}
@Test
public void testsForPost() throws Exception {
Foo foo = new Foo();
//given
given(myService.findById(Matchers.anyLong())).willReturn(foo);
//when
this.mockMvc.perform((post("/foo")))
//then
.andExpect(status().isMethodNotAllowed())
.andDo(print());
}
...
}
使用這種方法,我不使用的應用程序上下文。
我的問題是,我可以考慮第一種形式的集成測試?第二個是單元測試?
否則會是什麼做的用SpringMVC單元測試的最佳解決方案。