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.swcguild.bluraymvc.test; import com.swcguild.bluraymvc.dao.BluRayDaoInMemImpl; import com.swcguild.bluraymvc.dto.Movie; import org.junit.After; import org.junit.AfterClass; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; public class BluRayTest_New { private BluRayDaoInMemImpl dao; public BluRayTest_New() { } @BeforeClass public static void setUpClass() { } @AfterClass public static void tearDownClass() { } @Before public void setUp() { // NEW CODE START- this cleans up the database table before each test // Ask Spring for my DAO //I think we use the bean id here ApplicationContext ctx = new ClassPathXmlApplicationContext("test-applicationContext.xml"); dao = (BluRayDaoInMemImpl) ctx.getBean("movieDao"); // Grab a JdbcTemplate to use for cleaning up JdbcTemplate cleaner = (JdbcTemplate) ctx.getBean("jdbcTemplate"); cleaner.execute("delete from movies"); // NEW CODE STOP } @After public void tearDown() { } @Test public void addGetDeleteMovie() { // create new movie Movie nm = new Movie(); nm.setTitle("Star Wars"); nm.setDirector("Lucas"); nm.setYear("1977"); nm.setRating("PG"); nm.setComment("saw in the theater"); dao.createMovie(nm); Movie fromDb = dao.getMovieById(nm.getMovieId()); assertEquals(fromDb.getMovieId(), nm.getMovieId()); assertEquals(fromDb.getTitle(), nm.getTitle()); assertEquals(fromDb.getDirector(), nm.getDirector()); assertEquals(fromDb.getYear(), nm.getYear()); assertEquals(fromDb.getRating(), nm.getRating()); assertEquals(fromDb.getComment(), nm.getComment()); dao.removeMovie(nm.getMovieId()); assertNull(dao.getMovieById(nm.getMovieId())); } @Test public void addUpdateMovie() { // create new movie Movie nm = new Movie(); nm.setTitle("Alien"); nm.setDirector("Scott"); nm.setYear("1979"); nm.setRating("PG"); dao.createMovie(nm); nm.setRating("R"); dao.updateMovie(nm); Movie fromDb = dao.getMovieById(nm.getMovieId()); assertEquals(fromDb.getMovieId(), nm.getMovieId()); assertEquals(fromDb.getTitle(), nm.getTitle()); assertEquals(fromDb.getDirector(), nm.getDirector()); assertEquals(fromDb.getYear(), nm.getYear()); assertEquals(fromDb.getRating(), nm.getRating()); } // @Test // public void getAllMovies() { // create new movie // Contact nc = new Contact(); // nc.setName("Jimmy Smith"); // nc.setEmail("jimmy@smith.com"); // nc.setPhone("1112223333"); // // dao.addContact(nc); // // // create new contact // Contact nc2 = new Contact(); // nc2.setName("John Jones"); // nc2.setEmail("john@jones.com"); // nc2.setPhone("5556667777"); // // dao.addContact(nc); // // Contact[] cArr = dao.getAllContacts(); // assertEquals(cArr.length, 2); // } } // // // // @Test // public void TestAddRemoveGetAll() { // //see Mike King for systematic use of this test // //ensures that we start with no movies // List<Movie> testList = brimpl.getAllMovies(); // assertEquals(0, testList.size()); // // //tests getAll as part of other tests // testList = brimpl.getAllMovies(); // // //tests the add function // testList.add(movie1); // testList.add(movie2); // testList.add(movie3); // assertEquals(3, testList.size()); // // //tests the remove function // testList.remove(movie1); // assertEquals(2, testList.size()); // } // // @Test // public void TestGetById() { // brimpl.createMovie(movie1); // brimpl.createMovie(movie2); // brimpl.createMovie(movie3); // List<Movie> testList = new ArrayList<>(); // testList.add(brimpl.getMovieById(2)); // assertEquals(1, testList.size()); //// } // //}