In my spring webapp, I'm trying to test my controller:
@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;
}
}
My test Code looks like this:
@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());
}
When i execute the test i got this:
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 = []
when i test the url in Chrome Browser i got this:
[{"id":435651,"Position_id":33,"section":64509}]
In my pom.xml file i added jackson-mapper-asl.jar and jackson-core-asl.jar I don't know why the response status is 200 but i did'nt got any data,so how the unit test pass? When i put a breakpoint , it seems like the application doesn't execute the method.
Any help will be much appreciated :)