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