이번에 면접을 준비하면서 실무에서는 TDD 개념을 활용해 코드에 대한 피드백 및 협업을 진행한다는 것을 알게 되었다. 빠르게 개발하는 과정에서는 적용할 필요 없지만 이렇게 공부하는 과정에서 적용을 하며 정리를 하는 것도 괜찮은 듯하다. Django-Rest-Framework에서는 APITestCase
를 활용해 Test를 하지만 Spring에서는 오늘의 주제인 @DataJPATest를 활용해 Test를 진행한다. 한번 케이스를 만들어보자.
[Spring] JPA 와 AWS 연동하기 저번에 생성해놓은 AWS 과정을 따라하면서 세팅을 따라하시면 됩니다.
@Entity
@Table(name = "employees")
public class Employee {
private long id;
private String firstName;
private String lastName;
private String emailId;
public Employee() {}
public Employee(String firstName, String lastName, String emailId) {
this.firstName = firstName;
this.lastName = lastName;
this.emailId = emailId;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@Column(name = "first_name", nullable = false)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name", nullable = false)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Column(name = "email_address", nullable = false)
public String getEmailId() {
return emailId;
}
public void setEmailId(String emailId) {
this.emailId = emailId;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", emailId=" + emailId
+ "]";
}
}
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
Employee findByFirstName(String username);
}
@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = Replace.NONE)
public class EmployRepositoryTests {
@Autowired
private EmployeeRepository employeeRepository;
@Test
public void testSaveEmployee() {
Employee employee = new Employee("admin", "admin", "admin@gmail.com");
employeeRepository.save(employee);
Employee employee2 = employeeRepository.findByFirstName("admin");
assertNotNull(employee);
assertEquals(employee2.getFirstName(), employee.getFirstName());
assertEquals(employee2.getLastName(), employee.getLastName());
}
@Test
public void testGetEmployee() {
Employee employee = new Employee("admin", "admin", "admin@gmail.com");
employeeRepository.save(employee);
Employee employee2 = employeeRepository.findByFirstName("admin");
assertNotNull(employee);
assertEquals(employee2.getFirstName(), employee.getFirstName());
assertEquals(employee2.getLastName(), employee.getLastName());
}
@Test
public void testDeleteEmployee() {
Employee employee = new Employee("admin", "admin", "admin@gmail.com");
employeeRepository.save(employee);
employeeRepository.delete(employee);
}
@Test
public void findAllEmployees() {
Employee employee = new Employee("admin", "admin", "admin@gmail.com");
employeeRepository.save(employee);
assertNotNull(employeeRepository.findAll());
}
@Test
public void deleteByEmployeeIdTest() {
Employee employee = new Employee("admin", "admin", "admin@gmail.com");
Employee emp = employeeRepository.save(employee);
employeeRepository.deleteById(emp.getId());
}
}
testImplementation ('org.springframework.boot:spring-boot-starter-test') {
exclude module: 'junit'
}
testImplementation("org.junit.jupiter:junit-jupiter-api:5.0.0-M3")
testCompileOnly 'org.junit.jupiter:junit-jupiter-params:5.2.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.2.0'
testImplementation 'org.hamcrest:hamcrest-library:2.1'
@ExtendWith(SpringExtension.class)
로 사용해야 한다.@ExtendWith(SpringExtension.class)