我将创建一个课程模型,其中包含基于课程主题的字段:
教训:
@Data
public class Lesson {
private Long id;
private Course course;
private String title;
private String description;
}
字符串
课程:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Course {
private Long code;
private String subject;
private String description;
private Set<User> users;
}
public static final Model<Lesson> LESSON_TEST_MODEL = Instancio.of(Lesson.class)
.supply(field(Lesson::getId), () -> START_LESSON_ID.getAndSet(START_LESSON_ID.get() + DEFAULT_INCREMENT_STEP))
.supply(field(Lesson::getCourse), () -> Instancio.of(COURSE_TEST_MODEL).create())
.assign(given(field(Course::getSubject), field(Lesson::getTitle))
.set(When.is("Mathematics"), "Math Lesson")
.set(When.is("History"), "History Lesson")
.set(When.is("Literature"), "Literature Lesson")
.set(When.is("Physics"), "Physics Lesson")
.set(When.is("Computer Science"), "Computer Science Lesson"))
.assign(given(field(Course::getSubject), field(Lesson::getDescription))
.set(When.is("Mathematics"), "Lesson on mathematics concepts")
.set(When.is("History"), "Lesson on world history")
.set(When.is("Literature"), "Lesson on classical literature")
.set(When.is("Physics"), "Lesson on fundamental physics")
.set(When.is("Computer Science"), "Lesson on computer programming"))
.toModel();
型
但是提供这行代码会导致异常:
Reason: unresolved assignment expression
The following assignments could not be applied:
-> from [field(Course, "subject")] to [field(Lesson, "title")]
-> from [field(Course, "subject")] to [field(Lesson, "description")]
As a result, the following targets could not be assigned a value:
-> field Lesson.description (depth=1)
-> field Lesson.title (depth=1)
型
如何解决这个问题?
1条答案
按热度按时间mwkjh3gx1#
发生此错误的原因是您正在使用
supply()
设置Lesson.course
字段:字符串
实际上,您正在创建一个单独的
Course
示例,然后将其传递给supply()
。由于Course
是单独创建的,所以对Instancio.create()
的第二次调用不知道课程包含哪些值。因此,不计算赋值并引发错误。这可以通过在单个
Instancio.create()
调用中生成整个Lesson
(包括Course
)来解决:型