작성
·
315
0
안녕하세요. 질문 제목에 관해 말씀드리겠습니다.
상황은 이러합니다.
member table에 jdbc 탬플릿으로 잘 저장하고 잘 조회하나 테스트하는 중으로 테이블은 아래와 같습니다.
CREATE TABLE member
(
member_id BIGINT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(45),
login_id VARCHAR(45),
password VARCHAR(45),
email VARCHAR(45) UNIQUE
);
레포지토리는 아래와 같습니다.
package com.boaz.sketch2fashion.repository.member;
import com.boaz.sketch2fashion.domain.Member;
import com.boaz.sketch2fashion.repository.member.MemberRepository;
import com.boaz.sketch2fashion.repository.member.dto.MemberSaveDTO;
import com.boaz.sketch2fashion.repository.member.dto.MemberUpdateDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;
import javax.sql.DataSource;
import java.security.Signature;
import java.util.List;
import java.util.Map;
@Slf4j
@Repository
public class JdbcMemberRepository implements MemberRepository {
private final NamedParameterJdbcTemplate template;
public JdbcMemberRepository(DataSource dataSource) {
this.template = new NamedParameterJdbcTemplate(dataSource);
}
@Override
public Member save(Member member) {
String sql = "insert into member(name, login_id, password, email) values(:name, :loginId, :password, :email)";
KeyHolder keyHolder = new GeneratedKeyHolder();
SqlParameterSource param = new BeanPropertySqlParameterSource(member);
template.update(sql, param, keyHolder);
long key = keyHolder.getKey().longValue();
member.setId(key);
return member;
}
@Override
public Member findById(Long id) {
String sql = "select * from member where member_id = :id";
Map<String, Object> param = Map.of("id", id);
Member member = template.queryForObject(sql, param, memberRowMapper());
return member;
}
@Override
public Member findByLoginId(String loginId) {
String sql = "select * from member where login_id = :loginId";
Map<String, Object> param = Map.of("loginId", loginId);
Member member = template.queryForObject(sql, param, memberRowMapper());
return member;
}
@Override
public void update(Long id, MemberUpdateDTO memberUpdateDTO) {
String sql = "update member set name = :name, login_id = :loginId, password = :password, email = :email where id = :id";
SqlParameterSource param = new MapSqlParameterSource()
.addValue("name", memberUpdateDTO.getName())
.addValue("loginId", memberUpdateDTO.getLoginId())
.addValue("password", memberUpdateDTO.getPassword())
.addValue("email", memberUpdateDTO.getEmail());
template.update(sql, param);
}
@Override
public void delete(Long id) {
String sql = "delete from member where id = :id";
Map<String, Object> param = Map.of("id", id);
template.update(sql, param);
}
private RowMapper<Member> memberRowMapper() {
return BeanPropertyRowMapper.newInstance(Member.class);
}
}
테스트 코드는 아래와 같습니다.
@Test
void findById() {
// given
Member memberA = new Member("a", "a", "a", "a");
// when
Member saveMember = memberRepository.save(memberA);
log.info("{}", saveMember.getId());
Member findMember = memberRepository.findById(saveMember.getId());
log.info("{}", saveMember.getId());
log.info("{}", findMember.getId());
// then
assertThat(saveMember).isEqualTo(findMember);
}
memberA를 저장하고 findById로 찾아서 saveMember와 findMember의 내용물이 같은지 확인하는 코드입니다.
memberA를 저장하면 pk는 1이 될 것이라고 생각하여 findById(saveMember.getId());로 H2 DB에서 pk가 1인 행을 찾아 findMember로 반환하여 isEqualTo를 하려고 했습니다.
결과 이미지
saveMember의 getId 1로 findMember를 찾았는데 findMember의 getId는 null입니다.
따라서 테스트가 실패합니다.
테스트 환경
H2 스프링 내장 DB, jdk 17
항상 질문 답변 해주셔서 정말 감사합니다.