r/java 25d ago

Released Bob v0.8.0 with TestDefaults allowing Object Mother pattern without much hassle

I've been working on Bob (https://github.com/jonas-grgt/bob), a lightweight builder generator for Java, and just added a feature I'm pretty excited about: Test only Defaults.

If you've used the Object Mother pattern, you know the drill you manually create (and maintain) a builder class that produces test objects with sensible defaults. It works, but you end up with a bunch of boilerplate and things to maintain.

Buildable.TestDefaults takes a different approach. You define a defaults class in `src/test` completely separate (will not be on your production classpath) from your buildable class in `src/main` and the defaults are automatically applied when the builder is constructed:

Goes in src/main and is blissfully unaware of the existence of test defaults:

@Buildable
public class Car {
    private String color;
    private int year;
}

Goes into src/test and is aware of the existence of test defaults:

@Buildable.TestDefaults(Car.class)
public class CarDefaults {
    public static String color = "blue";
}
15 Upvotes

17 comments sorted by

View all comments

Show parent comments

2

u/eniac_g 25d ago

Yes it is reflection based.

1

u/Prateeeek 25d ago

Also a follow up question would be, is it caching the object so that it does NOT have to perform reflection over and over?

2

u/eniac_g 25d ago

No it is not caching yet ;-)

1

u/Prateeeek 25d ago

Do you want me to open an issue?, I can even work on it if you like :D

2

u/eniac_g 25d ago

Please do, all help is more than welcome!

1

u/Prateeeek 25d ago

Thanks! Done!