Skip to content

Instantly share code, notes, and snippets.

@ronaimate
Last active February 20, 2026 12:13
Show Gist options
  • Select an option

  • Save ronaimate/69ebabf9237949852ecd7d23e5a604bb to your computer and use it in GitHub Desktop.

Select an option

Save ronaimate/69ebabf9237949852ecd7d23e5a604bb to your computer and use it in GitHub Desktop.
InvocationHandler for fake implementation
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());
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment