Skip to content

Commit f0ff764

Browse files
Julien KroneggU117293
Julien Kronegg
authored and
U117293
committed
fix: reduced mockito usage (rebasing) for #2707
1 parent 0bb5e41 commit f0ff764

File tree

21 files changed

+1353
-722
lines changed

21 files changed

+1353
-722
lines changed

cucumber-core/src/test/java/io/cucumber/core/plugin/PluginsTest.java

+74-31
Original file line numberDiff line numberDiff line change
@@ -6,79 +6,122 @@
66
import io.cucumber.plugin.EventListener;
77
import io.cucumber.plugin.StrictAware;
88
import io.cucumber.plugin.event.Event;
9+
import io.cucumber.plugin.event.EventHandler;
910
import io.cucumber.plugin.event.EventPublisher;
1011
import org.junit.jupiter.api.Test;
11-
import org.junit.jupiter.api.extension.ExtendWith;
12-
import org.mockito.ArgumentCaptor;
13-
import org.mockito.ArgumentMatchers;
14-
import org.mockito.Captor;
15-
import org.mockito.Mock;
16-
import org.mockito.junit.jupiter.MockitoExtension;
17-
18-
import static org.hamcrest.MatcherAssert.assertThat;
19-
import static org.hamcrest.core.Is.is;
20-
import static org.hamcrest.core.IsEqual.equalTo;
21-
import static org.mockito.ArgumentMatchers.eq;
22-
import static org.mockito.Mockito.mock;
23-
import static org.mockito.Mockito.times;
24-
import static org.mockito.Mockito.verify;
25-
26-
@ExtendWith({ MockitoExtension.class })
12+
13+
import java.util.ArrayList;
14+
import java.util.HashMap;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
2720
class PluginsTest {
2821

2922
private final PluginFactory pluginFactory = new PluginFactory();
30-
@Mock
31-
private EventPublisher rootEventPublisher;
32-
@Captor
33-
private ArgumentCaptor<EventPublisher> eventPublisher;
3423

3524
@Test
3625
void shouldSetStrictOnPlugin() {
3726
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
3827
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
39-
StrictAware plugin = mock(StrictAware.class);
28+
MockStrictAware plugin = new MockStrictAware();
4029
plugins.addPlugin(plugin);
41-
verify(plugin).setStrict(true);
30+
assertTrue(plugin.strict);
4231
}
4332

4433
@Test
4534
void shouldSetMonochromeOnPlugin() {
4635
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
4736
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
48-
ColorAware plugin = mock(ColorAware.class);
37+
MockColorAware plugin = new MockColorAware();
4938
plugins.addPlugin(plugin);
50-
verify(plugin).setMonochrome(false);
39+
assertFalse(plugin.monochrome);
5140
}
5241

5342
@Test
5443
void shouldSetConcurrentEventListener() {
5544
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
5645
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
57-
ConcurrentEventListener plugin = mock(ConcurrentEventListener.class);
46+
MockConcurrentEventListener plugin = new MockConcurrentEventListener();
47+
EventPublisher rootEventPublisher = new MockEventPublisher();
5848
plugins.addPlugin(plugin);
5949
plugins.setEventBusOnEventListenerPlugins(rootEventPublisher);
60-
verify(plugin, times(1)).setEventPublisher(rootEventPublisher);
50+
51+
assertIterableEquals(List.of(rootEventPublisher), plugin.eventPublishers);
6152
}
6253

6354
@Test
6455
void shouldSetNonConcurrentEventListener() {
6556
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
6657
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
67-
EventListener plugin = mock(EventListener.class);
58+
MockEventListener plugin = new MockEventListener();
59+
EventPublisher rootEventPublisher = new MockEventPublisher();
6860
plugins.addPlugin(plugin);
6961
plugins.setSerialEventBusOnEventListenerPlugins(rootEventPublisher);
70-
verify(plugin, times(1)).setEventPublisher(eventPublisher.capture());
71-
assertThat(eventPublisher.getValue().getClass(), is(equalTo(CanonicalOrderEventPublisher.class)));
62+
63+
assertEquals(1, plugin.eventPublishers.size());
64+
assertInstanceOf(CanonicalOrderEventPublisher.class, plugin.eventPublishers.get(0));
7265
}
7366

7467
@Test
7568
void shouldRegisterCanonicalOrderEventPublisherWithRootEventPublisher() {
7669
RuntimeOptions runtimeOptions = RuntimeOptions.defaultOptions();
7770
Plugins plugins = new Plugins(pluginFactory, runtimeOptions);
78-
EventListener plugin = mock(EventListener.class);
71+
MockEventListener plugin = new MockEventListener();
72+
MockEventPublisher rootEventPublisher = new MockEventPublisher();
7973
plugins.addPlugin(plugin);
8074
plugins.setSerialEventBusOnEventListenerPlugins(rootEventPublisher);
81-
verify(rootEventPublisher, times(1)).registerHandlerFor(eq(Event.class), ArgumentMatchers.any());
75+
76+
List<EventHandler<?>> eventHandlers = rootEventPublisher.handlers.get(Event.class);
77+
assertNotNull(eventHandlers);
78+
assertEquals(1, eventHandlers.size());
79+
}
80+
81+
@SuppressWarnings("deprecation")
82+
private static class MockStrictAware implements StrictAware {
83+
Boolean strict;
84+
@Override
85+
public void setStrict(boolean strict) {
86+
this.strict = strict;
87+
}
8288
}
8389

90+
private static class MockColorAware implements ColorAware {
91+
Boolean monochrome;
92+
@Override
93+
public void setMonochrome(boolean monochrome) {
94+
this.monochrome = monochrome;
95+
}
96+
}
97+
98+
private static class MockConcurrentEventListener implements ConcurrentEventListener {
99+
final List<EventPublisher> eventPublishers = new ArrayList<>();
100+
@Override
101+
public void setEventPublisher(EventPublisher publisher) {
102+
eventPublishers.add(publisher);
103+
}
104+
}
105+
106+
private static class MockEventListener implements EventListener {
107+
final List<EventPublisher> eventPublishers = new ArrayList<>();
108+
@Override
109+
public void setEventPublisher(EventPublisher publisher) {
110+
eventPublishers.add(publisher);
111+
}
112+
}
113+
114+
private static class MockEventPublisher implements EventPublisher {
115+
final Map<Class<?>, List<EventHandler<?>>> handlers = new HashMap<>();
116+
@Override
117+
public <T> void registerHandlerFor(Class<T> eventType, EventHandler<T> handler) {
118+
List<EventHandler<?>> eventHandlers = handlers.computeIfAbsent(eventType, key -> new ArrayList<>());
119+
eventHandlers.add(handler);
120+
}
121+
122+
@Override
123+
public <T> void removeHandlerFor(Class<T> eventType, EventHandler<T> handler) {
124+
125+
}
126+
}
84127
}

