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 crud; import com.company.transactionapp.appconfig.ConnectionConfig; import com.company.transactionapp.model.Department; import com.company.transactionapp.model.Product; import com.company.transactionapp.model.Statement; import com.company.transactionapp.model.Teller; import com.company.transactionapp.repository.ProductRepository; import com.company.transactionapp.repository.StatementRepository; import java.util.ArrayList; import java.util.Date; 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; /** * * @author Luke */ public class StatementCrud { public static ApplicationContext ctx; private static StatementRepository statementRepo; private static ProductRepository productRepo; private static Product product; private static Department department; private static Teller teller; private static List<Product> productList; private Long id; public StatementCrud() { } // 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); department = new Department.Builder().department("Food").build(); productRepo = ctx.getBean(ProductRepository.class); product = new Product.Builder().productName("Topdeck").price(9.50).productType(null).quantity(10).build(); productRepo.save(product); teller = new Teller.Builder().tellerName("John Smith").build(); productList = new ArrayList(); productList.add(product); } @AfterClass public static void tearDownClass() throws Exception { } @BeforeMethod public void setUpMethod() throws Exception { } @AfterMethod public void tearDownMethod() throws Exception { } @Test public void createStatementTest() { statementRepo = ctx.getBean(StatementRepository.class); Statement statement = new Statement.Builder().department(department).products(productList).teller(teller) .purchaseDate(new Date()).build(); statementRepo.save(statement); id = statement.getId(); assertNotNull(statement); } @Test(dependsOnMethods = "createStatementTest") public void readStatementTest() { statementRepo = ctx.getBean(StatementRepository.class); Statement statement = statementRepo.findOne(id); assertEquals(statement.getDepartment(), department); } @Test(dependsOnMethods = "readStatementTest") private void updateStatementTest() { statementRepo = ctx.getBean(StatementRepository.class); Statement statement = statementRepo.findOne(id); Statement updatedStatement = new Statement.Builder().clone(statement).department(null).build(); statementRepo.save(updatedStatement); Statement newStatement = statementRepo.findOne(id); assertEquals(newStatement.getDepartment(), null); } @Test(dependsOnMethods = "updateStatementTest") private void deleteStatement() { statementRepo = ctx.getBean(StatementRepository.class); Statement statement = statementRepo.findOne(id); statementRepo.delete(statement); Statement deletedStatement = statementRepo.findOne(id); assertNull(deletedStatement); } }