I have the following REST controller
@Controller
@RequestMapping("/rest/transceptors")
public class TransceptorRestController
{
@Autowired
private TransceptorDao transceptorDao;
@RequestMapping(value="/get/{idTransceptor}", method=RequestMethod.GET)
public @ResponseBody Transceptor getOne(@PathVariable("idTransceptor") Long idTransceptor)
{
return transceptorDao.searchByIdTransceptor(idTransceptor);
}
}
This controller works correctly when running in JBoss, and the results are as expected. I use Postman (a REST testing extension for Google Chrome) and i can get correct results in XML and JSON.
But, i have a problem when using MockMVC for testing that.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations={
"classpath:test-servlet-context.xml"
})
@WebAppConfiguration
public class TransceptorRestControllerTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setup()
{
mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
}
@Test
public void testRoot() throws Exception
{
mockMvc.perform(get("/")).
andExpect(status().isOk());
}
@Test
public void testGet() throws Exception
{
mockMvc.perform(get("/rest/transceptors/get/1"))
.andExpect(status().isOk())
.andDo(print())
.andExpect(model().attribute("name", equals("Test_Name_1")));
}
The TestRoot test works OK. But, when i try to use andExpect(model()... i receive the message "No ModelAndView Found"
When replacing the model() part for specific expectations for XML or JSON, the XML and JSON strings always return empty.
I have spent days trying to understand this, and i'm rather new to Java and more new to Spring. Can you tell me where can i look to fix that?
As adittional info, i had put log messages (with sfj4l) everywhere, but when running with Junit, the log messages in the DAO works, the log messages in the Test module itself works, but the log messages inside my REST controller does not appears.
Is like the GET function is matched, but the content of the function is never executed, and get empty responses. Is spite of that, my calls to isOk() are succesful.