Source:
public void testInvalidShinyApp() throws IOException, InterpreterException, InterruptedException, UnirestException { InterpreterContext context = getInterpreterContext(); context.getLocalProperties().put("type", "ui"); InterpreterResult result = interpreter.interpret(IOUtils.toString(getClass().getResource("/invalid_ui.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); context = getInterpreterContext(); context.getLocalProperties().put("type", "server"); result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); final InterpreterContext context2 = getInterpreterContext(); context2.getLocalProperties().put("type", "run"); Thread thread = new Thread(() -> { try { interpreter.interpret("", context2); } catch (Exception e) { e.printStackTrace(); } }); thread.start(); Thread.sleep(5 * 1000); List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage(); assertEquals(1, resultMessages.size()); assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType()); String resultMessageData = resultMessages.get(0).getData(); assertTrue(resultMessageData, resultMessageData.contains("<iframe")); Pattern urlPattern = Pattern.compile(".*src=\"(http\\S*)\".*", Pattern.DOTALL); Matcher matcher = urlPattern.matcher(resultMessageData); if (!matcher.matches()) { fail("Unable to extract url: " + resultMessageData); } String shinyURL = matcher.group(1); HttpResponse<String> response = Unirest.get(shinyURL).asString(); assertEquals(500, response.getStatus()); resultMessages = context2.out.toInterpreterResultMessage(); assertTrue(resultMessages.get(1).getData(), resultMessages.get(1).getData().contains("'Invalid_code'")); interpreter.cancel(getInterpreterContext()); Thread.sleep(1000); try { Unirest.get(shinyURL).asString(); fail("Should fail to connect to shiny app"); } catch (Exception e) { assertTrue(e.getMessage(), e.getMessage().contains("Connection refused")); } }

Target:
public void testInvalidShinyApp() throws IOException, InterpreterException, InterruptedException, UnirestException { InterpreterContext context = getInterpreterContext(); context.getLocalProperties().put("type", "ui"); InterpreterResult result = interpreter.interpret(IOUtils.toString(getClass().getResource("/invalid_ui.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); context = getInterpreterContext(); context.getLocalProperties().put("type", "server"); result = interpreter.interpret(IOUtils.toString(getClass().getResource("/server.R")), context); assertEquals(InterpreterResult.Code.SUCCESS, result.code()); final InterpreterContext context2 = getInterpreterContext(); context2.getLocalProperties().put("type", "run"); Thread thread = new Thread(() -> { try { interpreter.interpret("", context2); } catch (Exception e) { e.printStackTrace(); } }); thread.start(); Thread.sleep(5 * 1000); List<InterpreterResultMessage> resultMessages = context2.out.toInterpreterResultMessage(); assertEquals(1, resultMessages.size()); assertEquals(InterpreterResult.Type.HTML, resultMessages.get(0).getType()); String resultMessageData = resultMessages.get(0).getData(); assertTrue(resultMessageData, resultMessageData.contains("<iframe")); Pattern urlPattern = Pattern.compile(".*src=\"(http\\S*)\".*", Pattern.DOTALL); Matcher matcher = urlPattern.matcher(resultMessageData); if (!matcher.matches()) { fail("Unable to extract url: " + resultMessageData); } String shinyURL = matcher.group(1); HttpResponse<String> response = Unirest.get(shinyURL).asString(); assertEquals(500, response.getStatus()); resultMessages = context2.out.toInterpreterResultMessage(); assertTrue(resultMessages.get(1).getData(), resultMessages.get(1).getData().contains("object 'Invalid_code' not found")); interpreter.cancel(getInterpreterContext()); Thread.sleep(1000); try { Unirest.get(shinyURL).asString(); fail("Should fail to connect to shiny app"); } catch (Exception e) { assertTrue(e.getMessage(), e.getMessage().contains("Connection refused")); } }