|
2 | 2 |
|
3 | 3 | import static org.assertj.core.api.Assertions.assertThat;
|
4 | 4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
5 |
| - |
| 5 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 7 | +import org.junit.jupiter.api.BeforeEach; |
| 8 | +import org.apache.commons.beanutils.PropertyUtils; |
6 | 9 | import java.lang.reflect.InvocationTargetException;
|
7 | 10 | import java.util.Arrays;
|
8 | 11 | import java.util.List;
|
9 |
| - |
| 12 | + |
10 | 13 | import org.junit.Assert;
|
11 | 14 | import org.junit.Test;
|
12 | 15 |
|
13 | 16 | public class CourseServiceUnitTest {
|
14 | 17 |
|
| 18 | + private static final String STUDENT_ID = "01"; |
| 19 | + |
| 20 | + @Test |
| 21 | + public void givenCourse_whenGettingSimplePropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 22 | + Course course = new Course(); |
| 23 | + String name = "Computer Science"; |
| 24 | + List<String> codes = Arrays.asList("CS101","CS102"); |
| 25 | + CourseService.setValues(course, name, codes); |
| 26 | + |
| 27 | + // Use getSimpleProperty to retrieve the 'name' property from the course bean |
| 28 | + String courseName = (String) PropertyUtils.getSimpleProperty(course, "name"); |
| 29 | + |
| 30 | + assertEquals("Computer Science", courseName); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void givenCourse_whenGettingIndexedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 35 | + Course course = new Course(); |
| 36 | + String name = "Computer Science"; |
| 37 | + List<String> codes = Arrays.asList("CS101","CS102"); |
| 38 | + CourseService.setValues(course, name, codes); |
| 39 | + // Use getIndexedProperty to retrieve the element at index 1 from the 'codes' list |
| 40 | + String secondCode = (String) PropertyUtils.getIndexedProperty(course, "codes[1]"); |
| 41 | + |
| 42 | + assertEquals("CS102", secondCode); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void givenCourse_whenGettingMappedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { |
| 47 | + Course course = new Course(); |
| 48 | + String name = "Computer Science"; |
| 49 | + List<String> codes = Arrays.asList("CS101","CS102"); |
| 50 | + CourseService.setValues(course, name, codes); |
| 51 | + |
| 52 | + // 1. Create and set a Student |
| 53 | + Student student = new Student(); |
| 54 | + student.setName("John Doe"); |
| 55 | + CourseService.setMappedValue(course, STUDENT_ID, student); |
| 56 | + // Use getMappedProperty to retrieve the value associated with the key '01' |
| 57 | + // from the 'enrolledStudent' map |
| 58 | + Student enrolledStudent = (Student) PropertyUtils.getMappedProperty(course, "enrolledStudent(" + STUDENT_ID + ")"); |
| 59 | + |
| 60 | + assertEquals("John Doe", enrolledStudent.getName()); |
| 61 | + } |
| 62 | + |
15 | 63 | @Test
|
16 | 64 | public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
17 | 65 | Course course = new Course();
|
@@ -48,7 +96,7 @@ public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWit
|
48 | 96 |
|
49 | 97 | CourseService.copyProperties(course, courseEntity);
|
50 | 98 | Assert.assertNotNull(course.getName());
|
51 |
| - Assert.assertNotNull(courseEntity.getName()); |
| 99 | + Assert.assertNotNull(courseEntity.getName()); |
52 | 100 | Assert.assertEquals(course.getName(), courseEntity.getName());
|
53 | 101 | Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
|
54 | 102 | Assert.assertNull(courseEntity.getStudent("ST-1"));
|
|
0 commit comments