bot-junit5

Object bot is a library for setting up Java objects as test data.

License

License

Categories

Categories

JUnit Unit Testing
GroupId

GroupId

com.github.dreamhead
ArtifactId

ArtifactId

bot-junit5
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

bot-junit5
Object bot is a library for setting up Java objects as test data.
Project URL

Project URL

https://github.com/dreamhead/object-bot
Source Code Management

Source Code Management

https://github.com/dreamhead/object-bot

Download bot-junit5

Dependencies

compile (2)

Group / Artifact Type Version
com.github.dreamhead : bot-core jar 1.0.0
org.junit.jupiter : junit-jupiter-api jar 5.0.0

runtime (1)

Group / Artifact Type Version
com.google.guava : guava jar 29.0-jre

Project Modules

There are no modules declared in this project.

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.annotation.BotInitializer;

public class FooBotInitializer implements BotInitializer {
    @Override
    public void initializer(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"));
  }
}

Versions

Version
1.0.0