Merge pull request #6589 from tinomthomas/master

BAEL - 2829
This commit is contained in:
Eric Martin 2019-03-30 23:44:24 -05:00 committed by GitHub
commit db72c6d2a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,9 @@
package com.baeldung.dao.repositories;
import org.springframework.data.jpa.repository.JpaRepository;
import com.baeldung.domain.Employee;
public interface EmployeeRepository extends JpaRepository<Employee, Long> {
}

View File

@ -0,0 +1,36 @@
package com.baeldung.domain;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Employee {
@Id
private Long id;
private String name;
public Employee() {
}
public Employee(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.dao.repositories;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.domain.Employee;
@RunWith(SpringRunner.class)
@DataJpaTest
public class EmployeeRepositoryIntegrationTest {
private static final Employee EMPLOYEE1 = new Employee(1L, "John");
private static final Employee EMPLOYEE2 = new Employee(2L, "Alice");
@Autowired
private EmployeeRepository employeeRepository;
@Test
public void givenEmployeeEntity_whenInsertWithSave_ThenEmployeeIsPersisted() {
employeeRepository.save(EMPLOYEE1);
assertEmployeePersisted(EMPLOYEE1);
}
@Test
public void givenEmployeeEntity_whenInsertWithSaveAndFlush_ThenEmployeeIsPersisted() {
employeeRepository.saveAndFlush(EMPLOYEE2);
assertEmployeePersisted(EMPLOYEE2);
}
private void assertEmployeePersisted(Employee input) {
Employee employee = employeeRepository.getOne(input.getId());
assertThat(employee).isNotNull();
}
}