Coder Social home page Coder Social logo

object-bot's Introduction

Object Bot

Object bot is a library for setting up Java objects as test data, which is inspired by Factory Bot.

Latest Release

1.0.0

More details in Release Notes.

Object Bot in Your Build

To add a dependency on Object Bot using Maven, use the following:

<dependency>
  <groupId>com.github.dreamhead</groupId>
  <artifactId>bot-junit5</artifactId>
  <version>1.0.0</version>
</dependency>

To add a dependency using Gradle:

dependencies {
  testImplementation(
    "com.github.dreamhead:bot-junit5:1.0.0",
  )
}

Quick Start

You have a POJO as your test data

class Foo {
  private String field1;
  private String field2;
  
  public Foo(String field1, String field2) {
    this.field1 = field1;
    this.field2 = field2;
  }
  
  public String getField1() {
    return this.field1;
  }
  
  public String getField2() {
    return this.field2;
  }
}

And then you could initialize all your test POJOs in an initializer.

import com.github.dreamhead.bot.BotInitializer;

public class FooBotInitializer implements BotInitializer {
    @Override
    public void initialize(final ObjectBot bot) {
        // Give a name to identify your Pojo.
        bot.define("defaultFoo", new Foo("foo", "bar"));
    }
}

Now you can use it in your test. Refer to the following Junit 5 example.

// Run BotExtension
@ExtendWith(BotExtension.class)
// All test POJOs are initialized with FooBotInitializer. 
@BotWith(FooBotInitializer.class)
public class FooTest {
  // Use the name to identify your defined Pojo.
  // It will be injected for each test.
  @Bot("defaultFoo")
  private Foo foo;
  
  @Test
  public void should_get_foo() {
    assertThat(foo.getField1(), is("foo"));
  }
}

The initialized field can be customized. You can modify a specific field with new value.

@ExtendWith(BotExtension.class)
@BotWith(FooBotInitializer.class)
public class ModifiedFooTest {
  @Bot(value = "defaultFoo")
  // Customize field field2 with value blah 
  @StringField(name = "field2", value="blah")
  private Foo foo;
  
  @Test
  public void should_get_foo() {
    assertThat(foo.getField2(), is("blah"));
  }
}

If the field customization only affects a single test, override API could be used.

@ExtendWith(BotExtension.class)
@BotWith(FooBotInitializer.class)
public class FooTest {
  @Bot("defaultFoo")
  private Foo foo;
  
  @Test
  public void should_get_foo() {
    Foo newFoo = override(foo, field("field2").value("blah"));
    assertThat(newFoo.getField2(), is("blah"));
  }
}

object-bot's People

Contributors

dreamhead 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.