-
Hello! Could you please suggest how can I get an instance of some random subtype among specified ones for a given super type? Something like this: interface Vehicle {}
class Car implements Vehicle {}
class Bicycle implements Vehicle {}
class Boat implements Vehicle {}
// doesn't work :c
final var carOrBicycleOrBoat = Instancio.of(Vehicle.class)
.subtype(all(Vehicle.class), Car.class)
.subtype(all(Vehicle.class), Bicycle.class)
.subtype(all(Vehicle.class), Boat.class)
.create();
final var listOfRandomVehicles = ???; |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@bash-spbu Instancio selects random subtypes only for https://www.instancio.org/user-guide/#subtype-mapping Since you're not using static Supplier<Vehicle> vehicleSupplier() {
return () -> {
Class<?> subtype = Instancio.gen()
.oneOf(Car.class, Bicycle.class, Boat.class)
.get();
return Instancio.of(Vehicle.class)
.subtype(all(Vehicle.class), subtype)
.create();
};
}
List<Vehicle> vehicles = Stream.generate(vehicleSupplier())
.limit(100)
.collect(Collectors.toList());
assertThat(vehicles).hasOnlyElementsOfTypes(Car.class, Bicycle.class, Boat.class);
For this, you can implement the SPI. Please check out the |
Beta Was this translation helpful? Give feedback.
@bash-spbu Instancio selects random subtypes only for
sealed
abstract types. You can find more info here:https://www.instancio.org/user-guide/#subtype-mapping
Since you're not using
sealed
types, you can achieve something similar by creating a helper method: