Unit testing a great way to ensure each segment of code is working as it should without the hassle of full deployment as you have seen.
Q1) As they have shown you can test the business logic separate from the HttpServlet (implementation) since your MVC should function the same whether the controller is executing on a web server or via some other protocol via socket for example. However, being you are specifically dealing with Spring MVC and Servlets, to test the web application, you will find HttpUnit (http://httpunit.org) helpful also.
Q2) Take a look at the HelloController implementation.
public class HelloController implements Controller {
protected final Log logger = LogFactory.getLog(getClass
public ModelAndView handleRequest(HttpServletR
throws ServletException, IOException {
logger.info("Returning hello view");
return new ModelAndView("hello.jsp");
}
}
Since it is for Servlet implementation, it takes HttpServletRequest and HttpServletResponse objects just like Servlet methods would. Since you are testing with JUnit and not executing in a ServletContext and therefore are not receiving a request nor writing to a response, you can just pass null for both. Hence:
ModelAndView modelAndView = controller.handleRequest(n
Q3) Referring again to the implementation above, see it returns a ModelAndView instance. That is all that matters. Doesn't matter this is running on web or on command line, as long as there is a valid ModelAndView that the getViewName() function can be called on and that the name of this view was "hello.jsp" then the assertion should be satisfied.
Q4) I typically am only looking for failures, issues; however, since there can be multiple tests in a test suite, IIRC, you will get trace for individual test which is the first and then the last is the suite completing for the overall test class completion.
FYI, it is not an error, it is result of logger.info().
Q5) It checks if the Model is set for the ModelAndView object. Assert that the object is not null. :)
You would get a failure listed.
Main Topics
Browse All Topics





by: cofactorPosted on 2009-11-07 at 06:09:04ID: 25766374
comments please