cucumber-core/src/test/java/io/cucumber/core/plugin/StubPickleStepTestStep.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import java.util.List;
1010
import java.util.UUID;
1111

12-
class StubPickleStepTestStep implements PickleStepTestStep {
12+
public class StubPickleStepTestStep implements PickleStepTestStep {
1313
private final String pattern;
1414
private final String stepText;
1515

Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.cucumber.core.runner;
22

33
import io.cucumber.core.eventbus.EventBus;
4+
import io.cucumber.core.plugin.StubPickleStepTestStep;
5+
import io.cucumber.core.plugin.StubTestCase;
46
import io.cucumber.core.runtime.TimeServiceEventBus;
57
import io.cucumber.plugin.event.EventHandler;
68
import io.cucumber.plugin.event.PickleStepTestStep;
@@ -10,50 +12,54 @@
1012
import io.cucumber.plugin.event.TestStepFinished;
1113
import io.cucumber.plugin.event.TestStepStarted;
1214
import org.junit.jupiter.api.Test;
13-
import org.junit.jupiter.api.extension.ExtendWith;
14-
import org.mockito.junit.jupiter.MockitoExtension;
1515

1616
import java.time.Clock;
1717
import java.time.Instant;
1818
import java.time.ZoneId;
19+
import java.util.ArrayList;
20+
import java.util.List;
1921
import java.util.UUID;
2022

2123
import static java.time.Duration.ZERO;
2224
import static java.time.Instant.EPOCH;
23-
import static org.mockito.Mockito.mock;
24-
import static org.mockito.Mockito.never;
25-
import static org.mockito.Mockito.verify;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
2626

27-
@ExtendWith(MockitoExtension.class)
2827
class EventBusTest {
2928

3029
@Test
3130
void handlers_receive_the_events_they_registered_for() {
32-
EventHandler<TestStepFinished> handler = mock(EventHandler.class);
33-
PickleStepTestStep testStep = mock(PickleStepTestStep.class);
31+
MockEventHandler<TestStepFinished> handler = new MockEventHandler<>();
32+
PickleStepTestStep testStep = new StubPickleStepTestStep();
3433
Result result = new Result(Status.PASSED, ZERO, null);
35-
TestCase testCase = mock(TestCase.class);
34+
TestCase testCase = new StubTestCase();
3635
TestStepFinished event = new TestStepFinished(EPOCH, testCase, testStep, result);
3736

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

42-
verify(handler).receive(event);
41+
assertEquals(event, handler.events.get(0));
4342
}
4443

4544
@Test
4645
void handlers_do_not_receive_the_events_they_did_not_registered_for() {
47-
EventHandler handler = mock(EventHandler.class);
48-
PickleStepTestStep testStep = mock(PickleStepTestStep.class);
49-
TestCase testCase = mock(TestCase.class);
46+
MockEventHandler<TestStepFinished> handler = new MockEventHandler<>();
47+
PickleStepTestStep testStep = new StubPickleStepTestStep();
48+
TestCase testCase = new StubTestCase();
5049
TestStepStarted event = new TestStepStarted(EPOCH, testCase, testStep);
5150

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

56-
verify(handler, never()).receive(event);
55+
assertEquals(0, handler.events.size());
5756
}
5857

58+
private static class MockEventHandler<T> implements EventHandler<T> {
59+
final List<T> events = new ArrayList<>();
60+
@Override
61+
public void receive(T event) {
62+
events.add(event);
63+
}
64+
}
5965
}

cucumber-core/src/test/java/io/cucumber/core/runner/HookTest.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class HookTest {
4646
void after_hooks_execute_before_objects_are_disposed() {
4747
Backend backend = mock(Backend.class);
4848
when(backend.getSnippet()).thenReturn(new TestSnippet());
49-
ObjectFactory objectFactory = mock(ObjectFactory.class);
49+
ObjectFactory objectFactory = new StubObjectFactory();
5050
final HookDefinition hook = mock(HookDefinition.class);
5151
when(hook.getLocation()).thenReturn("hook-location");
5252
when(hook.getTagExpression()).thenReturn("");
@@ -71,7 +71,7 @@ void after_hooks_execute_before_objects_are_disposed() {
7171
void hook_throws_exception_with_name_when_tag_expression_is_invalid() {
7272
Backend backend = mock(Backend.class);
7373
when(backend.getSnippet()).thenReturn(new TestSnippet());
74-
ObjectFactory objectFactory = mock(ObjectFactory.class);
74+
ObjectFactory objectFactory = new StubObjectFactory();
7575
final HookDefinition hook = mock(HookDefinition.class);
7676
when(hook.getLocation()).thenReturn("hook-location");
7777
when(hook.getTagExpression()).thenReturn("(");
@@ -90,4 +90,25 @@ void hook_throws_exception_with_name_when_tag_expression_is_invalid() {
9090
is("Invalid tag expression at 'hook-location'"));
9191
}
9292

93+
private static class StubObjectFactory implements ObjectFactory {
94+
@Override
95+
public boolean addClass(Class<?> glueClass) {
96+
return false;
97+
}
98+
99+
@Override
100+
public <T> T getInstance(Class<T> glueClass) {
101+
return null;
102+
}
103+
104+
@Override
105+
public void start() {
106+
107+
}
108+
109+
@Override
110+
public void stop() {
111+
112+
}
113+
}
93114
}

0 commit comments

Comments
 (0)