Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 22nd, 2012  |  syntax: None  |  size: 1.41 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package org.kimego.demos.db.persistence;
  2.  
  3. import javax.persistence.NoResultException;
  4.  
  5. import junit.framework.Assert;
  6.  
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.kimego.demos.db.domain.User;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.context.ApplicationContext;
  13. import org.springframework.test.context.ContextConfiguration;
  14. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  15.  
  16. @RunWith(SpringJUnit4ClassRunner.class)
  17. @ContextConfiguration("classpath:test-context.xml")
  18. public class UserDaoImplIntegrationTests {
  19.  
  20.         private static final Long USER_ID = 1L;
  21.  
  22.         private static final long NON_EXISTENT_USER_ID = 0;
  23.  
  24.         @Autowired
  25.         private ApplicationContext context;
  26.  
  27.         private IUserDao repo;
  28.  
  29.         @Before
  30.         public void TestsInitialization() {
  31.                 repo = context.getBean(IUserDao.class);
  32.         }
  33.  
  34.  
  35.         @Test
  36.         public void UserDaoImplFindByIdReturnsUserEntityWithThatIdIfItWasFound() {
  37.                 User user = repo.findUserById(USER_ID);
  38.  
  39.                 Assert.assertNotNull(user);
  40.         }
  41.  
  42.         @Test(expected=NoResultException.class)
  43.         public void UserDaoImplFindByIdThrowsNoResultExceptionIfAnEntityWithThatIdWasNotFound() {
  44.                 User user = repo.findUserById(NON_EXISTENT_USER_ID);
  45.  
  46.                 Assert.assertNull(user);
  47.         }
  48.  
  49.         @Test
  50.         public void UserDaoImplSaveStoresNewEntityInDatabase() {
  51.  
  52.                 User user = new User();
  53.  
  54.                 repo.save(user);
  55.  
  56.                 Assert.assertNotNull(repo.findUserById(user.getId()));
  57.         }
  58. }