Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cycle 6: RED, Service: save employee
- Loading branch information
gungor
committed
May 4, 2021
1 parent
2d293f5
commit 20a2468
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package tddexample.repository; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import tddexample.model.entity.Employee; | ||
|
||
public interface EmployeeRepository extends JpaRepository<Employee, Integer> { | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package tddexample.service; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit.jupiter.SpringExtension; | ||
import tddexample.model.entity.Employee; | ||
import tddexample.model.rest.EmployeeSaveRequest; | ||
import tddexample.repository.EmployeeRepository; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS) | ||
@ExtendWith(SpringExtension.class) | ||
@SpringBootTest | ||
public class EmployeeServiceTest { | ||
|
||
@Autowired | ||
private EmployeeService employeeService; | ||
|
||
@MockBean | ||
private EmployeeRepository employeeRepository; | ||
|
||
@Test | ||
public void shouldSaveEmployee(){ | ||
Employee employee = new Employee(20, "Frodo Baggins"); | ||
when(employeeRepository.save(new Employee(null, "Frodo Baggins"))) | ||
.thenReturn(employee); | ||
|
||
EmployeeSaveRequest request = new EmployeeSaveRequest("Frodo Baggins"); | ||
Employee savedEmployee = employeeService.saveEmployee(request); | ||
|
||
verify(employeeRepository,times(1)).save(any(Employee.class)); | ||
Assertions.assertEquals(employee,savedEmployee); | ||
} | ||
|
||
} |