CdiBootstrap { public BeanManager getBeanManager() { if (null == beanManager) { try { beanManager = (BeanManager) getInitialContext().lookup(Constants.BeanManager.BEANMANAGER_ALT); } catch (NamingException e) { } } return beanManager; }  BeanManager getBeanManager(); T getBean(Class<T> cls);  }
@Test public void testGetBeanManager() throws NamingException { System.out.println("getBeanManager"); when(cdiBootstrap.getInitialContext()).thenThrow(NamingException.class); BeanManager result = cdiBootstrap.getBeanManager(); assertThat(result).isNull(); cdiBootstrap = spy(CdiBootstrapImpl.class); BeanManager bm = mock(BeanManager.class); InitialContext ic = mock(InitialContext.class); when(ic.lookup(eq(BEANMANAGER))).thenReturn(bm); when(cdiBootstrap.getInitialContext()).thenReturn(ic); result = cdiBootstrap.getBeanManager(); assertThat(result).isEqualTo(bm); when(cdiBootstrap.getInitialContext()).thenThrow(NamingException.class); result = cdiBootstrap.getBeanManager(); assertThat(result).isEqualTo(bm); }