-
Notifications
You must be signed in to change notification settings - Fork 53
/
JUnitDescriptionGeneratorTest.java
494 lines (430 loc) · 19.7 KB
/
JUnitDescriptionGeneratorTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
package com.github.valfirst.jbehave.junit.monitoring;
import static java.util.Arrays.asList;
import static org.hamcrest.CoreMatchers.everyItem;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static java.util.Collections.singletonList;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.hamcrest.BaseMatcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.jbehave.core.annotations.AfterScenario.Outcome;
import org.jbehave.core.annotations.ScenarioType;
import org.jbehave.core.annotations.Scope;
import org.jbehave.core.configuration.Configuration;
import org.jbehave.core.configuration.Keywords;
import org.jbehave.core.embedder.PerformableTree;
import org.jbehave.core.embedder.PerformableTree.ExamplePerformableScenario;
import org.jbehave.core.embedder.PerformableTree.PerformableRoot;
import org.jbehave.core.embedder.PerformableTree.PerformableScenario;
import org.jbehave.core.embedder.PerformableTree.PerformableStory;
import org.jbehave.core.model.ExamplesTable;
import org.jbehave.core.model.GivenStories;
import org.jbehave.core.model.Lifecycle;
import org.jbehave.core.model.Lifecycle.Steps;
import org.jbehave.core.model.Meta;
import org.jbehave.core.model.Scenario;
import org.jbehave.core.model.Story;
import org.jbehave.core.steps.BeforeOrAfterStep;
import org.jbehave.core.steps.CandidateSteps;
import org.jbehave.core.steps.StepCandidate;
import org.jbehave.core.steps.StepCollector.Stage;
import org.jbehave.core.steps.StepType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;
@RunWith(MockitoJUnitRunner.class)
public class JUnitDescriptionGeneratorTest {
private static final String DEFAULT_SCENARIO_TITLE = "Default Scenario Title";
private static final String GIVEN_STEP = "Given Step1";
private static final Class<?> STEPS_TYPE = Object.class;
@Mock
private StepCandidate stepCandidate;
@Mock
private CandidateSteps steps;
@Mock
private GivenStories givenStories;
@Mock
private Configuration configuration;
private JUnitDescriptionGenerator generator;
@Before
public void setUp() {
when(steps.listCandidates()).thenReturn(singletonList(stepCandidate));
when(stepCandidate.matches(anyString(), ArgumentMatchers.isNull())).thenReturn(true);
when(stepCandidate.getStepsType()).then((Answer<Class<?>>) invocation -> STEPS_TYPE);
when(givenStories.getPaths()).thenReturn(Collections.emptyList());
when(configuration.keywords()).thenReturn(new Keywords());
}
@Test
public void shouldCountSteps() {
Scenario scenario = createScenario(GIVEN_STEP);
Story story = createStory(scenario);
createDescriptionFrom(story);
assertEquals(1, generator.getTestCases());
}
@Test
public void shouldNotCountComments() {
Scenario scenario = createScenario(GIVEN_STEP, "!-- This is a comment");
Story story = createStory(scenario);
createDescriptionFrom(story);
assertEquals(1, generator.getTestCases());
}
@Test
public void shouldCountIgnoredSteps() {
Scenario scenario = createScenario(GIVEN_STEP, "!-- Then ignored step");
Story story = createStory(scenario);
createDescriptionFrom(story);
assertEquals(2, generator.getTestCases());
}
@Test
public void shouldExcludeFilteredOutStory() {
Story story = createStory(createScenario(GIVEN_STEP));
createDescriptionFrom(false, true, story);
assertEquals(0, generator.getTestCases());
}
@Test
public void shouldGenerateDescriptionForStory() {
String step1 = GIVEN_STEP;
String step2 = "And step with table param:\n|Head|\n|Value|";
when(stepCandidate.matches(step2, "Given ")).thenReturn(true);
when(stepCandidate.getStartingWord()).thenReturn("Given");
Lifecycle lifecycle = new Lifecycle(createBeforeLifecycleSteps(), createAfterLifecycleSteps());
Scenario scenario = createScenario(step1, step2);
Story story = createStory(lifecycle, scenario);
List<Description> storyDescriptions = createDescriptionFrom(story);
assertEquals(10, generator.getTestCases());
assertEquals(1, storyDescriptions.size());
Description storyDescription = storyDescriptions.get(0);
assertEquals(Description.createSuiteDescription(story.getName()), storyDescription);
List<Description> storyLevelDescriptions = asList(
Description.createTestDescription(STEPS_TYPE, "Then before STORY"),
Description.createSuiteDescription("Scenario: " + scenario.getTitle()),
Description.createTestDescription(STEPS_TYPE, "Then after ANY STORY"));
assertEquals(storyLevelDescriptions, storyDescription.getChildren());
Description scenarioDescription = storyDescription.getChildren().get(1);
List<Description> scenarioLevelDescriptions = asList(
Description.createTestDescription(STEPS_TYPE, "Then before SCENARIO"),
Description.createTestDescription(STEPS_TYPE, "Then before STEP"),
Description.createTestDescription(STEPS_TYPE, step1),
Description.createTestDescription(STEPS_TYPE, "Then after STEP"),
Description.createTestDescription(STEPS_TYPE, "Then before STEP\u200B"),
Description.createTestDescription(Object.class, "And step with table param:"),
Description.createTestDescription(STEPS_TYPE, "Then after STEP\u200B"),
Description.createTestDescription(STEPS_TYPE, "Then after ANY SCENARIO"));
assertEquals(scenarioLevelDescriptions, scenarioDescription.getChildren());
}
@Test
public void shouldStripLinebreaksFromScenarioDescriptions() {
Scenario scenario = mock(Scenario.class);
when(scenario.getGivenStories()).thenReturn(givenStories);
when(scenario.getTitle()).thenReturn("Scenario with\nNewline");
Story story = createStory(scenario);
List<Description> descriptions = createDescriptionFrom(story);
assertEquals(1, descriptions.size());
assertThat(firstChild(descriptions.get(0)).getDisplayName(), not(containsString("\n")));
}
@Test
public void shouldStripCarriageReturnsFromScenarioDescriptions() {
Scenario scenario = mock(Scenario.class);
when(scenario.getGivenStories()).thenReturn(givenStories);
when(scenario.getTitle()).thenReturn("Scenario with\rCarriage Return");
Story story = createStory(scenario);
List<Description> descriptions = createDescriptionFrom(story);
assertEquals(1, descriptions.size());
assertThat(firstChild(descriptions.get(0)).getDisplayName(), not(containsString("\r")));
}
@Test
public void shouldCopeWithSeeminglyDuplicateSteps() {
Scenario scenario = createScenario(GIVEN_STEP, GIVEN_STEP);
when(stepCandidate.matches(GIVEN_STEP, "Given ")).thenReturn(true);
when(stepCandidate.getStartingWord()).thenReturn("Given");
Description description = createDescriptionFrom(scenario);
assertThat(description.getChildren(), everyItem(whoseDisplayName(startsWith(GIVEN_STEP))));
assertEquals(2, description.getChildren().size());
assertThat(description.getChildren(), allChildrenHaveUniqueDisplayNames());
assertEquals(2, generator.getTestCases());
}
@Test
public void shouldCopeWithDuplicateGivenStories() {
Story story = createStory(createScenario(), createScenario());
when(givenStories.getPaths()).thenReturn(singletonList("/some/path/to/GivenStory.story"));
List<Description> descriptions = createDescriptionFrom(story);
assertEquals(1, descriptions.size());
Description description = descriptions.get(0);
Description firstScenario = firstChild(description);
Description secondScenario = description.getChildren().get(1);
assertThat(firstChild(firstScenario).getDisplayName(), is(not(firstChild(secondScenario).getDisplayName())));
}
@Test
public void shouldGenerateDescriptionForGivenStories() {
Scenario scenario = createScenario();
when(givenStories.getPaths()).thenReturn(singletonList("/some/path/to/GivenStory.story"));
Description description = createDescriptionFrom(scenario);
assertThat(firstChild(description), hasProperty("displayName", is("GivenStory\u2024story")));
assertEquals(1, generator.getTestCases());
}
@Test
public void shouldGenerateDescriptionForExampleTablesOnScenario() {
int rowsNumber = 2;
ExamplesTable examplesTable = mockExamplesTable(rowsNumber);
Scenario scenario = createScenarioWithExamples(examplesTable, GIVEN_STEP);
Map<String, String> row = examplesTable.getRows().get(0);
Description description = createDescriptionFrom(scenario);
assertThat(description.getChildren().size(), is(rowsNumber));
for (Description exampleDescription : description.getChildren()) {
assertThat(exampleDescription.getChildren(),
hasItem(Matchers.<Description>hasProperty("displayName", startsWith(GIVEN_STEP))));
assertThat(exampleDescription, hasProperty("displayName", startsWith("Example: " + row)));
// Test is for verifying that the getAnnotations() is successfully executed
assertThat(exampleDescription.getAnnotations(), is(not(nullValue())));
}
}
@Test
public void shouldGenerateChildrenForComposedSteps() {
Scenario scenario = createScenario(GIVEN_STEP);
String compositeStep1 = "When compositeStep1";
String compositeStep2 = "When compositeStep2";
when(stepCandidate.composedSteps()).thenReturn(new String[] { compositeStep1, compositeStep2 });
StepCandidate composedStep1 = mockStepCandidate(compositeStep1, null);
StepCandidate composedStep2 = mockStepCandidate(compositeStep2, "When ");
when(stepCandidate.matches(anyString(), ArgumentMatchers.isNull())).thenReturn(false);
when(stepCandidate.matches(GIVEN_STEP, null)).thenReturn(true);
when(stepCandidate.isComposite()).thenReturn(true);
when(steps.listCandidates()).thenReturn(asList(stepCandidate, composedStep1, composedStep2));
Description description = createDescriptionFrom(scenario);
Description composedStep = firstChild(description);
verify(stepCandidate, times(0)).getStepsInstance();
assertThat(composedStep.getChildren(),
everyItem(hasProperty("displayName", startsWith("When compositeStep"))));
assertEquals(2, composedStep.getChildren().size());
assertTrue(composedStep.isSuite());
assertThat(composedStep.getDisplayName(), startsWith(GIVEN_STEP));
assertEquals(2, generator.getTestCases());
}
@Test
public void shouldCreateDescriptionForAndStep() {
Scenario scenario = createScenario(GIVEN_STEP, "And Step2");
when(stepCandidate.matches(GIVEN_STEP, null)).thenReturn(true);
when(stepCandidate.matches("And Step2", StepType.GIVEN.toString() + " ")).thenReturn(true);
when(stepCandidate.getStepType()).thenReturn(StepType.GIVEN);
when(stepCandidate.getStartingWord()).thenReturn("GIVEN");
Description description = createDescriptionFrom(scenario);
assertEquals(2, description.getChildren().size());
}
@Test
public void shouldGenerateDescriptionForPendingSteps() {
Scenario scenario = createScenario(GIVEN_STEP);
when(stepCandidate.matches(anyString(), ArgumentMatchers.isNull())).thenReturn(false);
Description description = createDescriptionFrom(scenario);
assertEquals(1, description.getChildren().size());
assertThat(description.getChildren(),
everyItem(hasProperty("displayName", containsString("PENDING"))));
assertEquals(1, generator.getTestCases());
}
@Test
public void shouldSkipExampleTablesForParameterizedGivenStories() {
// jbehave ignores example tables, when given stories are parameterized
Scenario scenario = createScenario(GIVEN_STEP);
when(givenStories.getPaths()).thenReturn(singletonList("/some/path/to/GivenStory.story#{0}"));
Description description = createDescriptionFrom(scenario);
assertThat(firstChild(description), hasProperty("displayName", is("GivenStory\u2024story")));
assertEquals(2, generator.getTestCases());
assertEquals(2, description.getChildren().size());
for (Description exampleDescription : description.getChildren()) {
assertEquals(0, exampleDescription.getChildren().size());
}
}
@Test
public void shouldExcludeFilteredOutScenarios() {
Story story = createStory(createScenario(GIVEN_STEP));
createDescriptionFrom(true, false, story);
assertEquals(0, generator.getTestCases());
}
@Test
public void shouldCountBeforeScenarioStepWithAnyType() {
mockListBeforeOrAfterScenarioCall(ScenarioType.ANY);
Scenario scenario = createScenario(GIVEN_STEP);
createDescriptionFrom(scenario);
assertEquals(2, generator.getTestCases());
}
@Test
public void shouldCountBeforeScenarioStepWithNormalType() {
mockListBeforeOrAfterScenarioCall(ScenarioType.NORMAL);
Scenario scenario = createScenario(GIVEN_STEP);
createDescriptionFrom(scenario);
assertEquals(2, generator.getTestCases());
}
@Test
public void shouldCountBeforeScenarioStepWithAnyAndNormalTypes() {
mockListBeforeOrAfterScenarioCall(ScenarioType.ANY, ScenarioType.NORMAL);
Scenario scenario = createScenario(GIVEN_STEP);
createDescriptionFrom(scenario);
assertEquals(2, generator.getTestCases());
}
@Test
public void shouldCountBeforeScenarioStepWithExampleType() {
mockListBeforeOrAfterScenarioCall(ScenarioType.EXAMPLE);
Scenario scenario = createScenario(GIVEN_STEP);
createDescriptionFrom(scenario);
assertEquals(1, generator.getTestCases());
}
private void mockListBeforeOrAfterScenarioCall(ScenarioType... scenarioTypes) {
Method method = new Object(){}.getClass().getEnclosingMethod();
for(ScenarioType scenarioType : scenarioTypes) {
when(steps.listBeforeOrAfterScenario(scenarioType)).thenReturn(
singletonList(new BeforeOrAfterStep(Stage.BEFORE, method, null)));
}
}
private Description createDescriptionFrom(Scenario scenario) {
return createDescriptionFrom(true, scenario);
}
private Description createDescriptionFrom(boolean allowed, Scenario scenario) {
PerformableScenario performableScenario = mockPerformableScenario(scenario, null, allowed);
generator = new JUnitDescriptionGenerator(singletonList(steps), configuration);
return generator.createDescriptionFrom(Lifecycle.EMPTY, performableScenario);
}
private List<Description> createDescriptionFrom(Story story) {
return createDescriptionFrom(true, true, story);
}
private List<Description> createDescriptionFrom(boolean storyAllowed, boolean scenariosAllowed, Story story) {
List<PerformableScenario> performableScenarios = mockPerformableScenarios(story.getPath(), scenariosAllowed,
story.getScenarios());
PerformableStory performableStory = mock(PerformableStory.class);
when(performableStory.isAllowed()).thenReturn(Boolean.valueOf(storyAllowed));
when(performableStory.getStory()).thenReturn(story);
when(performableStory.getScenarios()).thenReturn(performableScenarios);
List<PerformableStory> performableStories = new ArrayList<>();
performableStories.add(performableStory);
PerformableRoot performableRoot = mock(PerformableRoot.class);
when(performableRoot.getStories()).thenReturn(performableStories);
PerformableTree performableTree = mock(PerformableTree.class);
when(performableTree.getRoot()).thenReturn(performableRoot);
generator = new JUnitDescriptionGenerator(singletonList(steps), configuration);
return generator.createDescriptionFrom(performableTree);
}
private List<PerformableScenario> mockPerformableScenarios(String storyPath, boolean scenariosAllowed,
List<Scenario> scenarios) {
List<PerformableScenario> performableScenarios = new ArrayList<>(scenarios.size());
for (Scenario scenario : scenarios) {
performableScenarios.add(mockPerformableScenario(scenario, storyPath, scenariosAllowed));
}
return performableScenarios;
}
private PerformableScenario mockPerformableScenario(Scenario scenario, String storyPath, boolean allowed) {
PerformableScenario performableScenario = new PerformableScenario(scenario, storyPath);
performableScenario.allowed(allowed);
if (scenario.hasExamplesTable()) {
for (Map<String, String> row : scenario.getExamplesTable().getRows()) {
ExamplePerformableScenario exampleScenario = mock(ExamplePerformableScenario.class);
when(exampleScenario.getParameters()).thenReturn(row);
performableScenario.addExampleScenario(exampleScenario);
}
}
return performableScenario;
}
private List<Steps> createBeforeLifecycleSteps() {
List<Steps> steps = new ArrayList<>();
for (Scope scope : Scope.values()) {
steps.add(new Steps(scope, null, singletonList("Then before " + scope)));
}
return steps;
}
private List<Steps> createAfterLifecycleSteps() {
List<Steps> steps = new ArrayList<>();
for (Scope scope : new Scope[] { Scope.SCENARIO, Scope.STORY }) {
for (Outcome outcome : Outcome.values()) {
steps.add(new Steps(scope, outcome, singletonList("Then after " + outcome + " " + scope)));
}
}
steps.add(new Steps(Scope.STEP, Outcome.ANY, singletonList("Then after "+ Scope.STEP)));
return steps;
}
private Matcher<Description> whoseDisplayName(Matcher<String> startsWith) {
return hasProperty("displayName", startsWith);
}
private StepCandidate mockStepCandidate(String name, String previousNonAndStep) {
StepCandidate stepCandidate = mock(StepCandidate.class);
when(stepCandidate.getStepsType()).then((Answer<Class<?>>) invocation -> STEPS_TYPE);
when(stepCandidate.matches(name, previousNonAndStep)).thenReturn(true);
when(stepCandidate.getStartingWord()).thenReturn(name.split(" ")[0]);
return stepCandidate;
}
private Story createStory(Scenario... scenarios) {
return createStory(Lifecycle.EMPTY, scenarios);
}
private Story createStory(Lifecycle lifecycle, Scenario... scenarios) {
Story story = new Story("storyPath", null, Meta.EMPTY, null, null, lifecycle, asList(scenarios));
story.namedAs("Default Story Name");
return story;
}
private Scenario createScenario(String... steps) {
return createScenarioWithExamples(ExamplesTable.EMPTY, steps);
}
private Scenario createScenarioWithExamples(ExamplesTable examplesTable, String... steps) {
return new Scenario(DEFAULT_SCENARIO_TITLE, Meta.EMPTY, givenStories, examplesTable, asList(steps));
}
private ExamplesTable mockExamplesTable(int rowsNumber) {
ExamplesTable examplesTable = mock(ExamplesTable.class);
Map<String, String> row = new TreeMap<>();
List<Map<String, String>> rows = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
row.put("key" + i, "value" + i);
}
for (int i = 1; i <= rowsNumber; i++) {
rows.add(row);
}
when(examplesTable.getRows()).thenReturn(rows);
return examplesTable;
}
private Description firstChild(Description description) {
return description.getChildren().get(0);
}
private Matcher<List<Description>> allChildrenHaveUniqueDisplayNames() {
return new BaseMatcher<List<Description>>() {
private List<Description> descriptions;
@Override
@SuppressWarnings("unchecked")
public boolean matches(Object descriptions) {
this.descriptions = (List<Description>) descriptions;
Set<String> displayNames = new HashSet<>();
for (Description child : this.descriptions) {
displayNames.add(child.getDisplayName());
}
return displayNames.size() == this.descriptions.size();
}
@Override
public void describeTo(org.hamcrest.Description description) {
description
.appendText("Children of description do not have unique display names");
for (Description child : descriptions) {
description.appendText(child.getDisplayName());
}
}
};
}
}