Tuesday, December 23, 2014

Tests

Every good application shuld be create with function test which are checking whether the functionality works as intended. A good practice is to share JUnit tests so that they tested only the selected method or function, starting from the simplest to the more advanced.

In my project I must added dependency for JUnit in pom.xml file, also create new sourceFolder, add package for tests. To import junit classes in Project properties click on "Java Build Path"., next must to do:
  1. Select the Libraries tab.
  2. Click the "Add Library" button.
  3. Choose junit.
Thats it now I may start create test which are check functionality of DAO files. On the first I created test to check adding data to database. In that purpose I create PersonDAOTests file, which looks:
package com.pizza.delivery.tests;  
 import org.junit.After;  
 import org.junit.Assert;  
 import org.junit.Before;  
 import org.junit.Test;  
 import org.junit.runner.RunWith;  
 import org.springframework.beans.factory.annotation.Autowired;  
 import org.springframework.test.context.ContextConfiguration;  
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;  
 import com.pizza.delivery.domain.Person;  
 import com.pizza.delivery.services.PersonDAO;  
 @RunWith(SpringJUnit4ClassRunner.class)  
 @ContextConfiguration(locations = { "testApplicationContext.xml" })  
 public class PersonDAOTests {  
      @SuppressWarnings("rawtypes")  
      @Autowired  
      private PersonDAO<Person> personDAO;  
      private Person person;  
      @Before  
      public void setUp() {  
           person = new Person();  
           person.setCity("Rzeszow");  
           person.setFirstName("Iwa");  
           person.setLastName("Mat");  
           person.setStatus("");  
           person = personDAO.create(person);  
      }  
      @After  
      public void afterTest() {  
           personDAO.delete(person.getPersonId());  
      }  
      @Test  
      public void testFindByIdPerson() {  
           Person personToCompare = personDAO.findById(person.getPersonId());  
           Assert.assertEquals(personToCompare.getCity(), person.getCity());  
     Assert.assertEquals(personToCompare.getFirstName(), person.getFirstName());  
     Assert.assertEquals(personToCompare.getLastName(), person.getLastName());  
     return;  
           //Person findedPerson = personDAO.findById(person.getPersonId());  
           //Assert.assertNotNull("Find person should be not null", findedPerson);  
           }  
 }  

That test file create object  in database, in Person table. Next using method: 
 public void testFindByIdPerson() compare data from database and from method which adding data to table. At the end of test using method: public void afterTest() to delete object in database.

Similar methods was create in IngredientDAOTest file, but here was usind method:  
public void findByNameTest(),  to searching object for name not for Id. Source code looks:

 @RunWith(SpringJUnit4ClassRunner.class)  
 @ContextConfiguration(locations = { "testApplicationContext.xml" })  
 public class IngredientDAOTests {  
      @Autowired  
      private IngredientDAO<Ingredient> ingredientDAO;  
      private Ingredient ingredient;  
      private static final String IngredientTestName = "PepperTestXXXX";  
      @Before  
      public void setUp() {  
           ingredient = new Ingredient();  
           ingredient.setName(IngredientTestName);  
           ingredient.setPrice(2.0);  
           ingredient = ingredientDAO.create(ingredient);  
      }  
      @After  
      public void afterTests() {  
           ingredientDAO.delete(ingredient.getIngredientId());  
      }  
      @Test  
      public void findByNameTest() {  
           List<Ingredient> listFindByName = ingredientDAO  
                     .findByName(IngredientTestName);  
           assertNotNull(listFindByName);  
           assertEquals(listFindByName.size(), 1);  
           Ingredient ingredientToCompare = listFindByName.get(0);  
           assertEquals(ingredientToCompare.getName(), ingredient.getName());  
           assertEquals(ingredientToCompare.getPrice(), ingredient.getPrice());  
      }  
 }  





No comments:

Post a Comment