Skip to content

Commit 2d43dfb

Browse files
author
Julien Kronegg
committed
feat: reduced mockito usage (pass 1: simple cases) for #2707
1 parent ceb1211 commit 2d43dfb

27 files changed

+866
-241
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,24 @@
11
package io.cucumber.core.order;
22

33
import io.cucumber.core.gherkin.Pickle;
4+
import io.cucumber.core.gherkin.Step;
45
import io.cucumber.plugin.event.Location;
56
import org.junit.jupiter.api.Test;
6-
import org.junit.jupiter.api.extension.ExtendWith;
7-
import org.mockito.Mock;
8-
import org.mockito.junit.jupiter.MockitoExtension;
97

108
import java.net.URI;
119
import java.util.Arrays;
1210
import java.util.List;
1311

1412
import static org.hamcrest.MatcherAssert.assertThat;
1513
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
16-
import static org.mockito.Mockito.when;
1714

18-
@ExtendWith(MockitoExtension.class)
1915
class PickleOrderTest {
2016

21-
@Mock
22-
Pickle firstPickle;
23-
24-
@Mock
25-
Pickle secondPickle;
26-
27-
@Mock
28-
Pickle thirdPickle;
29-
3017
@Test
3118
void lexical_uri_order() {
32-
when(firstPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
33-
when(firstPickle.getLocation()).thenReturn(new Location(2, -1));
34-
when(secondPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
35-
when(secondPickle.getLocation()).thenReturn(new Location(3, -1));
36-
when(thirdPickle.getUri()).thenReturn(URI.create("file:com/example/b.feature"));
19+
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
20+
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
21+
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));
3722

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

4328
@Test
4429
void reverse_lexical_uri_order() {
45-
when(firstPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
46-
when(firstPickle.getLocation()).thenReturn(new Location(2, -1));
47-
when(secondPickle.getUri()).thenReturn(URI.create("file:com/example/a.feature"));
48-
when(secondPickle.getLocation()).thenReturn(new Location(3, -1));
49-
when(thirdPickle.getUri()).thenReturn(URI.create("file:com/example/b.feature"));
30+
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
31+
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
32+
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));
5033

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

5639
@Test
5740
void random_order() {
41+
Pickle firstPickle = new StubPickle(new Location(2, -1), URI.create("file:com/example/a.feature"));
42+
Pickle secondPickle = new StubPickle(new Location(3, -1), URI.create("file:com/example/a.feature"));
43+
Pickle thirdPickle = new StubPickle(null, URI.create("file:com/example/b.feature"));
44+
5845
PickleOrder order = StandardPickleOrders.random(42);
5946
List<Pickle> pickles = order.orderPickles(Arrays.asList(firstPickle, secondPickle, thirdPickle));
6047
assertThat(pickles, contains(secondPickle, firstPickle, thirdPickle));
6148
}
6249

50+
private static class StubPickle implements Pickle {
51+
private final Location location;
52+
private final URI uri;
53+
54+
public StubPickle(Location location, URI uri) {
55+
this.location = location;
56+
this.uri = uri;
57+
}
58+
59+
@Override
60+
public String getKeyword() {
61+
return null;
62+
}
63+
64+
@Override
65+
public String getLanguage() {
66+
return null;
67+
}
68+
69+
@Override
70+
public String getName() {
71+
return null;
72+
}
73+
74+
@Override
75+
public Location getLocation() {
76+
return location;
77+
}
78+
79+
@Override
80+
public Location getScenarioLocation() {
81+
return null;
82+
}
83+
84+
@Override
85+
public List<Step> getSteps() {
86+
return null;
87+
}
88+
89+
@Override
90+
public List<String> getTags() {
91+
return null;
92+
}
93+
94+
@Override
95+
public URI getUri() {
96+
return uri;
97+
}
98+
99+
@Override
100+
public String getId() {
101+
return null;
102+
}
103+
}
63104
}

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import static org.hamcrest.number.OrderingComparison.greaterThan;
2828
import static org.hamcrest.number.OrderingComparison.lessThan;
2929
import static org.junit.jupiter.api.Assertions.assertAll;
30-
import static org.mockito.BDDMockito.given;
31-
import static org.mockito.Mockito.mock;
3230

3331
class CanonicalEventOrderTest {
3432

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

8381
private static TestCaseStarted createTestCaseEvent(Instant instant, URI uri, int line) {
84-
final TestCase testCase = mock(TestCase.class);
85-
given(testCase.getUri()).willReturn(uri);
86-
given(testCase.getLocation()).willReturn(new Location(line, -1));
87-
return new TestCaseStarted(instant, testCase);
82+
return new TestCaseStarted(instant, new StubTestCase(uri, new Location(line, -1)));
8883
}
8984

9085
@Test

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@
88
import io.cucumber.messages.types.Envelope;
99
import io.cucumber.plugin.ConcurrentEventListener;
1010
import io.cucumber.plugin.EventListener;
11-
import io.cucumber.plugin.event.EventHandler;
12-
import io.cucumber.plugin.event.EventPublisher;
13-
import io.cucumber.plugin.event.PickleStepTestStep;
14-
import io.cucumber.plugin.event.Result;
15-
import io.cucumber.plugin.event.Status;
16-
import io.cucumber.plugin.event.TestCase;
17-
import io.cucumber.plugin.event.TestRunFinished;
18-
import io.cucumber.plugin.event.TestRunStarted;
19-
import io.cucumber.plugin.event.TestStepFinished;
11+
import io.cucumber.plugin.event.*;
2012
import org.junit.jupiter.api.AfterEach;
2113
import org.junit.jupiter.api.Test;
2214
import org.junit.jupiter.api.function.Executable;
@@ -48,7 +40,6 @@
4840
import static org.junit.jupiter.api.Assertions.assertAll;
4941
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5042
import static org.junit.jupiter.api.Assertions.assertThrows;
51-
import static org.mockito.Mockito.mock;
5243

5344
class PluginFactoryTest {
5445

@@ -196,8 +187,8 @@ void plugin_does_not_buffer_its_output() {
196187
EventBus bus = new TimeServiceEventBus(new ClockStub(ZERO), UUID::randomUUID);
197188
plugin.setEventPublisher(bus);
198189
Result result = new Result(Status.PASSED, ZERO, null);
199-
TestStepFinished event = new TestStepFinished(bus.getInstant(), mock(TestCase.class),
200-
mock(PickleStepTestStep.class), result);
190+
TestStepFinished event = new TestStepFinished(bus.getInstant(), new StubTestCase(),
191+
new StubPickleStepTestStep(), result);
201192
bus.send(event);
202193

203194
assertThat(mockSystemOut.toString(), is(not(equalTo(""))));

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

+104-13
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22

33
import io.cucumber.core.eventbus.EventBus;
44
import io.cucumber.core.runtime.TimeServiceEventBus;
5-
import io.cucumber.plugin.event.HookTestStep;
6-
import io.cucumber.plugin.event.PickleStepTestStep;
7-
import io.cucumber.plugin.event.Result;
8-
import io.cucumber.plugin.event.TestCase;
9-
import io.cucumber.plugin.event.TestRunFinished;
10-
import io.cucumber.plugin.event.TestStepFinished;
5+
import io.cucumber.plugin.event.*;
116
import org.junit.jupiter.api.BeforeEach;
127
import org.junit.jupiter.api.Test;
138

149
import java.io.ByteArrayOutputStream;
10+
import java.net.URI;
1511
import java.time.Clock;
1612
import java.time.Duration;
1713
import java.time.Instant;
14+
import java.util.List;
1815
import java.util.UUID;
1916

2017
import static io.cucumber.core.plugin.Bytes.bytes;
@@ -24,13 +21,14 @@
2421
import static org.hamcrest.CoreMatchers.equalTo;
2522
import static org.hamcrest.MatcherAssert.assertThat;
2623
import static org.hamcrest.text.IsEqualCompressingWhiteSpace.equalToCompressingWhiteSpace;
27-
import static org.mockito.Mockito.mock;
2824

2925
class ProgressFormatterTest {
3026

3127
final EventBus bus = new TimeServiceEventBus(Clock.systemUTC(), UUID::randomUUID);
3228
final ByteArrayOutputStream out = new ByteArrayOutputStream();
3329
final ProgressFormatter formatter = new ProgressFormatter(out);
30+
private final MockTestCase mocktestCase = new MockTestCase();
31+
private final MockPickleStepTestStep mockPickleStepTestStep = new MockPickleStepTestStep();
3432

3533
@BeforeEach
3634
void setup() {
@@ -47,43 +45,136 @@ void prints_empty_line_for_empty_test_run() {
4745
@Test
4846
void prints_empty_line_and_green_dot_for_passing_test_run() {
4947
Result result = new Result(PASSED, Duration.ZERO, null);
50-
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(PickleStepTestStep.class), result));
48+
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
5149
bus.send(new TestRunFinished(Instant.now(), result));
5250
assertThat(out, bytes(equalToCompressingWhiteSpace(AnsiEscapes.GREEN + "." + AnsiEscapes.RESET + "\n")));
5351
}
5452

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

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

6967
@Test
7068
void print_nothing_for_passed_hook() {
7169
Result result = new Result(PASSED, Duration.ZERO, null);
72-
bus.send(new TestStepFinished(Instant.now(), mock(TestCase.class), mock(HookTestStep.class), result));
70+
bus.send(new TestStepFinished(Instant.now(), mocktestCase, mockPickleStepTestStep, result));
7371
}
7472

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

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

87+
private class MockTestCase implements TestCase {
88+
@Override
89+
public Integer getLine() {
90+
return null;
91+
}
92+
93+
@Override
94+
public Location getLocation() {
95+
return null;
96+
}
97+
98+
@Override
99+
public String getKeyword() {
100+
return null;
101+
}
102+
103+
@Override
104+
public String getName() {
105+
return null;
106+
}
107+
108+
@Override
109+
public String getScenarioDesignation() {
110+
return null;
111+
}
112+
113+
@Override
114+
public List<String> getTags() {
115+
return null;
116+
}
117+
118+
@Override
119+
public List<TestStep> getTestSteps() {
120+
return null;
121+
}
122+
123+
@Override
124+
public URI getUri() {
125+
return null;
126+
}
127+
128+
@Override
129+
public UUID getId() {
130+
return null;
131+
}
132+
}
133+
134+
private class MockPickleStepTestStep implements PickleStepTestStep {
135+
@Override
136+
public String getPattern() {
137+
return null;
138+
}
139+
140+
@Override
141+
public Step getStep() {
142+
return null;
143+
}
144+
145+
@Override
146+
public List<Argument> getDefinitionArgument() {
147+
return null;
148+
}
149+
150+
@Override
151+
public StepArgument getStepArgument() {
152+
return null;
153+
}
154+
155+
@Override
156+
public int getStepLine() {
157+
return 0;
158+
}
159+
160+
@Override
161+
public URI getUri() {
162+
return null;
163+
}
164+
165+
@Override
166+
public String getStepText() {
167+
return null;
168+
}
169+
170+
@Override
171+
public String getCodeLocation() {
172+
return null;
173+
}
174+
175+
@Override
176+
public UUID getId() {
177+
return null;
178+
}
179+
}
89180
}

0 commit comments

Comments
 (0)