Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package repositoryTests; import com.pharmacy.pharmacyweb.configuration.ConnectionConfig; import com.pharmacy.pharmacyweb.domain.products.Ingredient; import com.pharmacy.pharmacyweb.domain.products.Medicine; import com.pharmacy.pharmacyweb.repository.MedicineRepository; import java.util.ArrayList; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import static org.testng.Assert.*; import org.testng.annotations.AfterClass; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeClass; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; import static repositoryTests.MiscRepositoryTestNGTest.ctx; /** * * @author Luke */ public class MedicineRepositoryTestNGTest { public static ApplicationContext ctx; private long id; private MedicineRepository repo; private static Ingredient ingredient; private static List<Ingredient> ingredientList; public MedicineRepositoryTestNGTest() { } // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // // @Test // public void hello() {} @BeforeClass public static void setUpClass() throws Exception { ctx = new AnnotationConfigApplicationContext(ConnectionConfig.class); ingredient = new Ingredient.Builder().allergen(true).ingredientName("Nuts").build(); ingredientList = new ArrayList<Ingredient>(); ingredientList.add(ingredient); } @AfterClass public static void tearDownClass() throws Exception { } @BeforeMethod public void setUpMethod() throws Exception { } @AfterMethod public void tearDownMethod() throws Exception { } @Test public void createMedicineTest() { repo = ctx.getBean(MedicineRepository.class); Medicine medicine = new Medicine.Builder().id(0).productDescription("Cancer Antidote") .productName("L Davids - Cancer-Away").price(349.95).quantity(39).treats("cancer") .ingredient(ingredientList).build(); repo.save(medicine); id = medicine.getId(); assertNotNull(medicine); } @Test(dependsOnMethods = "createMedicineTest") public void readMedicineTest() { repo = ctx.getBean(MedicineRepository.class); Medicine medicine = repo.findOne(id); assertEquals(medicine.getQuantity(), 39); } @Test(dependsOnMethods = "readMedicineTest") private void updateMedicineTest() { repo = ctx.getBean(MedicineRepository.class); Medicine medicine = repo.findOne(id); Medicine updatedMedicine = new Medicine.Builder().clone(medicine).quantity(2).build(); repo.save(updatedMedicine); Medicine newMedicine = repo.findOne(id); assertEquals(newMedicine.getQuantity(), 2); } @Test(dependsOnMethods = "updateMedicineTest") private void deleteMedicine() { repo = ctx.getBean(MedicineRepository.class); Medicine medicine = repo.findOne(id); repo.delete(medicine); Medicine deletedMedicine = repo.findOne(id); assertNull(deletedMedicine); } }