#21 jdbc: create test

This commit is contained in:
haerong22
2022-09-19 03:46:34 +09:00
parent abc21bfa82
commit 53c5d34a3b
3 changed files with 143 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
package org.example;
import java.util.Objects;
public class User {
private final String userId;
private final String password;
private final String name;
private final String email;
public User(String userId, String password, String name, String email) {
this.userId = userId;
this.password = password;
this.name = name;
this.email = email;
}
public String getUserId() {
return userId;
}
public String getPassword() {
return password;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof User)) return false;
User user = (User) o;
return Objects.equals(userId, user.userId) && Objects.equals(password, user.password) && Objects.equals(name, user.name) && Objects.equals(email, user.email);
}
@Override
public int hashCode() {
return Objects.hash(userId, password, name, email);
}
}

View File

@@ -0,0 +1,83 @@
package org.example;
import java.sql.*;
public class UserDao {
private Connection getConnection() {
String url = "jdbc:h2:mem://localhost/~/jdbc-practice;MODE=MySQL;DB_CLOSE_DELAY=-1";
String id = "sa";
String pw = "";
try {
Class.forName("org.h2.Driver");
return DriverManager.getConnection(url, id, pw);
} catch (Exception e) {
return null;
}
}
public void create(User user) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = getConnection();
String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getEmail());
pstmt.executeUpdate();
} finally {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
}
}
public User findByUserId(String userId) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = getConnection();
String sql = "SELECT userId, password, name, email FROM USERS WHERE userId = ?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, userId);
rs = pstmt.executeQuery();
User user = null;
if (rs.next()) {
user = new User(
rs.getString("userId"),
rs.getString("password"),
rs.getString("name"),
rs.getString("email")
);
}
return user;
} finally {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
}
}
}

View File

@@ -1,10 +1,14 @@
package org.example;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import java.sql.SQLException;
public class UserDaoTest {
@BeforeEach
@@ -14,5 +18,15 @@ public class UserDaoTest {
DatabasePopulatorUtils.execute(populator, ConnectionManager.getDataSource());
}
@Test
void createTest() throws SQLException {
UserDao userDao = new UserDao();
userDao.create(new User("bobby", "pass", "name", "email"));
User user = userDao.findByUserId("bobby");
Assertions.assertThat(user).isEqualTo(new User("bobby", "pass", "name", "email"));
}
}