Integration testing of controller and service layer

Let's combine the three layers in ReadMyBooks from Ch05_Declarative_Transaction_Management by performing the following steps:

  1. Add JARs for jdbc, spring-jdbc, and other required jars in the lib folder of ReadMyBooks.
  2. Copy the com.packt.ch03.dao and com.packt.ch05.service packages from Ch05_Declarative_Transaction_Management to the ReadMyBooks application.
  3. Copy connection_new.xml in the classpath of the ReadMyBooks application.
  4. In the Book class for form submission, we commented on the default constructor; the logic in addBook of service is to check against 98564567l as the default value.
  5. Change BookService shown in the following underlined code, keeping the rest of the code untouched:
        @Override 
@Transactional(readOnly=false)
public boolean addBook(Book book) {
// TODO Auto-generated method stub
if (searchBook(book.getISBN()).getISBN() == 0) {
//
}
}
  1. The controllers need to be updated to get a talk with the underlying layer, as follows:
    • Add the autowired data member of type BookService in the controllers.
    • Invoke the methods of the service layer in the method of controllers as per business logic requirements.
  1. The addBook() method will be updated as follows:
        @RequestMapping("/addBook.htm") 
public ModelAndView addBook(@Valid@ModelAttribute("book")
Book book, BindingResultbindingResult)
throws Exception {
// same code as developed in the controller
// later on the list will be fetched from the table
List<Book>books = new ArrayList();
if (bookService.addBook(book)) {
books.add(book);
}
modelAndView.addObject("book_list", books);
modelAndView.addObject("auth_name", book.getAuthor());
return modelAndView;
}
In the same way, we can update all the methods from the controller. You can refer to the complete source code.

Let's execute the test case TestAddBookController.java to get the result.

The code will execute, and display a success message. Also, a row with the ISBN and other values that we specified gets added to the table.

We have done testing of all the components successfully. We can now directly start with System testing.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.242.165