▶ Springboot TEST
=> src/test 내부에 ApplicationTest.java 파일을 사용
1. Repository TEST
// Emp.java //
@Entity
@Data
public class Emp {
@Id
Integer empno;
@Column(nullable = true)
String ename;
String job;
Integer mgr;
String hiredate;
Integer sal;
Integer comm;
@ManyToOne
@JoinColumn(name = "deptno")
Dept depts;
}
// Dept.java //
@Entity
@Data
@ToString(exclude = ("emps")) // springboot 서버 내에서 System.out.print 구문을 쓸 때에 필요한 구문. 되도록 쓰는게 좋음.
public class Dept {
@Id
int deptno;
@Column(nullable = true)
String dname;
String loc;
@JsonIgnore
@OneToMany(mappedBy = "depts", fetch = FetchType.EAGER)
List<Emp> emps = new ArrayList<>();
}
// Application.Tests.java //
@SpringBootTest
class BasicApplicationTests {
@Autowired
DeptRepository deptRepository;
@Autowired
EmpRepository empRepository;
@Test @Transactional // Transactional 방식을 많이 사용
void deptTableCheck() {
List<Dept> list = deptRepository.findAll();
System.out.println(list);
}
@Test @Transactional
void empTableCheck() {
List<Emp> list = empRepository.findAll();
System.out.println(list);
}
}
=> 서로 양방향 조인중인 dept, emp 엔티티들(테이블)의 repository TEST
※ 빠른 조회(EAGER) : 미리 조회(fetch 사용) -> @Transactional : 접속 해제 하지 않는 애너테이션 사용