Last active
February 20, 2026 12:13
-
-
Save ronaimate/69ebabf9237949852ecd7d23e5a604bb to your computer and use it in GitHub Desktop.
InvocationHandler for fake implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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