Skip to content

Creating objects with Fixtures

Lucas Saldanha edited this page Jul 11, 2015 · 4 revisions

Usage

When creating objects based on fixture files Playdoh will look for a .yml file with the name of its the class (in snake_case). For example, to create an instance of a MailData object you must have a mail_data.yml on your class-path. A fixture file must be a valid yaml file.

Within the fixture file you could place more than one definition of data for different instances of the correspondent object. You specify which one of the data should be use in the Playdoh.build method. For example:

Fixture file with multiple user definitions

aUser:
  name: Lucas Saldanha
  email: [email protected]
  age: 123

anotherUser:
  name: Abdi Abidu
  email: [email protected]
  age: 999

Creating one instance of each user

User user = Playdoh.build(User.class, "aUser");
User user = Playdoh.build(User.class, "anotherUser");

Below there are some examples of fixture files for a variety of objects.

Object/Fixture examples

Simple model object

Class

public class User {

    private String name;
    private String email;
    private int age;
    ...

Fixture

aUser:
  name: Lucas Saldanha
  email: [email protected]
  age: 123

Nested objects

Parent Class

public class Dummy {
    private Integer integerValue;
    private String stringValue;
    private NestedDummy nestedDummy;
    ...

Nested Class

public class NestedDummy {
    private int intValue;
    private String stringValue;
    ...

Fixture

dummy1:
  integerValue: 123
  stringValue: abc
  nestedDummy:
    intValue: 321
    stringValue: xyz

Object with Set, List or Array

TODO: explain the interface resolver

Class

public class ObjectWithCollection {
    private Collection collection;    //or Set, List, Array...
    ...

Fixture

collection1:
  collection:
    - string1
    - string2
    - string3

Object with Map

Class

public class ObjectWithMap {
    private Map map;
    ...

Fixture

map1:
  map:
    key1: value1
    key2: value2
    key3: value3

Object with Enum

Enum

public enum AnEnum {
    X, Y
}

Class

public class ObjectWithEnum {
    private AnEnum anEnum;
    ...

Fixture

enum1:
  anEnum: X