Instancio中根据关联Course字段赋值Lesson报错,如何解决?
问题:如何让Lesson模型字段基于关联Course的subject字段生成?
我想要创建一个Lesson模型,使其字段值基于关联Course的subject字段:
Lesson类代码:
@Data public class Lesson { private Long id; private Course course; private String title; private String description; }
Course类代码及Lesson测试模型:
@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)
解决方案
问题出在你用.supply()方法直接生成Course实例,这会跳过Instancio的内部字段追踪机制,导致它无法关联Course的subject字段和Lesson的title、description字段。
修改方式是去掉.supply()生成Course的代码,改用.withModel()来关联Course的测试模型,让Instancio能完整追踪对象间的关联关系:
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为withModel,让Instancio管理Course实例的创建 .withModel(field(Lesson::getCourse), COURSE_TEST_MODEL) .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();
如果你的COURSE_TEST_MODEL需要自定义生成逻辑,确保它是通过Instancio的Model API构建的,这样Instancio就能正确识别并关联嵌套对象的字段,完成条件赋值。
内容的提问来源于stack exchange,提问作者yaroslav96
相关产品推荐
相关产品推荐

