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 cz.swi2.mendeluis.dataaccesslayer.domain; import cz.swi2.mendeluis.dataaccesslayer.core.DatabaseConfig; import cz.swi2.mendeluis.dataaccesslayer.repository.FavouriteItemRepository; import cz.swi2.mendeluis.dataaccesslayer.repository.PortletRepository; import cz.swi2.mendeluis.dataaccesslayer.repository.UserFavouriteItemRepository; import cz.swi2.mendeluis.dataaccesslayer.repository.UserRepository; import java.util.ArrayList; import java.util.List; import org.apache.log4j.varia.NullAppender; import static org.junit.Assert.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; import org.springframework.test.context.transaction.TransactionalTestExecutionListener; import org.springframework.transaction.annotation.Transactional; /** * * @author Michal */ @ContextConfiguration(classes = DatabaseConfig.class) @TestExecutionListeners(TransactionalTestExecutionListener.class) @Transactional public class FavouriteItemTest extends AbstractTestNGSpringContextTests { @Autowired private FavouriteItemRepository favouriteItemRepository; public FavouriteItemTest() { org.apache.log4j.BasicConfigurator.configure(new NullAppender()); } @org.testng.annotations.Test public void testCreate() { // Create FavItem FavouriteItem favItem = new FavouriteItem("Rozvrh"); // Insert favItem this.favouriteItemRepository.insert(favItem); // Find favItem FavouriteItem result = this.favouriteItemRepository.getById(favItem.getId()); // asser equal assertEquals(favItem, result); } @org.testng.annotations.Test public void testUpdate() { // Create FavItem FavouriteItem favItem = new FavouriteItem("Rozvrh"); // Insert favItem this.favouriteItemRepository.insert(favItem); // try to update favItem.setName("Zkousky"); this.favouriteItemRepository.update(favItem); // Find favItem again FavouriteItem result = this.favouriteItemRepository.getById(favItem.getId()); // Assert updated value assertEquals("Zkousky", result.getName()); } @org.testng.annotations.Test public void testDelete() { // Create favItem FavouriteItem favItem = new FavouriteItem("Rozvrh"); // Insert favItem this.favouriteItemRepository.insert(favItem); // try to delete this.favouriteItemRepository.delete(favItem); // Find result FavouriteItem result = this.favouriteItemRepository.getById(favItem.getId()); // Assert updated value assertNull(result); } @org.testng.annotations.Test public void testGetAll() { // Create favitem FavouriteItem f1 = new FavouriteItem("Food menu xx"); FavouriteItem f2 = new FavouriteItem("Food menu xx 2"); // Insert favitem this.favouriteItemRepository.insert(f1); this.favouriteItemRepository.insert(f2); // Fetch all favitems List<FavouriteItem> results = this.favouriteItemRepository.getAll(); // Prepare a list to be compared List<FavouriteItem> fav = new ArrayList(); fav.add(f1); fav.add(f2); // We do not use a different db for testing. Thefore we must test only matching items, not all. assertTrue(results.containsAll(fav)); } }