Performing testing

The following are the steps to perform testing:

  1. Create a com.packt.ch07.tests package.
  2. Use the JUnit test case template to generate from Eclipse IDE as follows:
    1. Enter the name of the test case as TestBookDAO_JdbcTemplate.
    2. Select checkboxes for setUp and teardown to initialize and release the test case components.
    3. Click on the Browse button, and select BookDAO_JdbcTemplate as the class under test.
    4. Click on the Next button.
    5. In the Test Methods dialog box, select all the methods from the BookDAO_JdbcTemplate class.
    6. Click on Finish.
    7. A dialogue will appear asking to add JUnit4 on the build path. Click on the OK button.

The steps can be summarized as follows:

After clicking on the Next button, you will get the next dialogue, which looks like the following screenshot:

  1. In the test case, declare a data member as BookDAO_JdbcTemplate.
  2. Update the setUp() method to initialize the data member of test case using the ApplicationContext container.
  3. Update tearDown() to release the resources.
  4. Update testAddBook() as follows:
    1. Create an object of type Book with some values, and make sure the value of ISBN is not available in the Book table.
    2. Invoke addBook() from the BookDAO_JdbcTemplate class.
    3. Test the result using the assertEquals()method, as shown in the following code:
        public class TestBookDAO_JdbcTemplate{ 
BookDAO_JdbcTemplate bookDAO_JdbcTemplate;

@Before
public void setUp() throws Exception {
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("connection_new.xml");
bookDAO_JdbcTemplate = (BookDAO_JdbcTemplate)
applicationContext.getBean("bookDAO_jdbcTemplate");
}
@After
public void tearDown() throws Exception {
bookDAO_JdbcTemplate = null;
}

@Test
public void testAddBook() {
Book book = newBook("Book_Test", 909090L, "Test
Publication", 1000, "Test Book description", "Test
author");
int rows_insert= bookDAO_JdbcTemplate.addBook(book);
assertEquals(1, rows_insert);
}
}
  1. Select the testAddBook() method, and run it as a JUnit test.
  1. The JUnit window will be shown with a green mark, indicating that the code has passed the unit test, as shown in the following screenshot:

The ISBN is a primary key in the Book table; if you rerun the same testAddBook() class, it will fail, showing red color instead of green. Still, it proves that our code is working as per logic. If one of the test conditions fails, the test case execution stops by showing AssertionError.

Try to write the test that exactly reflects the intention of your code, so it should be always passed if and only if the code you write is correct.
  1. Let's add TestAddBook_Negative ()to test what happens if we try to add a book with the same ISBN. Don't forget to annotate the method by @Test. The code is written as follows:
        @Test(expected=DuplicateKeyException.class) 
public void testAddBook_Negative() {
Book book = newBook("Book_Test", 909090L, "Test
Publication", 1000, "Test Book description", "Test
author");
int rows_insert= bookDAO_JdbcTemplate.addBook(book);
assertEquals(0, rows_insert);
}

We are testing the method which has the logic of adding data to the table. On adding a duplicate key, the code will throw DuplicateKeyException. We had annotated the method by the @Test annotation, adding value for the attribute expected as DuplicateKeyException.

  1. In the same way, let's add code to the other test methods, as shown in the following piece of code:
        @Test 
public void testUpdateBook() {
//with ISBN which does exit in the table Book
long ISBN = 909090L;
int updated_price = 1000;
int rows_insert = bookDAO_JdbcTemplate.updateBook(ISBN,
updated_price);
assertEquals(1, rows_insert);
}

@Test
public void testUpdateBook_Negative() {
// code for deleting the book with ISBN not in the table
}
@Test
public void testDeleteBook() {
//with ISBN which does exit in the table Book
long ISBN = 909090L;
boolean deleted = bookDAO_JdbcTemplate.deleteBook(ISBN);
assertTrue(deleted);
}

@Test
public void testDeleteBook_negative() {
// deleting the book with no iSBN present in the table.
}
@Test
public void testFindAllBooks() {
List<Book>books =
bookDAO_JdbcTemplate.findAllBooks();
assertTrue(books.size()>0);
assertEquals(4, books.size());
assertEquals("Learning Modular Java
Programming",books.get(3).getBookName());
}

@Test
public void testFindAllBooks_Author() {
List<Book>books =
bookDAO_JdbcTemplate.findAllBooks("T.M.Jog");

assertEquals("Learning Modular Java
Programming",books.get(1).getBookName());
}
  1. Run the test case methods, and find the result. You can also execute the complete test case as one unit. The only thing to keep in mind is that if any of the methods fails while executing all the test case methods at once, no further methods will be run by the test runner.

The preceding code constructs a few objects, such as BookDAO_JdbcTemplate, which are constructed using the Spring container. In the code, we consumed an object of BookDAO_JdbcTemplate, which we obtained in setUp() using the Spring container. Can't we have a better choice instead of doing it manually? Yes, we can do it using the custom runner provided by Spring. The SprinJUnit4ClassRunner framework is a custom runner, which is an extension of the JUnit4Runner framework, and provides the facility to use the Spring TestContext framework, implicitly taking the complexity out.

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

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