---
title: How to Use Fixture Factory on Unit and Component Tests in Spring Boot
description: Fixture factory can expedite your testing while keeping quality control.
image: https://blog.avenuecode.com/hubfs/square-gd413f4ba6_1920.jpg
---

[AvenueCode.com](https://avenuecode.com) [News](https://avenuecode.com/news) [Contact](https://avenuecode.com/contact)

[![Avenue Code Snippets Logo](https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/AC-Snippets---Black.png)](https://blog.avenuecode.com/?hsLang=en-us) *menu*

- Technology
  
  [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us) [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us) [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us) [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us) [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
  
  [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us) [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us) [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us) [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

- [Cloud](https://blog.avenuecode.com/blog/topic/cloud?hsLang=en-us)
- [Design](https://blog.avenuecode.com/blog/topic/design?hsLang=en-us)
- [Delivery Infrastructure](https://blog.avenuecode.com/blog/topic/delivery-infrastructure?hsLang=en-us)
- [Solution Architecture](https://blog.avenuecode.com/blog/topic/solution-architecture?hsLang=en-us)
- [Web Experience](https://blog.avenuecode.com/blog/topic/web-experience?hsLang=en-us)
- [Data & ML](https://blog.avenuecode.com/blog/topic/data-and-machine-learning?hsLang=en-us)
- [Agile Mindset](https://blog.avenuecode.com/blog/topic/agile-mindset?hsLang=en-us)
- [Mobile Experience](https://blog.avenuecode.com/blog/topic/mobile-experience?hsLang=en-us)
- [Quality First](https://blog.avenuecode.com/blog/topic/quality-first?hsLang=en-us)
- [Whitepapers](https://blog.avenuecode.com/blog/topic/whitepapers?hsLang=en-us)
- [Spotlight](https://blog.avenuecode.com/blog/topic/spotlight?hsLang=en-us)
- [Extraordinary Women in Tech](https://blog.avenuecode.com/blog/topic/extraordinary-women-in-tech?hsLang=en-us)
- [Avenue Code Culture](https://blog.avenuecode.com/blog/topic/avenue-code-culture?hsLang=en-us)

# [How to Use Fixture Factory on Unit and Component Tests in Spring Boot](https://blog.avenuecode.com/how-to-use-fixture-factory-on-unit-and-component-tests-in-spring-boot)

- [Tweet](https://twitter.com/share)

*perm\_identity* [Matheus Sousa](https://blog.avenuecode.com/how-to-use-fixture-factory-on-unit-and-component-tests-in-spring-boot/author/matheus-sousa?hsLang=en-us)

*schedule* 11/23/22 2:00 PM

Want to expedite tests and avoid creating mock objects for every single scenario? Here's how Fixture Factory can help.

##### Introduction

Running tests at every stage of software development is becoming increasingly critical. Because of this, many test frameworks are emerging to expedite the full cycle of development, from conception to software delivery.  

Setting up scenarios for testing in the development environment, however, can be a highly stressful task. In some situations, developers need to create an enormous amount of test data to cover each scenario. Furthermore, most of the test codes are related to setting up these objects.

Fixture Factory is a Java test framework that aims to **reduce** the need to create multiple mock objects used in testing by reusing objects in various development testing scenarios.

This article describes how we can use the Fixture Factory framework on unit and component tests for a Spring Boot project.

##### What Is Fixture Factory Framework?

Fixture Factory is a tool that helps us create mock objects for unit tests in Java. This framework also helps us reuse these objects, creating new fake items based on existing items. 

The base of this tool is the object template composition, where you set several rules from a template class. The syntax of a rule is: 

```
Fixture.of(ClassThatYouWantToMock.class).addTemplate(<Rule ID>, new Rule());
```

Where:  **Rule ID** is a string value that serves as a reference when the template is used, and the **ClassThatYouWantToMock** is the class that will get its mock attributes from the rule. 

Fixture Factory allows the **reuse** of templates by the composition and inheritance of templates, just like those used in object-oriented concepts.

For example, consider a composite class called "Subscription" that maps a database table featuring Student and Course attributes. Considering that Student and Course template rules have been created with the rule identification "VALIDSTUDENTRULEID" and "VALIDCOURSERULEID" respectively, a valid template rule for Subscription with the Student and Course rules would be:

```
    public void load() {        Fixture.of(Subscription.class).addTemplate(<RuleID>, new Rule() );    }
```

Using the same example, imagine that we need to create a Subscription rule almost equal to the one created above except that one attribute needs to be different from the rest of the previous template. The new template rule, with inheretance from a previous template, should be:

```
    public void load() {        Fixture.of(Subscription.class).addTemplate(<ParentRuleID>, new Rule() );        Fixture.of(Subscription.class).addTemplate(<RuleID>)                .inherits(<ParentRuleID>, new Rule());    }
```

##### Creating a Fixture Factory on a Spring Boot Project

Before we can implement the templates in the test scenarios, we need to install the Fixture Factory dependency on the Spring Boot project.

Installation on a Spring Boot project using Maven looks like this:

```
  <dependency>       <groupId>br.com.six2six</groupId>       <artifactId>fixture-factory</artifactId>       <version>3.1.0</version>   </dependency>
```

And a Spring Boot project using Gradle looks like this:

```
  testImplementation group: 'br.com.six2six', name: 'fixture-factory', version: '3.1.0'
```

You can also create a test source package and create all template classes on this package.

##### Applying Fixture Factory in a Unit Test

Once the Fixture Factory dependency is installed in the Spring Boot project, it's time to turn our attention to the template's test source package. You can create variables in test scenarios based on the template rule defined earlier. The first step to achieving this is to add an instruction to load these templates into the test class.

```
    @BeforeEach    public void setup(){        FixtureFactoryLoader.loadTemplates("<template source package>");    }
```

This statement allows loading the templates by their package definition. 

Consider the subscription example. You can set a subscription variable on a test scenario with a template rule like this:

```
Subscription mockSubscriptionObject = Fixture.from(Subscription.class).gimme(<RuleID>);
```

Fixture Factory provides the **customization** of actions to be completed when a template rule is generated. For example, in the Subscription scenario, let's say the test unit needs to evaluate a custom query from the database. For this unit test to work as expected, the rules generated must be persisted on the test environment database. The Fixture Factory framework provides a class called "Processor" for these scenarios. In our Subscription illustration, the Processor class should be:

```
@Component@Slf4j@Transactionalpublic class EntityManagerProcessor implements Processor {    private final EntityManager entityManager;    public EntityManagerProcessor (final EntityManager entityManager){        this.entityManager = entityManager;    }    @Override    public void execute(final Object result) {        if(result.getClass().isAnnotationPresent(Entity.class)){            try{                persisteCompositeClass(result);                findOrPersist(result);            }catch (IllegalAccessException illegalAccessException){                log.error("Error to access the fields ",illegalAccessException);            }        }    }   private Object findOrPersist(Object result){...}   private void persisteCompositeClass(Object result){...}}
```

The usage of the Processor on the unit test scenario doesn't much change the structure of a template call:

```
@Autowiredprivate EntityManagerProcessor entityManagerProcessor;...Subscription mockSubscriptionObjectPersisted =  Fixture.from(Subscription.class).uses(entityManagerProcessor).gimme(<RuleID>);
```

##### Conclusion 

Fixture Factory contains functionalities that improve the organization of test code and mock object reuse on test scenarios. This framework also provides the customization of actions of generated objects, which allows the test developer to adapt the behavior of generated objects to the test project requirement.

Using Fixture Factory in a Spring Boot project is easy, and Spring Boot frameworks provide mechanisms for unit tests, integrated tests, and a mixture of both types of tests. You can use Fixture Factory with all kinds of tests in a Spring Boot project, including using the same template rule for both types of tests.

One thing to be aware of is template rule reuse: once a parent or component rule changes, the child or compound rule also changes and will likely break the test methods involved.

For more information about Fixture Factory and how to integrate it into a Spring Boot project, visit the Fixture Factory project and the Fixture Factory Spring Boot sample code project on GitHub via the links below.

 

##### References

[fixture factory](https://github.com/six2six/fixture-factory), six2six. GitHub. 

[fixture factory SB sample code](https://github.com/masousa/fixture-factory-sb-sample-code), masousa. GitHub.

---

### Author

# Matheus Sousa

 Matheus Sousa is a Senior Software Engineer at Avenue Code. He holds a master's degree in Computer Software Engineering. He has more than 11 years of experience in software development using Java and its frameworks, and he is passionate about software development.

---

### Related Posts

### \[JAVA 21\] Structured Concurrency: Powering Data Orchestration with Virtual Threads and Scopes

[READ MORE](https://blog.avenuecode.com/java-21-structured-concurrency-powering-data-orchestration-with-virtual-threads-and-scopes?hsLang=en-us)

### How to Use Circuit Breaker Resilience in Your API Integration

[READ MORE](https://blog.avenuecode.com/how-to-use-circuit-breaker-resilience-in-your-api-integration?hsLang=en-us)

### How to Create Your Own RAR Extractor Using Electron

[READ MORE](https://blog.avenuecode.com/how-to-create-your-own-rar-extractor-using-electron?hsLang=en-us)

### Leave a Comment!

### Avenue Code Social

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Blog/facebook.png?t=1486470796564)](https://www.facebook.com/avenuecode)

[![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Blog/twitter.png?t=1486470796842)](https://twitter.com/AvenueCode)

[![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Blog/linkedin.png?t=1486470796556)](https://www.linkedin.com/company/avenuecode/)

### Newsletter

Want to stay on top of all tips and news from Avenue Code?

### Popular Snippets

![Avenue Code-primary versions_logo white avenue code endorsement 2](https://blog.avenuecode.com/hs-fs/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png?width=1180&name=Avenue%20Code-primary%20versions_logo%20white%20avenue%20code%20endorsement%202.png "Avenue Code-primary versions_logo white avenue code endorsement 2")

### About Us

- [Who We Are](https://www.avenuecode.com/who-we-are)
- [What We Do](https://www.avenuecode.com/what-we-do)
- [Portfolio](https://www.avenuecode.com/portfolio)
- [Partners](https://www.avenuecode.com/partners)
- [News](https://www.avenuecode.com/news)
- [Events](https://www.avenuecode.com/events)
- [Blog](https://blog.avenuecode.com/)
- [Contact](https://www.avenuecode.com/contact)

### Our Offices

San Francisco

[+1 415 766 4178](tel:+553125161448) [ac.inquiries@avenuecode.com](mailto:brazil.info@avenuecode.com)

Belo Horizonte

[+55 31 2516 1448](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

São Paulo

[+55 11 3205 3232](tel:+553125161448) [brazil.info@avenuecode.com](mailto:brazil.info@avenuecode.com)

### We're Hiring!

- [Belo Horizonte](https://www.avenuecode.com/who-we-are)
- [New York](https://www.avenuecode.com/what-we-do)
- [San Francisco](https://www.avenuecode.com/portfolio)
- [São Paulo](https://www.avenuecode.com/partners)

---

©2015 - 2017 Avenue Code

[![Facebook Icon](https://blog.avenuecode.com/hubfs/Images/Icons/facebook-2.png)](https://www.facebook.com/avenuecode) [![Twitter Icon](https://blog.avenuecode.com/hubfs/Images/Icons/twitter-2.png)](https://twitter.com/AvenueCode) [![LinkedIn Icon](https://blog.avenuecode.com/hubfs/Images/Icons/linkedin-2.png)](https://www.linkedin.com/company/avenue-code) [![Glassdoor Icon](https://blog.avenuecode.com/hubfs/Images/Icons/glassdoor-icon-1.png)](https://www.glassdoor.com/Overview/Working-at-Avenue-Code-EI_IE456173.11,22.htm) [![YouTube Icon](https://blog.avenuecode.com/hubfs/Images/Icons/youtube-2.png)](https://www.youtube.com/user/AvenueCodePlay)

Please enable JavaScript to view the [comments powered by Disqus.](http://disqus.com/?ref_noscript)

© 2026 Avenue Code

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Matheus Sousa",
    "url" : "https://blog.avenuecode.com/author/matheus-sousa"
  },
  "dateModified" : "2022-11-23T17:00:00.434Z",
  "datePublished" : "2022-11-23T17:00:00.000Z",
  "headline" : "How to Use Fixture Factory on Unit and Component Tests in Spring Boot",
  "image" : [ "https://blog.avenuecode.com/hubfs/square-gd413f4ba6_1920.jpg" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.avenuecode.com/how-to-use-fixture-factory-on-unit-and-component-tests-in-spring-boot",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.avenuecode.com/hubfs/Avenue%20Code%20New%20Logos%20-%202023/Avenue%20Code-primary%20versions_LOGO%20HORIZONTAL%20group%201-7.png"
    },
    "name" : "Avenue Code"
  }
}
```