...7import io.cucumber.plugin.event.Status;8import io.cucumber.plugin.event.TestCase;9import io.cucumber.plugin.event.TestCaseFinished;10import io.cucumber.plugin.event.TestStepFinished;11import org.testng.SkipException;12import org.testng.annotations.Test;13import java.net.URI;14import java.time.Clock;15import java.util.UUID;16import static io.cucumber.plugin.event.Status.AMBIGUOUS;17import static io.cucumber.plugin.event.Status.FAILED;18import static io.cucumber.plugin.event.Status.PASSED;19import static io.cucumber.plugin.event.Status.PENDING;20import static io.cucumber.plugin.event.Status.SKIPPED;21import static io.cucumber.plugin.event.Status.UNDEFINED;22import static java.time.Duration.ZERO;23import static java.time.Instant.now;24import static java.util.Collections.singletonList;25import static java.util.Objects.requireNonNull;26import static org.hamcrest.CoreMatchers.is;27import static org.hamcrest.MatcherAssert.assertThat;28import static org.hamcrest.core.Is.isA;29import static org.mockito.Mockito.mock;30import static org.mockito.Mockito.when;31import static org.testng.Assert.assertEquals;32import static org.testng.Assert.assertFalse;33import static org.testng.Assert.assertNull;34import static org.testng.Assert.assertThrows;35import static org.testng.Assert.assertTrue;36import static org.testng.Assert.expectThrows;37public class TestCaseResultObserverTest {38 private final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);39 private final URI uri = URI.create("file:path/to.feature");40 private final int line = 0;41 private final Exception error = new Exception();42 private final TestCase testCase = mock(TestCase.class);43 private final PickleStepTestStep step = createPickleStepTestStep();44 private PickleStepTestStep createPickleStepTestStep() {45 PickleStepTestStep step = mock(PickleStepTestStep.class);46 when(step.getStepLine()).thenReturn(line);47 when(step.getUri()).thenReturn(uri);48 when(step.getStepText()).thenReturn("some step");49 return step;50 }51 @Test52 public void should_be_passed_for_passed_result() throws Throwable {53 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);54 Result stepResult = new Result(Status.PASSED, ZERO, null);55 bus.send(new TestStepFinished(now(), testCase, step, stepResult));56 Result testCaseResult = new Result(Status.PASSED, ZERO, null);57 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));58 resultListener.assertTestCasePassed();59 }60 @Test61 public void should_not_be_passed_for_failed_result() {62 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);63 Result stepResult = new Result(FAILED, ZERO, error);64 bus.send(new TestStepFinished(now(), testCase, step, stepResult));65 Result testCaseResult = new Result(FAILED, ZERO, error);66 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));67 Exception exception = expectThrows(Exception.class, resultListener::assertTestCasePassed);68 assertEquals(exception, error);69 }70 @Test71 public void should_not_be_passed_for_ambiguous_result() {72 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);73 Result stepResult = new Result(AMBIGUOUS, ZERO, error);74 bus.send(new TestStepFinished(now(), testCase, step, stepResult));75 Result testCaseResult = new Result(AMBIGUOUS, ZERO, error);76 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));77 Exception exception = expectThrows(Exception.class, resultListener::assertTestCasePassed);78 assertEquals(exception, error);79 }80 @Test81 public void should_be_skipped_for_undefined_result() {82 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);83 bus.send(new SnippetsSuggestedEvent(now(), uri, line, line, singletonList("stub snippet")));84 Result stepResult = new Result(UNDEFINED, ZERO, error);85 bus.send(new TestStepFinished(now(), testCase, step, stepResult));86 Result testCaseResult = new Result(UNDEFINED, ZERO, error);87 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));88 SkipException skipException = expectThrows(SkipException.class, resultListener::assertTestCasePassed);89 assertThat(skipException.isSkip(), is(true));90 assertThat(skipException.getMessage(), is("" +91 "The step \"some step\" is undefined. You can implement it using the snippet(s) below:\n" +92 "\n" +93 "stub snippet\n"94 ));95 }96 @Test97 public void should_not_be_skipped_for_undefined_result_in_strict_mode() {98 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, true);99 bus.send(new SnippetsSuggestedEvent(now(), uri, line, line, singletonList("stub snippet")));100 Result stepResult = new Result(UNDEFINED, ZERO, error);101 bus.send(new TestStepFinished(now(), testCase, step, stepResult));102 Result testCaseResult = new Result(UNDEFINED, ZERO, error);103 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));104 SkipException skipException = expectThrows(SkipException.class, resultListener::assertTestCasePassed);105 assertThat(skipException.isSkip(), is(false));106 assertThat(skipException.getMessage(), is("" +107 "The step \"some step\" is undefined. You can implement it using the snippet(s) below:\n" +108 "\n" +109 "stub snippet\n"110 ));111 }112 @Test113 public void should_be_passed_for_empty_scenario() throws Throwable {114 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);115 Result testCaseResult = new Result(PASSED, ZERO, error);116 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));117 resultListener.assertTestCasePassed();118 }119 @Test120 public void should_be_skipped_for_pending_result() {121 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);122 Exception error = new TestPendingException();123 Result stepResult = new Result(PENDING, ZERO, error);124 bus.send(new TestStepFinished(now(), testCase, step, stepResult));125 Result testCaseResult = new Result(PENDING, ZERO, error);126 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));127 expectThrows(SkipException.class, resultListener::assertTestCasePassed);128 }129 @Test130 public void should_not_be_skipped_for_pending_result_in_strict_mode() {131 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, true);132 TestPendingException error = new TestPendingException();133 Result stepResult = new Result(PENDING, ZERO, error);134 bus.send(new TestStepFinished(now(), testCase, step, stepResult));135 Result testCaseResult = new Result(PENDING, ZERO, error);136 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));137 Exception exception = expectThrows(Exception.class, resultListener::assertTestCasePassed);138 assertEquals(exception, error);139 }140 @Test141 public void should_be_skipped_for_skipped_result() {142 TestCaseResultObserver resultListener = TestCaseResultObserver.observe(bus, false);143 Result stepResult = new Result(SKIPPED, ZERO, null);144 bus.send(new TestStepFinished(now(), testCase, step, stepResult));145 Result testCaseResult = new Result(SKIPPED, ZERO, null);146 bus.send(new TestCaseFinished(now(), testCase, testCaseResult));147 expectThrows(SkipException.class, resultListener::assertTestCasePassed);148 }149}...