### Question:
MapBackedCredentialsRepository implements CredentialsRepository<T> { @Override public void save(T creds) { if (!creds.getType().equals(getType())) { throw new InvalidCredentialsTypeException( "Credentials '" + creds.getName() + "' of type '" + creds.getType() + "' cannot be added to repository of type '" + getType() + "'"); } if (eventHandler != null) { if (credentials.containsKey(creds.getName())) { eventHandler.credentialsUpdated(creds); } else { eventHandler.credentialsAdded(creds); } } credentials.put(creds.getName(), creds); } MapBackedCredentialsRepository(
      String type, @Nullable CredentialsLifecycleHandler<T> eventHandler); @Override T getOne(String key); @Override boolean has(String name); @Override Set<T> getAll(); @Override void save(T creds); @Override void delete(String key);  }### Answer:
@Test public void testTypeCheck() { final String TYPE1 = "type1"; final String TYPE2 = "type2"; final String CRED_NAME = "cred"; MapBackedCredentialsRepository<Credentials> repository = new MapBackedCredentialsRepository<>(TYPE1, null); Credentials c1 = mock(Credentials.class); when(c1.getName()).thenReturn(CRED_NAME); when(c1.getType()).thenReturn(TYPE2); Assertions.assertThrows(InvalidCredentialsTypeException.class, () -> repository.save(c1)); }