From 56153486c9f05419fda28191f6a08ef58ad5e905 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B2=D0=B3=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=A2=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D1=85=D0=BE=D0=B2?= Date: Wed, 31 Jul 2024 17:56:18 +0400 Subject: [PATCH] fix(test): running a test without Arrange section outputs informative fail reason Closes: #53 --- .../java/ru/ewc/checklogic/StateFactory.java | 5 +++ .../ru/ewc/checklogic/server/WebPages.java | 34 +++++++++++-------- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/main/java/ru/ewc/checklogic/StateFactory.java b/src/main/java/ru/ewc/checklogic/StateFactory.java index 595ca43..adb0754 100644 --- a/src/main/java/ru/ewc/checklogic/StateFactory.java +++ b/src/main/java/ru/ewc/checklogic/StateFactory.java @@ -95,6 +95,11 @@ private Map locatorsFromFile() { if (this.src != null) { final Map> raw = (Map>) new Yaml().loadAll(this.src).iterator().next(); + if (raw == null) { + throw new IllegalStateException( + "There is no Arrange section in the test file, you should add one" + ); + } raw.forEach((name, data) -> locators.put(name, new InMemoryStorage(data))); } return locators; diff --git a/src/main/java/ru/ewc/checklogic/server/WebPages.java b/src/main/java/ru/ewc/checklogic/server/WebPages.java index 41f8816..9380937 100644 --- a/src/main/java/ru/ewc/checklogic/server/WebPages.java +++ b/src/main/java/ru/ewc/checklogic/server/WebPages.java @@ -101,24 +101,30 @@ private String templateNamed(final String template, final Map va @SneakyThrows private TestResult performTest(final TestData test) { final SoftAssertions softly = new SoftAssertions(); - final FullServerContext target = new ServerContextFactory(this.root) - .fromStateFile(Files.newInputStream(new File(test.file()).toPath())); TestResult result; + final FullServerContext target; try { - if (!test.command().isEmpty()) { - target.perform(test.command()); + target = new ServerContextFactory(this.root) + .fromStateFile(Files.newInputStream(new File(test.file()).toPath())); + try { + if (!test.command().isEmpty()) { + target.perform(test.command()); + } + for (final String locator : test.expectations().keySet()) { + softly + .assertThat(target.stateFor(locator, test.expectations().get(locator))) + .describedAs(String.format("State for entity '%s'", locator)) + .containsExactlyInAnyOrderEntriesOf(test.expectations().get(locator)); + } + softly.assertAll(); + result = new TestResult(test.toString(), true, ""); + } catch (final AssertionError error) { + result = new TestResult(test.toString(), false, error.getMessage()); } - for (final String locator : test.expectations().keySet()) { - softly - .assertThat(target.stateFor(locator, test.expectations().get(locator))) - .describedAs(String.format("State for entity '%s'", locator)) - .containsExactlyInAnyOrderEntriesOf(test.expectations().get(locator)); - } - softly.assertAll(); - result = new TestResult(test.toString(), true, ""); - } catch (final AssertionError error) { - result = new TestResult(test.toString(), false, error.getMessage()); + } catch (IllegalStateException exception) { + result = new TestResult(test.toString(), false, exception.getMessage()); } + return result; }