### Question:
Parser { String parseLoginForm(final String body) { final Document document = Jsoup.parse(body); final Element form = document.selectFirst("#js_sessions_new_form form"); if (form == null) { throw new RuntimeException("Login form element not found."); } return parseAuthenticityToken(form); }    }### Answer:
@Test public void parseLoginFormTest() { final String token = parser.parseLoginForm( "<html><body><div id=\"js_sessions_new_form\"><form>" + "<input name=\"authenticity_token\" value=\"TOKEN\"></form></div></body></html>"); assertEquals("TOKEN", token); }
@Test public void parseLoginFormTest_noFormElement() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("Login form element not found."); parser.parseLoginForm("<html><body></body></html>"); }
@Test public void parseLoginFormTest_noAuthenticityTokenElement() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("Authenticity token element not found."); parser.parseLoginForm( "<html><body><div id=\"js_sessions_new_form\"><form></form></div></body></html>"); }
@Test public void parseLoginFormTest_noAuthenticityTokenValue() { expectedException.expect(RuntimeException.class); expectedException.expectMessage("`authenticity_token` is not found."); parser.parseLoginForm( "<html><body><div id=\"js_sessions_new_form\"><form>" + "<input name=\"authenticity_token\" value=\"\"></form></div></body></html>"); }