From b9966a21816c514cda20c18e735fe10a892410b8 Mon Sep 17 00:00:00 2001 From: Sharang Gupta Date: Wed, 25 Nov 2026 23:48:25 +0000 Subject: [PATCH] Fail exception tests when nothing is thrown getPage, redirectionSameURL and scriptErrorContainsPageUrl caught the expected exception without a fail(), so they also passed when it was never thrown. --- .../java/org/htmlunit/ScriptExceptionTest.java | 9 +++------ src/test/java/org/htmlunit/WebClientTest.java | 9 +++------ .../javascript/JavaScriptEngineTest.java | 18 ++++++------------ 3 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/test/java/org/htmlunit/ScriptExceptionTest.java b/src/test/java/org/htmlunit/ScriptExceptionTest.java index 0587afe755f..76117ca0206 100644 --- a/src/test/java/org/htmlunit/ScriptExceptionTest.java +++ b/src/test/java/org/htmlunit/ScriptExceptionTest.java @@ -14,6 +14,7 @@ */ package org.htmlunit; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.fail; import java.io.PrintWriter; @@ -53,12 +54,8 @@ public void constructor() throws Exception { @Test public void getPage() throws Exception { final String html = DOCTYPE_HTML + ""; - try { - loadPage(html); - } - catch (final ScriptException e) { - assertEquals(URL_FIRST, e.getPage().getUrl()); - } + final ScriptException e = assertThrows(ScriptException.class, () -> loadPage(html)); + assertEquals(URL_FIRST, e.getPage().getUrl()); } /** diff --git a/src/test/java/org/htmlunit/WebClientTest.java b/src/test/java/org/htmlunit/WebClientTest.java index 92ae189b2f4..ba9acdbe775 100644 --- a/src/test/java/org/htmlunit/WebClientTest.java +++ b/src/test/java/org/htmlunit/WebClientTest.java @@ -454,12 +454,9 @@ public void redirectionSameURL() throws Exception { final HtmlPage page1 = getPageWithRedirectionsSameURL(1); assertEquals("Second", page1.getTitleText()); - try { - getPageWithRedirectionsSameURL(30); - } - catch (final Exception e) { - assertTrue(e.getMessage(), e.getMessage().contains("Too many redirects")); - } + final FailingHttpStatusCodeException e = Assertions.assertThrows(FailingHttpStatusCodeException.class, + () -> getPageWithRedirectionsSameURL(30)); + assertTrue(e.getMessage(), e.getMessage().contains("Too many redirects")); } private HtmlPage getPageWithRedirectionsSameURL(final int nbRedirections) throws Exception { diff --git a/src/test/java/org/htmlunit/javascript/JavaScriptEngineTest.java b/src/test/java/org/htmlunit/javascript/JavaScriptEngineTest.java index 1d392369d64..44b89456602 100644 --- a/src/test/java/org/htmlunit/javascript/JavaScriptEngineTest.java +++ b/src/test/java/org/htmlunit/javascript/JavaScriptEngineTest.java @@ -282,12 +282,9 @@ public void scriptErrorContainsPageUrl() throws Exception { + "\n" + ""; - try { - loadPageWithAlerts(content1); - } - catch (final Exception e) { - assertTrue(e.getMessage().contains(URL_FIRST.toString())); - } + final ScriptException e1 = Assertions.assertThrows(ScriptException.class, + () -> loadPageWithAlerts(content1)); + assertTrue(e1.getMessage().contains(URL_FIRST.toString())); // external script final WebClient client = getWebClient(); @@ -305,12 +302,9 @@ public void scriptErrorContainsPageUrl() throws Exception { webConnection.setResponse(urlScript, jsContent, "text/javascript"); client.setWebConnection(webConnection); - try { - client.getPage(URL_FIRST); - } - catch (final Exception e) { - assertTrue(e.getMessage(), e.getMessage().contains(urlScript.toString())); - } + final ScriptException e2 = Assertions.assertThrows(ScriptException.class, + () -> client.getPage(URL_FIRST)); + assertTrue(e2.getMessage(), e2.getMessage().contains(urlScript.toString())); } /**