Skip to content

Reduced mockito usage #2767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions cucumber-core/pom.xml
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@
<xmlunit.version>2.10.0</xmlunit.version>
<hamcrest.version>3.0</hamcrest.version>
<hamcrest-json.version>0.2</hamcrest-json.version>
<mockito.version>5.14.2</mockito.version>
<vertx.version>4.5.10</vertx.version>
<reactive-streams.version>1.0.4</reactive-streams.version>
</properties>
@@ -143,12 +142,6 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
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));
@@ -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));
@@ -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
@@ -6,7 +6,6 @@
import io.cucumber.plugin.event.SnippetsSuggestedEvent;
import io.cucumber.plugin.event.SnippetsSuggestedEvent.Suggestion;
import io.cucumber.plugin.event.Status;
import io.cucumber.plugin.event.TestCase;
import io.cucumber.plugin.event.TestCaseStarted;
import io.cucumber.plugin.event.TestRunFinished;
import io.cucumber.plugin.event.TestRunStarted;
@@ -27,8 +26,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 {

@@ -81,10 +78,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
Original file line number Diff line number Diff line change
@@ -10,10 +10,8 @@
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;
@@ -48,7 +46,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 {

@@ -196,8 +193,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(""))));
110 changes: 79 additions & 31 deletions cucumber-core/src/test/java/io/cucumber/core/plugin/PluginsTest.java
Original file line number Diff line number Diff line change
@@ -6,79 +6,127 @@
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.StrictAware;
import io.cucumber.plugin.event.Event;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.EventPublisher;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

@ExtendWith({ MockitoExtension.class })

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class PluginsTest {

private final PluginFactory pluginFactory = new PluginFactory();
@Mock
private EventPublisher rootEventPublisher;
@Captor
private ArgumentCaptor<EventPublisher> eventPublisher;

@Test
void shouldSetStrictOnPlugin() {
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
StrictAware plugin = mock(StrictAware.class);
MockStrictAware plugin = new MockStrictAware();
plugins.addPlugin(plugin);
verify(plugin).setStrict(true);
assertTrue(plugin.strict);
}

@Test
void shouldSetMonochromeOnPlugin() {
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
ColorAware plugin = mock(ColorAware.class);
MockColorAware plugin = new MockColorAware();
plugins.addPlugin(plugin);
verify(plugin).setMonochrome(false);
assertFalse(plugin.monochrome);
}

@Test
void shouldSetConcurrentEventListener() {
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
ConcurrentEventListener plugin = mock(ConcurrentEventListener.class);
MockConcurrentEventListener plugin = new MockConcurrentEventListener();
EventPublisher rootEventPublisher = new MockEventPublisher();
plugins.addPlugin(plugin);
plugins.setEventBusOnEventListenerPlugins(rootEventPublisher);
verify(plugin, times(1)).setEventPublisher(rootEventPublisher);

assertIterableEquals(List.of(rootEventPublisher), plugin.eventPublishers);
}

@Test
void shouldSetNonConcurrentEventListener() {
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
EventListener plugin = mock(EventListener.class);
MockEventListener plugin = new MockEventListener();
EventPublisher rootEventPublisher = new MockEventPublisher();
plugins.addPlugin(plugin);
plugins.setSerialEventBusOnEventListenerPlugins(rootEventPublisher);
verify(plugin, times(1)).setEventPublisher(eventPublisher.capture());
assertThat(eventPublisher.getValue().getClass(), is(equalTo(CanonicalOrderEventPublisher.class)));

assertEquals(1, plugin.eventPublishers.size());
assertInstanceOf(CanonicalOrderEventPublisher.class, plugin.eventPublishers.get(0));
}

@Test
void shouldRegisterCanonicalOrderEventPublisherWithRootEventPublisher() {
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
EventListener plugin = mock(EventListener.class);
MockEventListener plugin = new MockEventListener();
MockEventPublisher rootEventPublisher = new MockEventPublisher();
plugins.addPlugin(plugin);
plugins.setSerialEventBusOnEventListenerPlugins(rootEventPublisher);
verify(rootEventPublisher, times(1)).registerHandlerFor(eq(Event.class), ArgumentMatchers.any());

List<EventHandler<?>> eventHandlers = rootEventPublisher.handlers.get(Event.class);
assertNotNull(eventHandlers);
assertEquals(1, eventHandlers.size());
}

@SuppressWarnings("deprecation")
private static class MockStrictAware implements StrictAware {
Boolean strict;
@Override
public void setStrict(boolean strict) {
this.strict = strict;
}
}

private static class MockColorAware implements ColorAware {
Boolean monochrome;
@Override
public void setMonochrome(boolean monochrome) {
this.monochrome = monochrome;
}
}

private static class MockConcurrentEventListener implements ConcurrentEventListener {
final List<EventPublisher> eventPublishers = new ArrayList<>();
@Override
public void setEventPublisher(EventPublisher publisher) {
eventPublishers.add(publisher);
}
}

private static class MockEventListener implements EventListener {
final List<EventPublisher> eventPublishers = new ArrayList<>();
@Override
public void setEventPublisher(EventPublisher publisher) {
eventPublishers.add(publisher);
}
}

private static class MockEventPublisher implements EventPublisher {
final Map<Class<?>, List<EventHandler<?>>> handlers = new HashMap<>();
@Override
public <T> void registerHandlerFor(Class<T> eventType, EventHandler<T> handler) {
List<EventHandler<?>> eventHandlers = handlers.computeIfAbsent(eventType, key -> new ArrayList<>());
eventHandlers.add(handler);
}

@Override
public <T> void removeHandlerFor(Class<T> eventType, EventHandler<T> handler) {

}
}
}
Loading