Skip to content

Commit 1dca05d

Browse files
hermyampkorstanje
andauthored
Fix flaky test-cases in HtmlFormatterTest (#2939)
Co-authored-by: M.P. Korstanje <[email protected]>
1 parent d83b51b commit 1dca05d

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

cucumber-core/src/test/java/io/cucumber/core/plugin/HtmlFormatterTest.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@
1818
import java.time.Clock;
1919
import java.util.Collections;
2020
import java.util.UUID;
21+
import java.util.regex.Matcher;
22+
import java.util.regex.Pattern;
2123

22-
import static io.cucumber.core.plugin.Bytes.bytes;
23-
import static org.hamcrest.CoreMatchers.containsString;
24+
import static java.nio.charset.StandardCharsets.UTF_8;
2425
import static org.hamcrest.MatcherAssert.assertThat;
26+
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
27+
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;
2528

2629
class HtmlFormatterTest {
2730

@@ -38,11 +41,11 @@ void writes_index_html() throws Throwable {
3841
TestRunFinished testRunFinished = new TestRunFinished(null, true, new Timestamp(15L, 0L), null);
3942
bus.send(Envelope.of(testRunFinished));
4043

41-
assertThat(bytes, bytes(containsString("" +
42-
"window.CUCUMBER_MESSAGES = [" +
43-
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," +
44-
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" +
45-
"];\n")));
44+
assertEquals("[" +
45+
"{\"testRunStarted\":{\"timestamp\":{\"nanos\":0,\"seconds\":10}}}," +
46+
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"nanos\":0,\"seconds\":15}}}" +
47+
"]",
48+
extractCucumberMessages(bytes), STRICT);
4649
}
4750

4851
@Test
@@ -86,11 +89,17 @@ void ignores_step_definitions() throws Throwable {
8689
null);
8790
bus.send(Envelope.of(testRunFinished));
8891

89-
assertThat(bytes, bytes(containsString("" +
90-
"window.CUCUMBER_MESSAGES = [" +
91-
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}," +
92-
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}" +
93-
"];\n")));
92+
assertEquals("[" +
93+
"{\"testRunStarted\":{\"timestamp\":{\"nanos\":0,\"seconds\":10}}}," +
94+
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"nanos\":0,\"seconds\":15}}}" +
95+
"]",
96+
extractCucumberMessages(bytes), STRICT);
9497
}
9598

99+
private static String extractCucumberMessages(ByteArrayOutputStream bytes) {
100+
Pattern pattern = Pattern.compile("^.*window\\.CUCUMBER_MESSAGES = (\\[.+]);.*$", Pattern.DOTALL);
101+
Matcher matcher = pattern.matcher(new String(bytes.toByteArray(), UTF_8));
102+
assertThat("bytes must match " + pattern, matcher.find());
103+
return matcher.group(1);
104+
}
96105
}

0 commit comments

Comments
 (0)