### Question:
ActiveStandbyElector implements StatCallback, StringCallback { public synchronized void ensureParentZNode() throws IOException, InterruptedException { Preconditions.checkState(!wantToBeInElection, "ensureParentZNode() may not be called while in the election"); String pathParts[] = znodeWorkingDir.split("/"); Preconditions.checkArgument(pathParts.length >= 1 && "".equals(pathParts[0]), "Invalid path: %s", znodeWorkingDir); StringBuilder sb = new StringBuilder(); for (int i = 1; i < pathParts.length; i++) { sb.append("/").append(pathParts[i]); String prefixPath = sb.toString(); LOG.debug("Ensuring existence of " + prefixPath); try { createWithRetries(prefixPath, new byte[]{}, zkAcl, CreateMode.PERSISTENT); } catch (KeeperException e) { if (isNodeExists(e.code())) { continue; } else { throw new IOException("Couldn't create " + prefixPath, e); } } } LOG.info("Successfully created " + znodeWorkingDir + " in ZK."); } ActiveStandbyElector(String zookeeperHostPorts,
      int zookeeperSessionTimeout, String parentZnodeName, List<ACL> acl,
      List<ZKAuthInfo> authInfo,
      ActiveStandbyElectorCallback app); synchronized void joinElection(byte[] data); synchronized boolean parentZNodeExists(); synchronized void ensureParentZNode(); synchronized void clearParentZNode(); synchronized void quitElection(boolean needFence); synchronized byte[] getActiveData(); @Override synchronized void processResult(int rc, String path, Object ctx,
      String name); @Override synchronized void processResult(int rc, String path, Object ctx,
      Stat stat); @Override String toString(); static final Log LOG; }### Answer:
@Test public void testEnsureBaseNodeFails() throws Exception { Mockito.doThrow(new KeeperException.ConnectionLossException()) .when(mockZK).create( Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(), Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT)); try { elector.ensureParentZNode(); Assert.fail("Did not throw!"); } catch (IOException ioe) { if (!(ioe.getCause() instanceof KeeperException.ConnectionLossException)) { throw ioe; } } Mockito.verify(mockZK, Mockito.times(3)).create( Mockito.eq(ZK_PARENT_NAME), Mockito.<byte[]>any(), Mockito.eq(Ids.OPEN_ACL_UNSAFE), Mockito.eq(CreateMode.PERSISTENT)); }