Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch02] Refactored UserDaoTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 authored and Dongho Sim committed Jul 29, 2017
1 parent 1ec2601 commit 5c05fb5
Showing 1 changed file with 42 additions and 36 deletions.
78 changes: 42 additions & 36 deletions src/test/java/ch01/springbook/user/UserDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import java.sql.SQLException;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
Expand All @@ -15,64 +17,68 @@

public class UserDaoTest {

@Test
public void addAndGet() throws SQLException, ClassNotFoundException {
ApplicationContext applicationContext =
private static ApplicationContext applicationContext;

private UserDao userDao;

private User user1;
private User user2;
private User user3;

@BeforeClass
public static void init() {
applicationContext =
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);
}

User user1 = new User("whiteship", "test", "no1");
User user2 = new User("blackship", "test", "no2");
@Before
public void setUp() {
this.userDao = applicationContext.getBean("userDao", UserDao.class);

user1 = new User("test01", "test", "no1");
user2 = new User("test02", "test", "no2");
user3 = new User("test03", "test", "no3");
}

@Test
public void addAndGet() throws SQLException, ClassNotFoundException {
// Delete Test
dao.deleteAll();
assertThat(dao.getCount(), is(0));
userDao.deleteAll();
assertThat(userDao.getCount(), is(0));

dao.add(user1);
dao.add(user2);
assertThat(dao.getCount(), is(2));
userDao.add(user1);
userDao.add(user2);
assertThat(userDao.getCount(), is(2));

User userget1 = dao.get(user1.getId());
User userget1 = userDao.get(user1.getId());
assertThat(userget1.getName(), is(user1.getName()));
assertThat(userget1.getPassword(), is(user1.getPassword()));

User userget2 = dao.get(user2.getId());
User userget2 = userDao.get(user2.getId());
assertThat(userget2.getName(), is(user2.getName()));
assertThat(userget2.getPassword(), is(user2.getPassword()));
}

@Test
public void count() throws SQLException, ClassNotFoundException {
ApplicationContext applicationContext =
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);

User user1 = new User("test01", "test", "no1");
User user2 = new User("test02", "test", "no2");
User user3 = new User("test03", "test", "no3");
userDao.deleteAll();
assertThat(userDao.getCount(), is(0));

dao.deleteAll();
assertThat(dao.getCount(), is(0));
userDao.add(user1);
assertThat(userDao.getCount(), is(1));

dao.add(user1);
assertThat(dao.getCount(), is(1));
userDao.add(user2);
assertThat(userDao.getCount(), is(2));

dao.add(user2);
assertThat(dao.getCount(), is(2));

dao.add(user3);
assertThat(dao.getCount(), is(3));
userDao.add(user3);
assertThat(userDao.getCount(), is(3));
}

@Test(expected = EmptyResultDataAccessException.class)
public void getUserFailure() throws SQLException, ClassNotFoundException {
ApplicationContext applicationContext =
new GenericXmlApplicationContext("applicationContext.xml");
UserDao dao = applicationContext.getBean("userDao", UserDao.class);

dao.deleteAll();
assertThat(dao.getCount(), is(0));
userDao.deleteAll();
assertThat(userDao.getCount(), is(0));

dao.get("unknown");
userDao.get("unknown");
}
}

0 comments on commit 5c05fb5

Please sign in to comment.