Java-builder
A library for building objects in test.
Getting Started
Given a bean class:
@Getter
@Setter
public class Bean {
private String strValue;
private int intValue;
};
Create FactorySet
FactorySet factorySet = new FactorySet();
Register and build
// Register class
factorySet.onBuild(Bean.class, bean -> {
bean.setStrValue("hello world");
});
// Build one object
Bean bean = factorySet.type(Bean.class).build();
// Output is
// hello world
println(bean.getStrValue());
Register
- With build context
factorySet.onBuild(Bean.class, (bean, buildContext) -> {
bean.setStrValue("hello " + buildContext.getSequence());
});
Builder<Bean> builder = factorySet.type(Bean.class);
// Output is:
// hello 1
// hello 2
println(builder.build().getStrValue());
println(builder.build().getStrValue());
- with no default constructor
factorySet.register(Bean.class, (sequence) -> {
Bean bean = new Bean();
bean.setStrValue("hello " + sequence);
return bean;
});
Builder<Bean> builder = factorySet.type(Bean.class);
// Output is:
// hello 1
println(builder.build().getStrValue());
Build with property
factorySet.onBuild(Bean.class, (bean) -> {
});
// Output is:
// hello world
println(factorySet.type(Bean.class).properties(new HashMap<String, Object>{{
put("strValue", "hello world");
}}).build().getStrValue());
- guess and convert to right type
factorySet.onBuild(Bean.class, (bean) -> {
});
// Output is:
// 100
println(factorySet.type(Bean.class).properties(new HashMap<String, Object>{{
put("intValue", "100");
}}).build().getIntValue());
- register customer converter
factorySet.onBuild(Bean.class, (bean) -> {
});
factorySet.registerConverter(converter ->
converter.addTypeConverter(Long.class, int.class, Long::intValue));
// Output is:
// 100
println(factorySet.type(Bean.class).properties(new HashMap<String, Object>{{
put("intValue", 100L);
}}).build().getIntValue());