NamingContext implements Context, Serializable { @Override public void unbind(Name toUnbind) throws NamingException { Name name = this.stripEmptyPrefix(toUnbind); if (name.isEmpty()) throw new NamingException("Invalid name: " + name.toString()); NamingEntry entry = bindings.get(name.get(0)); if (entry == null) throw new NameNotFoundException("Name not bound: " + name.get(0)); if (name.size() > 1) { if (entry.getType().equals(NamingEntryType.CONTEXT)) { ((Context) entry.getObject()).unbind(name.getSuffix(1)); } else { throw new NamingException("Context expected: " + name.toString()); } } else { bindings.remove(name.get(0)); } } NamingContext();  NamingContext(Hashtable<String, Object> environment);  NamingContext(Hashtable<String, Object> environment, Map<String, Object> objects);  NamingContext(Hashtable<String, Object> environment, String nameInNamespace); @Override void bind(Name name, Object object); void bind(Name bindName, Object object, boolean rebind); @Override Name composeName(Name name, Name prefix); @Override String composeName(String name, String prefix); @Override Context createSubcontext(Name name); @Override void destroySubcontext(Name contextToDestroy); @Override String getNameInNamespace(); @Override NameParser getNameParser(Name name); @Override NameParser getNameParser(String name); @Override NamingEnumeration list(Name name); @Override NamingEnumeration list(String name); @Override NamingEnumeration<Binding> listBindings(Name name); @Override NamingEnumeration<Binding> listBindings(String name); @Override Object lookup(Name lookupName); @Override Object lookupLink(Name name); @Override void rebind(Name name, Object object); @Override void rename(Name oldName, Name newName); @Override void unbind(Name toUnbind); @Override Object lookup(String name); @Override Object lookupLink(String name); @Override void unbind(String name); @Override void rebind(String name, Object object); @Override Object removeFromEnvironment(String propertyName); @Override Object addToEnvironment(String propertyName, Object propertyVal); @Override Hashtable<?, ?> getEnvironment(); @Override void bind(String name, Object object); @Override void close(); @Override Context createSubcontext(String name); @Override void destroySubcontext(String name); @Override void rename(String oldName, String newName);  }
@Test public void testUnbind() throws Exception { try (CloseableNamingContext context = closeableContext()) { context.bind("hello", new Object()); context.unbind("hello"); try { context.lookup("hello"); fail(); } catch (NamingException expected) { } try { context.unbind("hello"); fail(); } catch (NameNotFoundException expected) { } try { context.unbind(new CompositeName()); fail(); } catch (NamingException expected) { } } }