Skip to content

Repository files navigation

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

classFoo {
privateStringfield1;
privateStringfield2;
publicFoo(Stringfield1, Stringfield2) {
this.field1 = field1;
this.field2 = field2;
}
publicStringgetField1() {
returnthis.field1;
}
publicStringgetField2() {
returnthis.field2;
}
}

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

importcom.github.dreamhead.bot.BotInitializer;
publicclassFooBotInitializerimplementsBotInitializer {
@Overridepublicvoidinitialize(finalObjectBotbot) {
// Give a name to identify your Pojo.bot.define("defaultFoo", newFoo("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)
publicclassFooTest {
// Use the name to identify your defined Pojo.// It will be injected for each test.@Bot("defaultFoo")
privateFoofoo;
@Testpublicvoidshould_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)
publicclassModifiedFooTest {
@Bot(value = "defaultFoo")
// Customize field field2 with value blah @StringField(name = "field2", value="blah")
privateFoofoo;
@Testpublicvoidshould_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)
publicclassFooTest {
@Bot("defaultFoo")
privateFoofoo;
@Testpublicvoidshould_get_foo() {
FoonewFoo = override(foo, field("field2").value("blah"));
assertThat(newFoo.getField2(), is("blah"));
}
}

About

A library for setting up Java objects as test data

Resources

Stars

39 stars

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages