-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 커넥션 객체를 JdbcTemplate이 관리하도록 변경
- Loading branch information
Showing
8 changed files
with
73 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,17 +27,17 @@ void setup() throws SQLException { | |
|
||
userDao = new UserDao(DataSourceConfig.getInstance()); | ||
final var user = new User("gitchan", "password", "[email protected]"); | ||
userDao.insert(conn, user); | ||
userDao.insert(user); | ||
} | ||
|
||
@AfterEach | ||
void tearDown() { | ||
userDao.deleteAll(conn); | ||
userDao.deleteAll(); | ||
} | ||
|
||
@Test | ||
void findAll() { | ||
final var users = userDao.findAll(conn); | ||
final var users = userDao.findAll(); | ||
|
||
assertAll( | ||
() -> assertThat(users).isNotEmpty(), | ||
|
@@ -47,7 +47,7 @@ void findAll() { | |
|
||
@Test | ||
void findById() { | ||
final var findUser = userDao.findById(conn, 1L).get(); | ||
final var findUser = userDao.findById(1L).get(); | ||
|
||
assertAll( | ||
() -> assertThat(findUser.getAccount()).isEqualTo("gitchan"), | ||
|
@@ -58,8 +58,7 @@ void findById() { | |
|
||
@Test | ||
void findByAccount() { | ||
final String account = "gitchan"; | ||
final User findUser = userDao.findByAccount(conn, account).get(); | ||
final User findUser = userDao.findByAccount("gitchan").get(); | ||
|
||
assertAll( | ||
() -> assertThat(findUser.getAccount()).isEqualTo("gitchan"), | ||
|
@@ -72,22 +71,22 @@ void findByAccount() { | |
void insert() { | ||
final var account = "insert-gitchan"; | ||
final var user = new User(account, "password", "[email protected]"); | ||
userDao.insert(conn, user); | ||
userDao.insert(user); | ||
|
||
final var actual = userDao.findById(conn, 2L).get(); | ||
final var actual = userDao.findById(2L).get(); | ||
|
||
assertThat(actual.getAccount()).isEqualTo(account); | ||
} | ||
|
||
@Test | ||
void update() { | ||
final var newPassword = "password99"; | ||
final var user = userDao.findById(conn, 1L).get(); | ||
final var user = userDao.findById(1L).get(); | ||
user.changePassword(newPassword); | ||
|
||
userDao.update(conn, user); | ||
userDao.update(user); | ||
|
||
final var actual = userDao.findById(conn, 1L).get(); | ||
final var actual = userDao.findById(1L).get(); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,6 @@ | |
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
|
||
import java.sql.SQLException; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
|
@@ -21,42 +19,44 @@ class AppUserServiceTest { | |
private UserDao userDao; | ||
|
||
@BeforeEach | ||
void setUp() throws SQLException { | ||
void setUp() { | ||
this.jdbcTemplate = new JdbcTemplate(DataSourceConfig.getInstance()); | ||
this.userDao = new UserDao(jdbcTemplate); | ||
|
||
DatabasePopulatorUtils.execute(DataSourceConfig.getInstance()); | ||
final var user = new User("gugu", "password", "[email protected]"); | ||
userDao.insert(jdbcTemplate.getDataSource().getConnection(), user); | ||
userDao.insert(user); | ||
} | ||
|
||
@Test | ||
void testChangePassword() throws SQLException { | ||
void testChangePassword() { | ||
final var userHistoryDao = new UserHistoryDao(jdbcTemplate); | ||
final var userService = new AppUserService(userDao, userHistoryDao, jdbcTemplate.getDataSource()); | ||
final var appUserService = new AppUserService(userDao, userHistoryDao, jdbcTemplate.getDataSource()); | ||
final TxUserService userService = new TxUserService(jdbcTemplate.getDataSource(), appUserService); | ||
|
||
final var newPassword = "qqqqq"; | ||
final var createBy = "gugu"; | ||
userService.changePassword(1L, newPassword, createBy); | ||
appUserService.changePassword(1L, newPassword, createBy); | ||
|
||
final var actual = userService.findById(1L); | ||
|
||
assertThat(actual.getPassword()).isEqualTo(newPassword); | ||
} | ||
|
||
@Test | ||
void testTransactionRollback() throws SQLException { | ||
void testTransactionRollback() { | ||
// 트랜잭션 롤백 테스트를 위해 mock으로 교체 | ||
final MockUserHistoryDao userHistoryDao = new MockUserHistoryDao(jdbcTemplate); | ||
final AppUserService appUserService = new AppUserService(userDao, userHistoryDao, jdbcTemplate.getDataSource()); | ||
final TxUserService userService = new TxUserService(jdbcTemplate.getDataSource(), appUserService); | ||
|
||
final var newPassword = "newPassword"; | ||
final var createBy = "gugu"; | ||
|
||
// 트랜잭션이 정상 동작하는지 확인하기 위해 의도적으로 MockUserHistoryDao에서 예외를 발생시킨다. | ||
assertThrows( | ||
DataAccessException.class, | ||
() -> appUserService.changePassword(1L, newPassword, createBy) | ||
() -> userService.changePassword(1L, newPassword, createBy) | ||
); | ||
|
||
final var actual = appUserService.findById(1L); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.