Skip to content

Commit c3a10bb

Browse files
authored
Update CourseServiceUnitTest.java
1 parent 90c5d56 commit c3a10bb

File tree

1 file changed

+28
-30
lines changed

1 file changed

+28
-30
lines changed

libraries-apache-commons/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
55
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
67
import org.junit.jupiter.api.BeforeEach;
78
import org.apache.commons.beanutils.PropertyUtils;
89
import java.lang.reflect.InvocationTargetException;
@@ -14,50 +15,47 @@
1415

1516
public class CourseServiceUnitTest {
1617

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-
3518
@Test
3619
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
3826
String courseName = (String) PropertyUtils.getSimpleProperty(course, "name");
3927

40-
assertNotNull(courseName);
41-
assertEquals(COURSE_NAME, courseName);
28+
assertEquals("Computer Science", courseName);
4229
}
4330

4431
@Test
4532
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
4738
String secondCode = (String) PropertyUtils.getIndexedProperty(course, "codes[1]");
48-
49-
assertNotNull(secondCode);
39+
5040
assertEquals("CS102", secondCode);
5141
}
5242

5343
@Test
5444
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);
5849

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());
6159
}
6260

6361
@Test
@@ -96,7 +94,7 @@ public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWit
9694

9795
CourseService.copyProperties(course, courseEntity);
9896
Assert.assertNotNull(course.getName());
99-
Assert.assertNotNull(courseEntity.getName());
97+
Assert.assertNotNull(courseEntity.getName());
10098
Assert.assertEquals(course.getName(), courseEntity.getName());
10199
Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
102100
Assert.assertNull(courseEntity.getStudent("ST-1"));

0 commit comments

Comments
 (0)