Giter VIP home page Giter VIP logo

jbacon's Introduction

JBacon ๐Ÿฅ“

A library for setting up Java objects as test data, inspired in Factory Bot

Instaling

Maven:

<dependency>
    <groupId>br.com.leonardoferreira</groupId>
    <artifactId>JBacon</artifactId>
    <version>2.1.1</version>
    <scope>test</scope>
</dependency>

Gradle:

testImplementation "br.com.leonardoferreira:JBacon:2.1.1"

Usage

It is recommended use with some fake data generator lib like java-faker

Define factory class:

@Component
public class ContactFactory extends JBacon<Contact> {

    @Autowired
    private ContactRepository contactRepository;
    
    @Autowired
    private Faker faker;

    @Override
    protected Contact getDefault() {
        Contact contact = new Contact();
        contact.setName(faker.name().fullName());
        contact.setEmail(faker.internet().emailAddress());
        contact.setPhone(faker.phoneNumber().phoneNumber());
        return contact;
    }

    @Override
    protected Contact getEmpty() {
        return new Contact();
    }

    @Override
    protected void persist(Contact contact) {
        contactRepository.save(contact);
    }
}

Using factories

// Returns a Contact instance that not saved, based on default
Contact build = contactFactory.build();

// Returns a list of Contact instance that not saved, based on default
List<Contact> buildList = contactFactory.build(5);

// Returns a saved Contact instance, based on default
Contact create = contactFactory.create();

// Returns a saved list of Contact instance, based on default
List<Contact> createList = contactFactory.create(5);

Using examples

JBacon allows create object based on example.

// All contacts create/build with this example will have "Leonardo Ferreira" as name, 
// and others attributes will be fill based on getDefault
Contact example = new Contact();
example.setName("Leonardo Ferreira");

Contact build = contactFactory.build(example);

List<Contact> buildList = contactFactory.build(5, example);

Contact create = contactFactory.create(example);

List<Contact> createList = contactFactory.create(5, example);

Other example syntax supported:

Contact build = contactFactory.build(empty -> {
    empty.setName("Leonardo Ferreira");
    empty.setEmail("[email protected]");
});

List<Contact> buildList = contactFactory.build(5, empty -> {
    empty.setName("Leonardo Ferreira");
    empty.setEmail("[email protected]");
});

Contact create = contactFactory.create(empty -> {
    empty.setName("Leonardo Ferreira");
    empty.setEmail("[email protected]");
});

List<Contact> createList = contactFactory.create(5, empty -> {
    empty.setName("Leonardo Ferreira");
    empty.setEmail("[email protected]");
});

Using templates

@Component
public class ContactFactory extends JBacon<Contact> {

    @Autowired
    private ContactRepository contactRepository;

    @Autowired
    private Faker faker;

    @Override
    protected Contact getDefault() {
        Contact contact = new Contact();
        contact.setName(faker.name().fullName());
        contact.setEmail(faker.internet().emailAddress());
        contact.setPhone(faker.phoneNumber().phoneNumber());
        return contact;
    }

    @JBaconTemplate("invalid")
    protected Contact invalidContact() {
        Contact contact = new Contact();
        contact.setName(faker.name().fullName());
        contact.setEmail("invalid_email");
        contact.setPhone(faker.phoneNumber().phoneNumber());
        return contact;
    }

    @Override
    protected Contact getEmpty() {
        return new Contact();
    }

    @Override
    protected void persist(Contact contact) {
        contactRepository.save(contact);
    }
}
// Returns a Contact instance that not saved, based on invalid template
Contact build = contactFactory.build("invalid");

// Returns a list of Contact instance that not saved, based on invalid template
List<Contact> buildList = contactFactory.build(5, "invalid");

// Returns a saved Contact instance, based on invalid template
Contact create = contactFactory.create("invalid");

// Returns a saved list of Contact instance, based on invalid template
List<Contact> createList = contactFactory.create(5, "invalid");

Lazy Loader

When getDefault depends on creating another object, you can use:

public class PhoneFactory extends JBacon<Phone> {

    @Autowired
    private Faker faker;

    @Autowired
    private ContactFactory contactFactory;

    @Autowired
    private PhoneRepository phoneRepository;

    @Override
    protected Phone getDefault() {
        Phone phone = new Phone();
        phone.setNumber(faker.phoneNumber().phoneNumber());
        phone.setContact(Lazy.of(Contact.class).with(contactFactory::create));
        return phone;
    }

    @Override
    protected Phone getEmpty() {
        return new Phone();
    }

    @Override
    protected void persist(final Phone phone) {
        phoneRepository.save(phone);
    }

}

Using this way, it's avoids save unnecessary objects, when uses example. Ex:

Contact contact = contactFacotory.create();
List<Phone> phones = phoneFactory.create(5, empty -> {
    empty.setContact(contact);
});

Without Lazy this will persist 6 contacts. When uses lazy it will persist only one contact.

ChangeLog

2.0.1 - 2019-03-03

  • Better project description :)

2.0.0 - 2019-03-03

1.2.1 - 2018-10-21

1.2.0 - 2018-10-14

1.1.2 - 2018-05-23

  • Using lombok only in compile

1.1.0 - 2018-03-11

1.0.1 - 2018-02-13

1.0.0 - 2018-02-04

  • JBacon first release ๐Ÿป
  • Create operation
  • CreateList operation
  • Build operation

jbacon's People

Contributors

leonardoferreiraa avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.