Skip to content

Commit

Permalink
Fix test failure by comparing json instead of strings (#2822)
Browse files Browse the repository at this point in the history
Co-authored-by: bongale2 <[email protected]>
Co-authored-by: M.P. Korstanje <[email protected]>
  • Loading branch information
3 people authored Dec 3, 2023
1 parent d53c2a6 commit 42dbaee
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
import io.cucumber.messages.types.TestRunFinished;
import io.cucumber.messages.types.TestRunStarted;
import io.cucumber.messages.types.Timestamp;
import org.json.JSONException;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.time.Clock;
import java.util.UUID;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
import static org.skyscreamer.jsonassert.JSONCompareMode.STRICT;

public class MessageFormatterTest {

@Test
void test() {
void test() throws JSONException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
MessageFormatter formatter = new MessageFormatter(bytes);
EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
Expand All @@ -32,9 +35,15 @@ void test() {
bus.send(Envelope.of(testRunFinished));

String ndjson = new String(bytes.toByteArray(), UTF_8);
assertThat(ndjson, containsString("" +
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}\n" +
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}\n"));
String[] actual = ndjson.split("\\n");
String[] expected = {
"{\"testRunStarted\":{\"timestamp\":{\"seconds\":10,\"nanos\":0}}}",
"{\"testRunFinished\":{\"success\":true,\"timestamp\":{\"seconds\":15,\"nanos\":0}}}"
};
assertThat(actual.length, equalTo(expected.length));
for (int i = 0; i < actual.length; i++) {
assertEquals(expected[i], actual[i], STRICT);
}
}

}

0 comments on commit 42dbaee

Please sign in to comment.