Modification

OBJECTO

Objecto provides a powerful mechanism to customize and adapt generated objects based on specific requirements or scenarios. This flexibility enables developers to tailor objects to their exact needs, ensuring that the generated data aligns with the desired context. Objecto supports the modification of generated objects through both parameters of factory methods and modifier methods.

@Modifier

Modifier Parameters

Specify modification parameters using @Modifier annotation in factory methods to alter specific fields or invoke methods during generation.

Declaration Example:

// Using @Modifier for clarity
Issue createIssue(@Modifier("type") Type type);
Issue createIssue(@Modifier("type=?") Type type);
Issue createIssue(@Modifier("setType(?)") Type type);

// When compiled with -parameters, the parameter name "type" is inferred
Issue createIssue(Type type);

Usage Examples

// Generating an Issue object with specific parameters
Issue randomBug = issueFactory.createIssue(Type.BUG);

Modifier Methods

Define @Modifier methods with a return type of the factory interface itself.

Declaration Examples

@Modifier("type")
IssueFactory withType(Type type);

@Modifier("type=?")
IssueFactory withType(Type type);

@Modifier("setType(?)")
IssueFactory withType(Type type);

Usage Examples

// Using modifier methods for easy modification
Issue randomOpenBug = issueFactory
                          .withType(Type.BUG)
                          .withStatus(Status.OPEN)
                          .withAssignee(user)
                          .withAllSubtaskStatuses(Status.IN_PROGRESS)
                          .createIssue();

Complex Modifiers

Modifiers can also be complex, allowing modification of nested fields or collections.

Examples:

// Modifying the status of all subtasks
@Modifier("subtasks[*].status")
IssueFactory withAllSubtaskStatuses(Status status);

// Modifying the commenter of the first comment
@Modifier("comments[0].commenter=?")
IssueFactory withFirstCommenter(User commenter);

@Modifier("dependencies.put(?, ?)")
IssueFactory withDependency(DependencyType type, Issue issue);

Last updated