-
How I Create Test Objects in Java
2024-01-16 "The Problem
When I write tests in Java, I find it tedious to manually create objects and setting attributes. Code easily turns out like this:
var foo = new Foo("Satellite", 1000, true); var bar = new Bar("Edward", "Scissorhands", List.of(foo)); // do stuff assertEquals(foo, bar.findByName("Satellite"));
Here we're first creating a
"Foo
, which is then added to aBar
. We then test that we can find theFoo
when we ask theBar
to search for it.