Skip to content

Commit

Permalink
fix: reduced mockito usage pass 2 for #2707
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Kronegg committed Jul 17, 2023
1 parent e38cae2 commit 2f82475
Show file tree
Hide file tree
Showing 21 changed files with 1,355 additions and 725 deletions.
105 changes: 74 additions & 31 deletions cucumber-core/src/test/java/io/cucumber/core/plugin/PluginsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,122 @@
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.*;

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) {

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import java.util.List;
import java.util.UUID;

class StubPickleStepTestStep implements PickleStepTestStep {
public class StubPickleStepTestStep implements PickleStepTestStep {
private final String pattern;
private final String stepText;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.cucumber.core.runner;

import io.cucumber.core.eventbus.EventBus;
import io.cucumber.core.plugin.StubPickleStepTestStep;
import io.cucumber.core.plugin.StubTestCase;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.plugin.event.EventHandler;
import io.cucumber.plugin.event.PickleStepTestStep;
Expand All @@ -10,50 +12,54 @@
import io.cucumber.plugin.event.TestStepFinished;
import io.cucumber.plugin.event.TestStepStarted;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static java.time.Duration.ZERO;
import static java.time.Instant.EPOCH;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(MockitoExtension.class)
class EventBusTest {

@Test
void handlers_receive_the_events_they_registered_for() {
EventHandler<TestStepFinished> handler = mock(EventHandler.class);
PickleStepTestStep testStep = mock(PickleStepTestStep.class);
MockEventHandler<TestStepFinished> handler = new MockEventHandler<>();
PickleStepTestStep testStep = new StubPickleStepTestStep();
Result result = new Result(Status.PASSED, ZERO, null);
TestCase testCase = mock(TestCase.class);
TestCase testCase = new StubTestCase();
TestStepFinished event = new TestStepFinished(EPOCH, testCase, testStep, result);

EventBus bus = new TimeServiceEventBus(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC")), UUID::randomUUID);
bus.registerHandlerFor(TestStepFinished.class, handler);
bus.send(event);

verify(handler).receive(event);
assertEquals(event, handler.events.get(0));
}

@Test
void handlers_do_not_receive_the_events_they_did_not_registered_for() {
EventHandler handler = mock(EventHandler.class);
PickleStepTestStep testStep = mock(PickleStepTestStep.class);
TestCase testCase = mock(TestCase.class);
MockEventHandler<TestStepFinished> handler = new MockEventHandler<>();
PickleStepTestStep testStep = new StubPickleStepTestStep();
TestCase testCase = new StubTestCase();
TestStepStarted event = new TestStepStarted(EPOCH, testCase, testStep);

EventBus bus = new TimeServiceEventBus(Clock.fixed(Instant.EPOCH, ZoneId.of("UTC")), UUID::randomUUID);
bus.registerHandlerFor(TestStepFinished.class, handler);
bus.send(event);

verify(handler, never()).receive(event);
assertEquals(0, handler.events.size());
}

private static class MockEventHandler<T> implements EventHandler<T> {
final List<T> events = new ArrayList<>();
@Override
public void receive(T event) {
events.add(event);
}
}
}
25 changes: 23 additions & 2 deletions cucumber-core/src/test/java/io/cucumber/core/runner/HookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HookTest {
void after_hooks_execute_before_objects_are_disposed() {
Backend backend = mock(Backend.class);
when(backend.getSnippet()).thenReturn(new TestSnippet());
ObjectFactory objectFactory = mock(ObjectFactory.class);
ObjectFactory objectFactory = new StubObjectFactory();
final HookDefinition hook = mock(HookDefinition.class);
when(hook.getLocation()).thenReturn("hook-location");
when(hook.getTagExpression()).thenReturn("");
Expand All @@ -71,7 +71,7 @@ void after_hooks_execute_before_objects_are_disposed() {
void hook_throws_exception_with_name_when_tag_expression_is_invalid() {
Backend backend = mock(Backend.class);
when(backend.getSnippet()).thenReturn(new TestSnippet());
ObjectFactory objectFactory = mock(ObjectFactory.class);
ObjectFactory objectFactory = new StubObjectFactory();
final HookDefinition hook = mock(HookDefinition.class);
when(hook.getLocation()).thenReturn("hook-location");
when(hook.getTagExpression()).thenReturn("(");
Expand All @@ -90,4 +90,25 @@ void hook_throws_exception_with_name_when_tag_expression_is_invalid() {
is("Invalid tag expression at 'hook-location'"));
}

private static class StubObjectFactory implements ObjectFactory {
@Override
public boolean addClass(Class<?> glueClass) {
return false;
}

@Override
public <T> T getInstance(Class<T> glueClass) {
return null;
}

@Override
public void start() {

}

@Override
public void stop() {

}
}
}
Loading

0 comments on commit 2f82475

Please sign in to comment.