在我的春季网络应用程序中,我正在尝试测试我的控制器:
@Controller
@RequestMapping(value="/Position")
public class PositionController {
@Autowired
PositionRepository positionRepository;
@RequestMapping(method=RequestMethod.GET, value="/search/infoforPosition)
public @ResponseBody List<Map<String, Object>> read(@RequestParam(value="id", required=true) Integer id){
List<Map<String, Object>> res = positionRepository.infoPerPosition(id);
return res;
}
}
我的测试代码如下所示:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"classpath*:/META-INF/spring/applicationContext.xml"})
private MockMvc mockMvc;
@Mock
PositionController PositionMock;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
this.mockMvc = MockMvcBuilders.standaloneSetup(PositionMock).build();
}
@Test
public void test() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/Position/search/infoforPosition")
.param("id", "435651")
.accept("application/json"))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk());
}
当我执行测试时,我得到了这个:
MockHttpServletRequest:
HTTP Method = GET
Request URI = /Position/search/infoforPosition
Parameters = {id=[435651]}
Headers = {Accept=[application/json]}
Handler:
Type = org.controllers.PositionController
Method = public java.util.List<java.util.Map<java.lang.String, java.lang.Object>> org.controllers.PositionController.read(java.lang.Integer)
Async:
Was async started = false
Async result = null
Resolved Exception:
Type = null
ModelAndView:
View name = null
View = null
Model = null
FlashMap:
MockHttpServletResponse:
Status = 200
Error message = null
Headers = {Content-Type=[application/json;charset=UTF-8]}
Content type = application/json;charset=UTF-8
Body = []
Forwarded URL = null
Redirected URL = null
Cookies = []
当我在Chrome浏览器中测试网址时,我得到了这个:
[{ “ID”:435651 “Position_id”:33, “部分”:64509}]
在我的pom.xml文件中,我添加了jackson-mapper-asl.jar和jackson-core-asl.jar 我不知道为什么响应状态是200,但我没有得到任何数据,所以单元测试如何通过? 当我放置断点时,似乎应用程序不执行该方法。
任何帮助将不胜感激:)