Object Bot Java 测试数据管理库开源项目

我要开发同款
匿名用户2020年09月02日
21阅读
开发技术Java
所属分类开发工具、测试工具
授权协议MIT

作品详情

ObjectBot是什么?

ObjectBot是Java的一个管理测试数据的程序库。

为什么要有ObjectBot?

随着测试数量增多,测试测试数据会散落在各个测试里,每个测试都要编写大量的测试数据初始化相关的代码,而且,通常测试数据还要满足一定的依赖关系,这会让测试数据的初始化代码大幅度增加。

ObjectBot就是为了解决这种困难而产生的。

添加依赖

在项目中添加相应的依赖,如果你的项目用的是Maven,可以这样添加:

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

如果是Gradle,可以这样添加:

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

使用方式

假设我们有如下的一个测试数据类Foo:

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

然后,编写一个Initializer初始化数据:

importcom.github.dreamhead.bot.annotation.BotInitializer;publicclassFooBotInitializerimplementsBotInitializer{@Overridepublicvoidinitializer(finalObjectBotbot){//GiveanametoidentifyyourPojo.bot.define("defaultFoo",newFoo("foo","bar"));}}

接下来,就可以在测试中使用初始化好的数据了:

@ExtendWith(BotExtension.class)//AlltestPOJOsareinitializedwithFooBotInitializer.@BotWith(FooBotInitializer.class)publicclassFooTest{//UsethenametoidentifyyourdefinedPojo.//Itwillbeinjectedforeachtest.@Bot("defaultFoo")privateFoofoo;@Testpublicvoidshould_get_foo(){assertThat(foo.getField1(),is("foo"));}}

如果需要在测试类中修改某些字段,为字段赋予新值,可以这么做:

@ExtendWith(BotExtension.class)@BotWith(FooBotInitializer.class)publicclassModifiedFooTest{@Bot(value="defaultFoo")//Customizefieldfield2withvalueblah@StringField(name="field2",value="blah")privateFoofoo;@Testpublicvoidshould_get_foo(){assertThat(foo.getField2(),is("blah"));}}

如果只是在单个测试中修改一个字段,可以使用override:

@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"));}

就是这么简单,用起来吧!

 

查看全文
声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论