Lightweight fixture support

Lightweight support for fixtures with Spring

License

License

GroupId

GroupId

nl.42
ArtifactId

ArtifactId

fixie
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

Lightweight fixture support
Lightweight support for fixtures with Spring
Source Code Management

Source Code Management

https://github.com/42BV/fixie

Download fixie

How to add to project

<!-- https://jarcasting.com/artifacts/nl.42/fixie/ -->
<dependency>
    <groupId>nl.42</groupId>
    <artifactId>fixie</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/nl.42/fixie/
implementation 'nl.42:fixie:1.1.0'
// https://jarcasting.com/artifacts/nl.42/fixie/
implementation ("nl.42:fixie:1.1.0")
'nl.42:fixie:jar:1.1.0'
<dependency org="nl.42" name="fixie" rev="1.1.0">
  <artifact name="fixie" type="jar" />
</dependency>
@Grapes(
@Grab(group='nl.42', module='fixie', version='1.1.0')
)
libraryDependencies += "nl.42" % "fixie" % "1.1.0"
[nl.42/fixie "1.1.0"]

Dependencies

compile (4)

Group / Artifact Type Version
org.projectlombok : lombok jar
org.springframework : spring-tx jar 5.2.1.RELEASE
org.springframework.boot : spring-boot-starter-aop jar
org.springframework.boot : spring-boot-starter-test jar

test (7)

Group / Artifact Type Version
org.springframework.boot : spring-boot-starter-web jar
org.springframework.boot : spring-boot-starter-data-jpa jar
com.h2database : h2 jar
nl.42 : database-truncator jar 1.0.0
org.junit.jupiter : junit-jupiter-api jar 5.3.2
org.junit.jupiter : junit-jupiter-engine jar 5.3.2
org.junit.jupiter : junit-jupiter-params jar 5.3.2

Project Modules

There are no modules declared in this project.

Fixie

Fixie is a lightweight library for managing test Fixtures.

Fixtures are standardized data sets that represent production data as much as possible. These fixtures are used as input in regression tests to verify that the functionality will work accordingly:

@Fixture
@AllArgsConstructor
public class CountryFixtures {
    
    private final CountryRepository repository;
    
    public Country netherlands() {
        Country country = new Country();
        country.setCode("NL");
        country.setName("Netherlands");
        return repository.save(country);
    }
    
}

Because fixtures object are unique, they should only be created once per test method. Otherwise we might violate database constraints or create strange test results. Fixie manages this uniqueness, allowing the developers to focus on their tests instead.

By annotating the fixture class with @Fixture, Fixie will automatically proxy the bean with an in-memory cache, named Fixtures. This cache is reset before and after each test method invocation, considering the database should also be truncated.

The uniqueness of our fixtures is guaranteed, allowing fixtures to be used multiple times and even use fixtures in other fixtures. This way we reuse code as much as possible and make the construction of complex data structures easier:

@Fixture
@AllArgsConstructor
public class PersonFixtures {
    
    private final PersonRepository repository;
    private final CountryFixtures countries;
    
    public Person jan() {
        Person person = new Person();
        person.setName("Jan");
        person.setEmail("[email protected]");
        person.setCountry(countries.netherlands());
        return repository.save();
    }
    
    public Person dirk() {
        Person person = new Person();
        person.setName("Dirk");
        person.setEmail("[email protected]");
        person.setCountry(countries.netherlands());
        return repository.save();
    }
    
}

Allowing us to write understandable unit tests, without violating database unique constraints:

public class FixtureTest {

  private PersonFixture persons;
  private CountryFixture countries;
  private DatabaseTruncator truncator;

  @BeforeEach
  public void clear() {
    truncator.truncate();
  }

  @Test
  public void unique() {
    Person jan = persons.jan();
    Person dirk = persons.dirk();

    assertEquals("Jan", jan.getName());
    assertEquals("Dirk", dirk.getName());
    
    assertEquals(jan.getCountry(), dirk.getCountry());
    assertEquals(countries.netherlands(), jan.getCountry());
  }
  
}

Usage

Include the dependency in your project:

<dependency>
    <groupId>nl._42</groupId>
    <artifactId>fixie</artifactId>
    <version>1.0.0</version>
</dependency>

This includes a Spring Boot starter, requiring no further configuration.

Plain Spring

Fixie can also be used without Spring Boot.

Register the following aspect:

@Configuration
public class FixtureConfiguration {
    
    @Bean
    public FixtureAspect fixtureAspect() {
      return new FixtureAspect(Fixtures.get());
    }
    
}

Reset the fixtures before each unit test:

public class SomeTest {
    
    @BeforeEach
    public void clear() {
        Fixtures.get().clear();
    }
    
}
nl.42

Versions

Version
1.1.0
1.0.2
1.0.1
1.0.0
0.0.2
0.0.1