Invocation handler
public static CustomerRepository fakeCustomerRepository() {
Customer alice = new Customer(1L, "Alice");
Customer bob = new Customer(2L, "Bob");
List<Customer> all = List.of(alice, bob);
return (CustomerRepository) Proxy.newProxyInstance(
CustomerRepository.class.getClassLoader(),
new Class[]{CustomerRepository.class},
(proxy, method, args) -> switch (method.getName()) {
case "findAll" -> all;
case "count" -> (long) all.size();
case "save" -> args[0]; // visszaadja amit kapott
case "deleteById" -> {
System.out.println("deleteById called with: " + args[0]);
yield null;
}
case "findById" -> {
Long id = (Long) args[0];
yield all.stream()
.filter(c -> c.getId().equals(id))
.findFirst();
}
case "findByName" -> {
String name = (String) args[0];
yield all.stream()
.filter(c -> c.getName().equals(name))
.toList();
}
default -> throw new UnsupportedOperationException("Not faked: " + method.getName());
}
);
}