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 com.startup.musicstore.test.repository; import com.startup.musicstore.domain.CustomerAddress; import com.startup.musicstore.respository.AddressRepository; import com.startup.musicstore.test.ConnectionConfigTest; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import 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 hashcode */ public class AddressRepoTest { public static ApplicationContext ctx; private Long id; private AddressRepository addRepo; public AddressRepoTest() { } // TODO add test methods here. // The methods must be annotated with annotation @Test. For example: // @Test(enabled = true) public void createAddress() { addRepo = ctx.getBean(AddressRepository.class); CustomerAddress add = new CustomerAddress.Builder("qwertyui").build(); addRepo.save(add); id = add.getId(); Assert.assertNotNull(id); } @Test(dependsOnMethods = "createAddress", enabled = true) public void readAddress() { addRepo = ctx.getBean(AddressRepository.class); Assert.assertEquals(addRepo.findOne(id).getStreetAddress(), "qwertyui"); } @Test(dependsOnMethods = "readAddress", enabled = true) private void updateAddress() { addRepo = ctx.getBean(AddressRepository.class); CustomerAddress old = addRepo.findOne(id); CustomerAddress newAddress = new CustomerAddress.Builder(old.getStreetAddress()).clone(old) .setPostalAddress("2121").build(); addRepo.save(newAddress); Assert.assertEquals(addRepo.findOne(id).getPostalAddress(), "2121"); } @Test(dependsOnMethods = "updateAddress", enabled = true) private void deleteAddress() { addRepo = ctx.getBean(AddressRepository.class); addRepo.delete(id); Assert.assertNull(addRepo.findOne(id)); } @BeforeClass public static void setUpClass() throws Exception { // INITIALISE THE TEST CONTEST ctx = new AnnotationConfigApplicationContext(ConnectionConfigTest.class); } @AfterClass public static void tearDownClass() throws Exception { } @BeforeMethod public void setUpMethod() throws Exception { } @AfterMethod public void tearDownMethod() throws Exception { } }