|
| 1 | +package com.baeldung.spring.data.keyvalue.services.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.junit.BeforeClass; |
| 8 | +import org.junit.FixMethodOrder; |
| 9 | +import org.junit.Test; |
| 10 | +import org.junit.runner.RunWith; |
| 11 | +import org.junit.runners.MethodSorters; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 14 | +import org.springframework.boot.test.context.SpringBootTest; |
| 15 | +import org.springframework.data.keyvalue.core.KeyValueTemplate; |
| 16 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 17 | + |
| 18 | +import com.baeldung.spring.data.keyvalue.SpringDataKeyValueApplication; |
| 19 | +import com.baeldung.spring.data.keyvalue.services.EmployeeService; |
| 20 | +import com.baeldung.spring.data.keyvalue.vo.Employee; |
| 21 | + |
| 22 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 23 | +@SpringBootTest(classes = SpringDataKeyValueApplication.class) |
| 24 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 25 | +public class EmployeeServicesWithKeyValueTemplateTest { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + @Qualifier("employeeServicesWithKeyValueTemplate") |
| 29 | + EmployeeService employeeService; |
| 30 | + |
| 31 | + @Autowired |
| 32 | + @Qualifier("keyValueTemplate") |
| 33 | + KeyValueTemplate keyValueTemplate; |
| 34 | + |
| 35 | + static Employee employee1; |
| 36 | + |
| 37 | + static Employee employee2; |
| 38 | + |
| 39 | + @BeforeClass |
| 40 | + public static void setUp() { |
| 41 | + employee1 = new Employee(1, "Karan", "IT", "5000"); |
| 42 | + employee2 = new Employee(2, "Jack", "HR", "2000"); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void test1_whenEmployeeSaved_thenEmployeeIsAddedToMap() { |
| 47 | + employeeService.save(employee1); |
| 48 | + assertEquals(keyValueTemplate.findById(1, Employee.class).get(), employee1); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void test2_whenEmployeeGet_thenEmployeeIsReturnedFromMap() { |
| 53 | + Employee employeeFetched = employeeService.get(1); |
| 54 | + assertEquals(employeeFetched, employee1); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void test3_whenEmployeesFetched_thenEmployeesAreReturnedFromMap() { |
| 59 | + List<Employee> employees = (List<Employee>)employeeService.fetchAll(); |
| 60 | + assertEquals(employees.size(), 1); |
| 61 | + assertEquals(employees.get(0), employee1); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void test4_whenEmployeeUpdated_thenEmployeeIsUpdatedToMap() { |
| 66 | + employee1.setName("Pawan"); |
| 67 | + employeeService.update(employee1); |
| 68 | + assertEquals(keyValueTemplate.findById(1, Employee.class).get().getName(),"Pawan"); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void test5_whenSortedEmployeesFetched_thenEmployeesAreReturnedFromMapInOrder() { |
| 73 | + employeeService.save(employee2); |
| 74 | + List<Employee> employees = (List<Employee>)employeeService.getSortedListOfEmployeesBySalary(); |
| 75 | + assertEquals(employees.size(), 2); |
| 76 | + assertEquals(employees.get(0), employee1); |
| 77 | + assertEquals(employees.get(1), employee2); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void test6_whenEmployeeDeleted_thenEmployeeIsRemovedMap() { |
| 82 | + employeeService.delete(1); |
| 83 | + assertEquals(keyValueTemplate.findById(1, Employee.class).isPresent(), false); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + |
| 88 | +} |
0 commit comments