Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 서버설정
- 친절한SQL튜닝
- network configuration
- 티스토리 쿠키 삭제
- 페이징
- resttemplate
- /etc/network/interfaces
- baeldung
- 코드로배우는스프링부트웹프로젝트
- iterator
- 데비안
- 이터레이터
- 선형대수
- 코드로배우는스프링웹프로젝트
- 목록처리
- 자바편
- 자료구조와함께배우는알고리즘입문
- d
- ㅒ
- 스프링 시큐리티
- 네트워크 설정
- 처음 만나는 AI수학 with Python
- Kernighan의 C언어 프로그래밍
- 스프링부트핵심가이드
- 처음 만나는 AI 수학 with Python
- 구멍가게코딩단
- 리눅스
- GIT
- 자료구조와 함께 배우는 알고리즘 입문
- 알파회계
Archives
- Today
- Total
bright jazz music
[bootBoard] N:1(다대일) 연관관계: 5. 연관 관계 추가 본문
연관관계 테스트
현재 3개의 테이블이 PK와 FK의 관계로 이루어져 있다. 따라서 테스트를 위한 데이터를 추가하는 작업도 PK 쪽에서부터 시작하는 것이 좋다.
- 테스트를 위해 프로젝트 내 test 폴더에 repository 폴더를 생성한다.
- 그 안에 각 엔티티 클래스를 다루는 아래의 클래스를 생성한다.
- MemberRepositoryTests, BoardRepositoryTests, ReplyRepositoryTests
테스트 데이터 추가하기
MemberRepositoryTests.java
MemberRepository를 주입하고 예제로 사용할 Member객체를 100개 추가한다.
package com.example.bootboard.repository;
import com.example.bootboard.entity.Member;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.stream.IntStream;
@SpringBootTest
public class MemberRepositoryTests {
@Autowired
private MemberRepository memberRepository;
@Test
public void insertMembers(){
IntStream.rangeClosed(1, 100).forEach(i -> {
Member member = Member.builder()
.email("user" + i + "@aaa.com")
.password("11111")
.name("USER" + i)
.build();
memberRepository.save(member);
});
}
}
테스트 코드 실행
BoardRepositoryTests.java
BoardRepositoryTests 코드 역시 동일한 패키지에 추가한다.
앞에서 만들어진 member테이블을 이용해서 Board객체를 생성해서 추가하는 테스트 코드를 작성한다.
package com.example.bootboard.repository;
import com.example.bootboard.entity.Board;
import com.example.bootboard.entity.Member;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.stream.IntStream;
@SpringBootTest
public class BoardRepositoryTests {
@Autowired
private BoardRepository boardRepository;
@Test
public void insertBoard(){
IntStream.rangeClosed(1, 100).forEach(i -> {
Member member = Member.builder().email("user" + i + "@aaa.com").build();
Board board = Board.builder()
.title("Title..." + i)
.content("Content..." + i)
.writer(member)
.build();
boardRepository.save(board);
});
}
}
/*
* testInsert()는 한 명의 사용자가 하나의 게시물을 등록하도록 작성되었다.
* */
테스트 코드 실행
ReplyRepositoryTests.java
댓글은 ReplyRepositoryTests 클래스를 작성해서 특정한 임의의 게시글을 대상으로 댓글을 추가한다.
현재 게시글은 1번~100번까지의 임의 번호를 사용해서 300개의 댓글을 추가한다.
package com.example.bootboard.repository;
import com.example.bootboard.entity.Board;
import com.example.bootboard.entity.Reply;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.stream.IntStream;
@SpringBootTest
public class ReplyRepositoryTests {
@Autowired
private ReplyRepository replyRepository;
@Test
public void insertReply() {
IntStream.rangeClosed(1, 300).forEach(i -> {
//1~100까지의 임의 번호
long bno = (long)(Math.random() * 100) + 1;
Board board = Board.builder().bno(bno).build();
Reply reply = Reply.builder()
.text("Reply......" + i)
.board(board)
.replyer("guest")
.build();
replyRepository.save(reply);
});
}
}
'Framework > Spring' 카테고리의 다른 글
[bootBoard] N:1(다대일) 연관관계: 6-2. @ManyToOne과 Lazy loading (0) | 2022.09.27 |
---|---|
[bootBoard] N:1(다대일) 연관관계: 6-1. @ManyToOne과 Eager loading (0) | 2022.09.27 |
스프링 부트 DB 설정 없이 구동하는 법 (0) | 2022.09.05 |
[bootBoard] N:1(다대일) 연관관계: 4. @ManyToOne 어노테이션 추가 (0) | 2022.07.26 |
[bootBoard] N:1(다대일) 연관관계: 3. 프로젝트 구성 요소 작성(추가) (0) | 2022.07.26 |
Comments