### Question:
Daemon { @SuppressWarnings("squid:S1217") public static Thread runInNewDaemonThread(final Runnable runnable) { if (!isDaemonThread()) { throw new APIAuthenticationException("Only daemon threads can spawn new daemon threads"); } DaemonThread thread = new DaemonThread() { @Override public void run() { isDaemonThread.set(true); try { Context.openSession(); runnable.run(); } finally { Context.closeSession(); } } }; thread.start(); return thread; }  static Module startModule(Module module); static Module startModule(final Module module, final boolean isOpenmrsStartup,
	        final AbstractRefreshableApplicationContext applicationContext); static void executeScheduledTask(final Task task); @SuppressWarnings("squid:S1217") static Thread runInNewDaemonThread(final Runnable runnable); static boolean isDaemonThread(); static void runStartupForService(final OpenmrsService service); @SuppressWarnings("squid:S1217") static Thread runInDaemonThread(final Runnable runnable, DaemonToken token); static void runInDaemonThreadAndWait(final Runnable runnable, DaemonToken token); static boolean isDaemonUser(User user); static User getDaemonThreadUser();  }### Answer:
@Test public void runInNewDaemonThread_shouldThrowErrorIfCalledFromANonDaemonThread() { try { Daemon.runInNewDaemonThread(() -> { }); Assert.assertTrue("Should not hit this line, since the previous needed to throw an exception", false); } catch (APIAuthenticationException ex) { Assert.assertEquals("Only daemon threads can spawn new daemon threads", ex.getMessage()); } }
@Test public void runInNewDaemonThread_shouldThrowErrorIfCalledFromANonDaemonThread() { try { Daemon.runInNewDaemonThread(new Runnable() { @Override public void run() { } }); Assert.assertTrue("Should not hit this line, since the previous needed to throw an exception", false); } catch (APIAuthenticationException ex) { Assert.assertEquals("Only daemon threads can spawn new daemon threads", ex.getMessage()); } }