3

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.

devnull
  • 49
  • 1
  • 4

2 Answers2

0

"No ModelAndView Found" is right. With @ResponseBody the returned value is written directly to the body of the response. No model, no view resolution, etc.

More generally, ideally focus on testing the outcome of the request from a client perspective. That includes the response headers, body, and the response status. Test other results, that are usually not visible to the client, such as model attributes more sparingly.

Rossen Stoyanchev
  • 4,910
  • 23
  • 26
  • If ModelAndView is empty, i understand it. But, shouldn't that return XML or JSON content in the body? I say this because when using _xpath_ or _json_ functions with _andExpect( )_ the results are empty also. I receive messages like "malformed xml at line 1 column 1" for XML or "no json data" for JSON. Also, when using _andDo(print())_ the body returns empty too when i check the console output. – devnull Mar 28 '13 at 14:26
  • Also, i should add that using just _andExpect(content().string(_ ... this too returns empty results. Is not a matter of ModelAndView. – devnull Apr 02 '13 at 21:35
  • 1
    @devnull, same issue here. did you ever figure it out? – Nikita Sep 04 '13 at 15:42
  • @Nikita I have same issue with only one of my controllers on Spring Roo, Spring 3.2.3. Almost identical test case, controllers and underlying services as well as model objects are all pretty much same, nothing special. When I run project, curl returns results for all of those properly. Any clues?! – Ivor Prebeg Nov 05 '13 at 11:11
  • @devnull in your test you're expecting a model which isn't there because you wrote to the response body. If you changed your test please share it in your question so we can see the full picture. – Bart Nov 05 '13 at 11:54
0

I had almost identical issue as yours and seem to find what caused the problem.

I've explained my situation in one of the comments below, but this should be more complete.

I'm using Spring Roo 1.2.4, Spring 3.2.3. When I run my app, I can curl all controllers and all of them work just fine. But, one of my controllers returns empty json in mockmvc tests, no errors thrown.

I found the problem with try-catching problems in Roo generated AspectJ controller code (those files that say you should not edit them) and discovered that serialization code (.toJsonArray) fails with

org.hibernate.LazyInitializationException: failed to lazily initialize a collection...

which is silently ignored and no stack trace is spewed out automatically.

Ivor Prebeg
  • 988
  • 6
  • 11