Java application testing methodologies

There are several levels of testing that are progressively executed on production ready java applications. These several rounds of testing test different aspects of the application and this blog discusses about these various levels of testing and their purposes.

Below is an example of a sample Service layer class in which the DAO layer class is injected.

	public class CustomerService {
		
		@Resource
		private CustomerDAO customerDAO;
	
		public boolean saveCustomer(Customer customerDAO) {
			if (null != customer) return false;
			return customerDAO.save(customer);
		}	
	}

Below is an example of how the class has been unit tested, a few points to note here:

	@RunWith(MockitoJUnitRunner.class)
	public class TestCustomerService {
		
		@InjectMocks
		private CustomerService customerService;
		
		@Mock
		private CustomerDAO customerDAO;
		
		@Mock
		private Customer customer;
	
		@Test
		public boolean testSaveCustomer_whenCustomerIsNull() {
			Customer cust = null;
			customerService.saveCustomer(cust);
			verify(customerDAO.times(0)).save();
		}	
		
		@Test
		public boolean testSaveCustomer_whenCustomerIsValid() {
			doNothing().when(customerDAO.save(customer));
			customerService.saveCustomer(customer);
			verify(customerDAO.times(1)).save();
		}				
	}

Below is an example where the same class is now subjected to integration testing but before you see the below code - please review the below interesting features:

	public class ITTestCustomerService {
		
		@Test
		public boolean testSaveCustomer_whenCustomerIsValid() {
			CustomerDAO customerDAO = new CustomerDAO();
			Customer customer = new Customer ();
			customer.build.firestName("Soumik").lastName("Mukherjee").profession("Java Architect");
			customerService.saveCustomer(customer);
			assertTrue(checkIfCustomerIsInsertedInDB(checkIfCustomerIsInsertedInDB));
		}	
		
		@Test
		public boolean testSaveCustomer_whenCustomerAlreadyExists () {
			CustomerDAO customerDAO = new CustomerDAO();
			Customer customer = new Customer ();
			customer.build.firestName("Soumik").lastName("Mukherjee").profession("Java Architect");
			boolean result = customerService.saveCustomer(customer);
			assertFalse(checkIfCustomerIsInsertedInDB(checkIfCustomerIsInsertedInDB));
		}		
		
		private boolean checkIfCustomerIsInsertedInDB(Customer customer){
		..........
		// logic to go in DB and check whether the customer is inserted
		}
		
	}