Replies: 1 comment 1 reply
-
|
I agree the cause of the unused selector error can be confusing sometimes :) I think in this case it happens because Instancio is not generating If I understood correctly, you want to overwrite the empty collection with a generated one. If so, you can call @Data class Example {
String value;
List<Bar> bars;
}
@Data class Bar {
Integer b;
}
@Test
void example() {
var example = new Example();
example.setBars(new ArrayList<>());
Instancio.fill(example);
System.out.println("1: " + example);
Instancio.ofObject(example)
.generate(all(List.class), Generators::collection)
.fill();
System.out.println("2: " + example);
}should produce Does this work for you? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I have a use case where I would like to let the Instacio fill the object instance via:
Instancio.fill(Foo.class)Where Foo has an empty collection in it.
public class Foo { private List<Bar> bars = new ArrayList<>(); //or lazy getter for the collection //... getters, setters etc... }In my case, I need first to create the Foo instance, store it in DB for integration tests and only then populate it with the Bars.
However, the fill method will ignore the bars collection as it is not null. If I also set the Model for Bar class via:
Instancio.fill(Foo.class).setModel(types().of(Bar.class), createBarModel());I will get the "org.instancio.exception.UnusedSelectorException", which is in this case a bit confusing...
I don't see any setting key to allow this behaviour - the closest one could be the "FillType.POPULATE_NULLS_AND_DEFAULT_PRIMITIVES", but I would need something like: POPULATE_EMPTY_COLLECTIONS.
Setting the Keys.COLLECTION_MIN_SIZE to 1 is also not working for this case.
The only way now is to set the collection to null: foo.setBars(null), but it is not handy if I have multiple of those or the model already has some depth.
Would it be something that could be added? Or am I missing something?
Thanks :-)
Beta Was this translation helpful? Give feedback.
All reactions