Skip to content

Commit

Permalink
feat: reduced mockito usage (pass 1: simple cases) for #2707
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Kronegg committed Jul 17, 2023
1 parent ceb1211 commit 2d43dfb
Show file tree
Hide file tree
Showing 27 changed files with 866 additions and 241 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,24 @@
package io.cucumber.core.order;

import io.cucumber.core.gherkin.Pickle;
import io.cucumber.core.gherkin.Step;
import io.cucumber.plugin.event.Location;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class PickleOrderTest {

@Mock
Pickle firstPickle;

@Mock
Pickle secondPickle;

@Mock
Pickle thirdPickle;

@Test
void lexical_uri_order() {
when(firstPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(firstPickle.getLocation()).thenReturn(new Location(2, -1));
when(secondPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(secondPickle.getLocation()).thenReturn(new Location(3, -1));
when(thirdPickle.getUri()).thenReturn(URI.create("file:com/example/b.feature"));
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));

PickleOrder order = StandardPickleOrders.lexicalUriOrder();
List<Pickle> pickles = order.orderPickles(Arrays.asList(thirdPickle, secondPickle, firstPickle));
Expand All @@ -42,11 +27,9 @@ void lexical_uri_order() {

@Test
void reverse_lexical_uri_order() {
when(firstPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(firstPickle.getLocation()).thenReturn(new Location(2, -1));
when(secondPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
when(secondPickle.getLocation()).thenReturn(new Location(3, -1));
when(thirdPickle.getUri()).thenReturn(URI.create("file:com/example/b.feature"));
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));

PickleOrder order = StandardPickleOrders.reverseLexicalUriOrder();
List<Pickle> pickles = order.orderPickles(Arrays.asList(secondPickle, thirdPickle, firstPickle));
Expand All @@ -55,9 +38,67 @@ void reverse_lexical_uri_order() {

@Test
void random_order() {
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));

PickleOrder order = StandardPickleOrders.random(42);
List<Pickle> pickles = order.orderPickles(Arrays.asList(firstPickle, secondPickle, thirdPickle));
assertThat(pickles, contains(secondPickle, firstPickle, thirdPickle));
}

private static class StubPickle implements Pickle {
private final Location location;
private final URI uri;

public StubPickle(Location location, URI uri) {
this.location = location;
this.uri = uri;
}

@Override
public String getKeyword() {
return null;
}

@Override
public String getLanguage() {
return null;
}

@Override
public String getName() {
return null;
}

@Override
public Location getLocation() {
return location;
}

@Override
public Location getScenarioLocation() {
return null;
}

@Override
public List<Step> getSteps() {
return null;
}

@Override
public List<String> getTags() {
return null;
}

@Override
public URI getUri() {
return uri;
}

@Override
public String getId() {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.hamcrest.number.OrderingComparison.lessThan;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

class CanonicalEventOrderTest {

Expand Down Expand Up @@ -81,10 +79,7 @@ class CanonicalEventOrderTest {
new Result(Status.PASSED, Duration.ZERO, null));

private static TestCaseStarted createTestCaseEvent(Instant instant, URI uri, int line) {
final TestCase testCase = mock(TestCase.class);
given(testCase.getUri()).willReturn(uri);
given(testCase.getLocation()).willReturn(new Location(line, -1));
return new TestCaseStarted(instant, testCase);
return new TestCaseStarted(instant, new StubTestCase(uri, new Location(line, -1)));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,7 @@
import io.cucumber.messages.types.Envelope;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.Status;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -48,7 +40,6 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;

class PluginFactoryTest {

Expand Down Expand Up @@ -196,8 +187,8 @@ void plugin_does_not_buffer_its_output() {
EventBus bus = new TimeServiceEventBus(new ClockStub(ZERO), UUID::randomUUID);
plugin.setEventPublisher(bus);
Result result = new Result(Status.PASSED, ZERO, null);
TestStepFinished event = new TestStepFinished(bus.getInstant(), mock(TestCase.class),
mock(PickleStepTestStep.class), result);
TestStepFinished event = new TestStepFinished(bus.getInstant(), new StubTestCase(),
new StubPickleStepTestStep(), result);
bus.send(event);

assertThat(mockSystemOut.toString(), is(not(equalTo(""))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@

import io.cucumber.core.eventbus.EventBus;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.plugin.event.HookTestStep;
import io.cucumber.plugin.event.PickleStepTestStep;
import io.cucumber.plugin.event.Result;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.net.URI;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.UUID;

import static io.cucumber.core.plugin.Bytes.bytes;
Expand All @@ -24,13 +21,14 @@
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace;
import static org.mockito.Mockito.mock;

class ProgressFormatterTest {

final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ProgressFormatter formatter = new ProgressFormatter(out);
private final MockTestCase mocktestCase = new MockTestCase();
private final MockPickleStepTestStep mockPickleStepTestStep = new MockPickleStepTestStep();

@BeforeEach
void setup() {
Expand All @@ -47,43 +45,136 @@ void prints_empty_line_for_empty_test_run() {
@Test
void prints_empty_line_and_green_dot_for_passing_test_run() {
Result result = new Result(PASSED, Duration.ZERO, null);
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result));
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
bus.send(new TestRunFinished(Instant.now(), result));
assertThat(out, bytes(equalToCompressingWhiteSpace(AnsiEscapes.GREEN + "." + AnsiEscapes.RESET + "\n")));
}

@Test
void print_green_dot_for_passing_step() {
Result result = new Result(PASSED, Duration.ZERO, null);
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result));
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
assertThat(out, bytes(equalTo(AnsiEscapes.GREEN + "." + AnsiEscapes.RESET)));
}

@Test
void print_yellow_U_for_undefined_step() {
Result result = new Result(UNDEFINED, Duration.ZERO, null);
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result));
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
assertThat(out, bytes(equalTo(AnsiEscapes.YELLOW + "U" + AnsiEscapes.RESET)));
}

@Test
void print_nothing_for_passed_hook() {
Result result = new Result(PASSED, Duration.ZERO, null);
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(HookTestStep.class), result));
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
}

@Test
void print_red_F_for_failed_step() {
Result result = new Result(FAILED, Duration.ZERO, null);
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result));
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
assertThat(out, bytes(equalTo(AnsiEscapes.RED + "F" + AnsiEscapes.RESET)));
}

@Test
void print_red_F_for_failed_hook() {
Result result = new Result(FAILED, Duration.ZERO, null);
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(HookTestStep.class), result));
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
assertThat(out, bytes(equalTo(AnsiEscapes.RED + "F" + AnsiEscapes.RESET)));
}

private class MockTestCase implements TestCase {
@Override
public Integer getLine() {
return null;
}

@Override
public Location getLocation() {
return null;
}

@Override
public String getKeyword() {
return null;
}

@Override
public String getName() {
return null;
}

@Override
public String getScenarioDesignation() {
return null;
}

@Override
public List<String> getTags() {
return null;
}

@Override
public List<TestStep> getTestSteps() {
return null;
}

@Override
public URI getUri() {
return null;
}

@Override
public UUID getId() {
return null;
}
}

private class MockPickleStepTestStep implements PickleStepTestStep {
@Override
public String getPattern() {
return null;
}

@Override
public Step getStep() {
return null;
}

@Override
public List<Argument> getDefinitionArgument() {
return null;
}

@Override
public StepArgument getStepArgument() {
return null;
}

@Override
public int getStepLine() {
return 0;
}

@Override
public URI getUri() {
return null;
}

@Override
public String getStepText() {
return null;
}

@Override
public String getCodeLocation() {
return null;
}

@Override
public UUID getId() {
return null;
}
}
}
Loading

0 comments on commit 2d43dfb

Please sign in to comment.