728x90
반응형
SMALL
@Test
	void testJpa() {
		//findBySubjectLike
		List<Question> qList = this.questionRepostiory.findBySubjectLike("sbb%");
		Question q = qList.get(0);
		assertEquals("sbb가 무엇인가요?", q.getSubject());
		
	}

리포지터리 생성

앞서 만든 엔티티에 대해 데이터들을 CRUD할 수 있도록 도와주는 인터페이스를 작성한다.

package com.mysite.sbb;

import org.springframework.data.jpa.repository.JpaRepository;

public interface QuestionRepostiory extends JpaRepository<Question, Integer> {

}
package com.mysite.sbb;

import org.springframework.data.jpa.repository.JpaRepository;

public interface AnswerRepository extends JpaRepository<Answer, Integer> {

}

JpaRepository를 상속받아야 함.

 

JUnit 설치

//JUnit
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly 'org..junit.platform:junit-platform-launcher'

build.gradle 수정.

 

질문 데이터 저장하기 (Test 이용)

package com.mysite.sbb;

import java.time.LocalDateTime;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SbbApplicationTests {
	
	@Autowired
	private QuestionRepostiory questionRepostiory;

	@Test
	void testJpa() {
		Question q1 = new Question();
		q1.setSubject("sbb가 무엇인가요?");
		q1.setContent("sbb에 대해서 알고 싶습니다.");
		q1.setCreateDate(LocalDateTime.now());
		this.questionRepostiory.save(q1);
		
		Question q2 = new Question();
		q2.setSubject("스프링 부트 모델 질문입니다.");
		q2.setContent("id는 자동으로 생성되나요?");
		q2.setCreateDate(LocalDateTime.now());
		this.questionRepostiory.save(q2);
	}

}

테스트 실행은 [Run As] -> [Junit Test]
* 테스트를 실행할 때는 server를 끄고 실행해야함!!

@Autowired 어노테이션은 repository를 new Repository();로 생성하지 않고 spring이 만들어주는 것을 사용하겠다는 의미.

데이터가 잘 들어간 것을 확인할 수 있음.

 

질문 데이터 조회하기

findAll()

//질문 조회하기
List<Question> all = this.questionRepostiory.findAll();
assertEquals(2, all.size());
		
Question q = all.get(0);
assertEquals("sbb가 무엇인가요?", q.getSubject());

test 코드에 추가.

 

* asserEquals 메서드는 jupiter에서 제공함. 두 인자값을 비교해주는 역할.
* findAll() 메서드는 jpa에서 제공하는 기본 메서드.

테스트 성공.

 

findById()

id값으로 찾기

@Test
	void testJpa() {
		//findById
		Optional<Question> oq = this.questionRepostiory.findById(1);
		if (oq.isPresent()) {
			Question question = oq.get();
			assertEquals("sbb가 무엇인가요?", question.getSubject());
		}
	}

 

findBySubject()

findBySubject는 JPA에서 제공해주는 것이 아니기 때문에 직접 선언을 해주어야 한다.
그럼 select * from question where subject=? 형식으로 쿼리를 자동으로 날린다.
package com.mysite.sbb;

import org.springframework.data.jpa.repository.JpaRepository;

public interface QuestionRepostiory extends JpaRepository<Question, Integer> {
	Question findBySubject(String subject);
}
@Test
	void testJpa() {
		//findBySubject
		Question question = this.questionRepostiory.findBySubject("sbb가 무엇인가요?");
		assertEquals(1, question.getId());
	}

 

 

findBySubjectAndContent()

Question findbySubjectAndContent(String subject, String content);

QuestionRepository에 메서드 추가.

@Test
	void testJpa() {
		//findBySubjectAndContent
		Question q = this.questionRepostiory.findbySubjectAndContent("sbb가 무엇인가요?",
				"sbb에 대해서 알고 싶습니다.");
		assertEquals(1, q.getId());
	}

 

findBySubjectList()

List<Question> findBySubjectLike(String subject);

@Test
	void testJpa() {
		//findBySubjectLike
		List<Question> qList = this.questionRepostiory.findBySubjectLike("sbb%");
		Question q = qList.get(0);
		assertEquals("sbb가 무엇인가요?", q.getSubject());
		
	}

 

질문 데이터 수정하기

@Test
	void testJpa() {
		//질문 데이터 수정하기
		Optional<Question> oq= this.questionRepostiory.findById(1);
		assertTrue(oq.isPresent());
		Question q = oq.get();
		q.setSubject("수정된 제목");
		this.questionRepostiory.save(q);
	}

제목이 수정된 것을 확인할 수 있음.

 

질문 데이터 삭제하기

@Test
	void testJpa() {
		//질문 데이터 삭제하기
		assertEquals(2, this.questionRepostiory.count());
		Optional<Question> oq= this.questionRepostiory.findById(1);
		assertTrue(oq.isPresent());
		Question q = oq.get();
		this.questionRepostiory.delete(q);
		assertEquals(1, this.questionRepostiory.count());
	}

ID가 1인 question이 삭제됨.

728x90
반응형
LIST

+ Recent posts