java Instancio基于另一个类字段赋值

06odsfpq  于 2023-08-01  发布在  Java
关注(0)|答案(1)|浏览(107)

我将创建一个课程模型,其中包含基于课程主题的字段:
教训:

@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)


如何解决这个问题?

mwkjh3gx

mwkjh3gx1#

发生此错误的原因是您正在使用supply()设置Lesson.course字段:

.supply(field(Lesson::getCourse), () -> Instancio.of(COURSE_TEST_MODEL).create())

字符串
实际上,您正在创建一个单独的Course示例,然后将其传递给supply()。由于Course是单独创建的,所以对Instancio.create()的第二次调用不知道课程包含哪些值。因此,不计算赋值并引发错误。
这可以通过在单个Instancio.create()调用中生成整个Lesson(包括Course)来解决:

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))
    // generate Course as part of the Lesson instead of using supply()
    .generate(field(Course::getSubject), gen -> gen.oneOf("Mathematics", "History", "Literature", "Physics", "Computer Science"))
    .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();

相关问题