로컬에서 사용하는 애플리케이션 서버와 테스트에서 같은 데이터베이스를 사용하고 있으니 테스트에서 문제가 발생한다.
이런 문제를 해결하려면 테스트를 다른 환경과 철저하게 분리해야 한다.
가장 간단한 방법은 테스트 전용 데이터베이스를 별도로 운영하는 것이다.
데이터베이스 파일 생성 방법
테이블 생성하기
testcase 데이터베이스에도 item 테이블을 생성하자.
sql/schema.sql 파일 참고
drop table if exists item CASCADE;
create table item
(
id bigint generated by default as identity,
item_name varchar(10),
price integer,
quantity integer,
primary key (id)
);
main - application.properties
src/main/resources/application.properties
spring.profiles.active=local
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.username=sa
이제 테스트를 실행해보자
findItems() 테스트만 단독으로 실행해보자
처음에는 실행에 성공한다.
그런데 같은 findItems() 테스트를 다시 실행하면 테스트에 실패한다.
테스트에서 매우 중요한 원칙은 다음과 같다.