这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,64 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import org.junit.jupiter.api.BeforeEach;
import org.apache.commons.beanutils.PropertyUtils;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;

public class CourseServiceUnitTest {

private static final String STUDENT_ID = "01";

@Test
public void givenCourse_whenGettingSimplePropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Course course = new Course();
String name = "Computer Science";
List<String> codes = Arrays.asList("CS101","CS102");
CourseService.setValues(course, name, codes);

// Use getSimpleProperty to retrieve the 'name' property from the course bean
String courseName = (String) PropertyUtils.getSimpleProperty(course, "name");

assertEquals("Computer Science", courseName);
}

@Test
public void givenCourse_whenGettingIndexedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Course course = new Course();
String name = "Computer Science";
List<String> codes = Arrays.asList("CS101","CS102");
CourseService.setValues(course, name, codes);
// Use getIndexedProperty to retrieve the element at index 1 from the 'codes' list
String secondCode = (String) PropertyUtils.getIndexedProperty(course, "codes[1]");

assertEquals("CS102", secondCode);
}

@Test
public void givenCourse_whenGettingMappedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Course course = new Course();
String name = "Computer Science";
List<String> codes = Arrays.asList("CS101","CS102");
CourseService.setValues(course, name, codes);

// 1. Create and set a Student
Student student = new Student();
student.setName("John Doe");
CourseService.setMappedValue(course, STUDENT_ID, student);
// Use getMappedProperty to retrieve the value associated with the key '01'
// from the 'enrolledStudent' map
Student enrolledStudent = (Student) PropertyUtils.getMappedProperty(course, "enrolledStudent(" + STUDENT_ID + ")");

assertEquals("John Doe", enrolledStudent.getName());
}

@Test
public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Course course = new Course();
Expand Down Expand Up @@ -48,7 +96,7 @@ public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWit

CourseService.copyProperties(course, courseEntity);
Assert.assertNotNull(course.getName());
Assert.assertNotNull(courseEntity.getName());
Assert.assertNotNull(courseEntity.getName());
Assert.assertEquals(course.getName(), courseEntity.getName());
Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
Assert.assertNull(courseEntity.getStudent("ST-1"));
Expand Down